@Consumes中的charset = utf-8无效

时间:2017-12-04 11:54:52

标签: http character-encoding jersey jax-rs

我使用的是泽西岛2.16。对于休息界面,我需要接受' application / xml'并生产应用程序/ xml'与charset = utf-8。我的代码是,

@POST
@Produces("application/xml; charset=utf-8")
@Consumes("application/xml; charset=utf-8")
public String process(String request) {
 ...
return response;

}

在这里,当我从客户端发送请求时,我将Content-Type设置为application/xml;charset=iso-8859-1我希望服务器因为与utf-8不匹配而抛出一些错误。但这不会发生。但是charset=utf-8已设置为响应。这是检查charset的{​​{1}}的正确方法吗?

1 个答案:

答案 0 :(得分:0)

您可以直接使用@HeaderParam注入,然后验证代码中的字符集。

public String process(@HeaderParam("Accept-Charset") String charset,  String request) {

 if (charset.equalsIgnoreCase ("utf-8"))
 {
     // do something
 }
 else
 {
   // update your response.
 }
 ...
return response;