Reputation: 3944
I am trying to use the Jersey client to call a REST service (which doesn't use Jersey / JAX-RS). The following code works fine to POST my form data and receive a response:
Form form = new Form();
form.param("email", email);
form.param("password", password);
String clientResponse =
target.request(MediaType.TEXT_PLAIN)
.post(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE),
String.class);
The code runs fine and the clientResponse variable contains the plain text response from the server.
Now, I need to inspect the status code returned by the server. To my understanding, I need to retrieve the response as a ClientResponse object instead of a String. So, I changed my code to:
Form form = new Form();
form.param("email", email);
form.param("password", password);
ClientResponse clientResponse =
target.request(MediaType.TEXT_PLAIN)
.post(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE),
ClientResponse.class);
When I run this new code, I get the following exception:
org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyReader not found for media type=text/plain, type=class org.glassfish.jersey.client.ClientResponse, genericType=class org.glassfish.jersey.client.ClientResponse.
Why am I getting this exception?
Upvotes: 1
Views: 2500
Reputation: 209102
You don't use ClientResponse
. You're supposed to use Response
. And you only need to use the second parameter of .post()
when you want the response entity automatically deserialized. When you don't use the second parameter, the return type is Response
. Then you can read read/deserialize the entity with Response#readEntity()
Response response = target
.request(MediaType.TEXT_PLAIN)
.post(Entity.form(form));
String data = response.readEntity(String.class);
Upvotes: 3