jersey
  1. jersey
  2. JERSEY-1937

Charset dropped for content-type text/html

    Details

    • Type: Bug Bug
    • Status: Open
    • Priority: Minor Minor
    • Resolution: Unresolved
    • Affects Version/s: 2.0
    • Fix Version/s: backlog
    • Component/s: None
    • Labels:
      None
    • Environment:

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

      Description

      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

        Activity

        Hide
        Miroslav Fuksa added a comment -

        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().

        Show
        Miroslav Fuksa added a comment - 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().

          People

          • Assignee:
            Unassigned
            Reporter:
            webd0de
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated: