eclipse-ee4j / eclipse-ee4j/jersey
Content-Type is evaluated for 204 No Content
- Dominant language
- Java
- Stars
- 730
- Forks
- 382
- PR merge metrics
- No merged PRs in 30d
Description
We consume a REST service using the Jersey client API.
The code we use to execute the GET request is:
```
MyEntity myEntity = webTarget.request(MediaType.APPLICATION_JSON_TYPE).get(MyEntity.class);
```
This works well if the response contains an entity (so the return value is not null).
For empty responses we get the following exception:
> org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyReader not found for media type=text/plain; charset=UTF-8, type=class com.mypkg.MyEntity, genericType=class com.mypkg.MyEntity.
Request/response for the null case are:
> 1 * Request received on thread main
> 1 > GET [https://rest.test.com/myservice/rest/myentity/unknown](https://rest.test.com/myservice/rest/myentity/unknown)
> 1 > Accept: application/json
>
> 2 * Response received on thread main
> 2 < 204
> 2 < Date: Thu, 01 Aug 2013 12:05:50 GMT
> 2 < Content-Length: 0
> 2 < Keep-Alive: timeout=15, max=100
> 2 < Connection: Keep-Alive
> 2 < Content-Type: text/plain; charset=UTF-8
> 2 < Server: Apache
> 2 < Cache-Control: max-age=0
As you can see the HTTP status code is 204.
I do not know why, but the Content-Type 'text/plain' is returned.
This seems to be the cause of the exception above.
The behavior I would expect is that the client first evaluates the HTTP status code and only if applicable evaluates the content related stuff.
So as for status 204 the HTTP spec clearly states that there must be no content in the body, the client should ignore all that content stuff (content-type, content-length, etc.).
Right now our work-around is to get the Response object, check the status code ourself and only if not 204 read the entity. It works but is not the way I expected the client API to work.
#### Environment
JDK 1.6
Jersey 2.0 bundle and Jersey 2.1 bundle
#### Affected Versions
[2.0, 2.1]
Contributor guide
Assessment
This issue has not been assessed yet.