How to set the charset with JAX-RS?

后端 未结 7 1101
时光说笑
时光说笑 2020-12-08 09:30

How can I set the charset with JAX-RS? I\'ve tried @Produces(\"text/html; charset=UTF-8\") but that was ignored and only text/html was send with th

7条回答
  •  执笔经年
    2020-12-08 09:46

    First setup @Produces annotation on your resource class methods.

    Then in MessageBodyWriter of your returned type, you can do this in writeTo() method:

    response.setContentType(mediaType.toString);
    

    Remark: You can inject response in your writer by:

    @Context
    protected HttpServletResponse response;
    

提交回复
热议问题