Skip to content
This repository has been archived by the owner on May 28, 2018. It is now read-only.

Charset dropped for content-type text/html #2209

Open
glassfishrobot opened this issue Jun 20, 2013 · 3 comments
Open

Charset dropped for content-type text/html #2209

glassfishrobot opened this issue Jun 20, 2013 · 3 comments

Comments

@glassfishrobot
Copy link

In Jersey 2.0/GlassFish 4.0 there seems to be some weird behavior when specifying the charset in a @produces annotation:

@Path(...) @Produces("text/html; charset=utf-8") // Charset gets dropped public Response get() {
   return Response.ok("Test");
}

This will drop the charset from the response and only return text/html. However, if you change the content type to anything other than text/html the charset will be left intact:

@Path(...) @Produces("text/plain; charset=utf-8") // Charset won't get dropped public Response get() {
   return Response.ok("Test");
}

The only way I've found to keep the charset with text/html, is to specify it in the response like so:

@Path(...)
public Response get() {
   return Response.ok("Test", "text/html; charset=utf-8"); // Charset won't get dropped }

Cheers

Environment

Jersey 2.0, GlassFish 4.0, NetBeans 7.3.1, Windows 8.

Affected Versions

[2.0]

@glassfishrobot
Copy link
Author

Reported by webd0de

@glassfishrobot
Copy link
Author

mfuksa said:
The problem is probably in the Accept header of the request. I did some tests and it looks like when the Accept header defines "Accept: text/html" then the response will contain only "text/html" and the charset will be removed. In this case, the media type of accept header is used precisely. When Accept header is "Accept: /" the charset is added correctly to the response. So, it looks when there Accept header and Produces media type matches, the Accept header value is used exactly as it is. In my opinion, the charset parameter should be added to the response even in the case that Accept is "text/html". The JAX-RS 2.0 spec, chapter 3.8 does not define these details.

I move this bug to backlog. Here is the test code

public class SimpleCharsetTest extends JerseyTest {
    @Path("resource")
    public static class MyResource {
        @GET
        @Produces("text/html;charset=UTF-8")
        public String get() {
            return "html";
        }
    }

    @Test
    public void test() {
        Response response = target().path("resource").request("*/*").get();
        assertEquals(200, response.getStatus());
        assertEquals("text/html; charset=UTF-8", response.getMediaType().toString());
    }

    @Override
    protected Application configure() {
        return new ResourceConfig(MyResource.class, LoggingFilter.class);
    }
}

if you change the code to :

Response response = target().path("resource").request("text/html").get();

the test will fail.

Hint for fixing the issue: check the code in MethodSelectingRouter.determineResponseMediaType().

@glassfishrobot
Copy link
Author

This issue was imported from java.net JIRA JERSEY-1937

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants