Jersey - @QueryParam 和字符编码

Jersey - @QueryParam and character encoding

我需要在我的 HTTP 请求中传递一个包含来自不同 UTF 语言(德语、塞尔维亚语、加泰罗尼亚语、中文......)的文本的约束值作为查询参数。

尝试从另一侧使用 URLEncoder.encode 和 URLDecoder.decode,但没有用。

尝试使用 UriComponent。encode/decode 也没有帮助。

尝试使用 utf-8 字符集向我的请求添加 header。

尝试使用@Connsumes/@Produces/@Encoded jersy 注释,但还是一样。

客户端:

private static final String ENCODING = "UTF-8";
private static final String CHARSET= "charset=" + ENCODING;

Invocation.Builder builder;
    try {
        builder = baseTarget.path(apiPath + "setConstraint")
                //.queryParam("constraint", constraint)
                .queryParam("constraint",UriComponent.encode(constraint, UriComponent.Type.FRAGMENT))
                .queryParam("path", URLEncoder.encode(jsonRuleIdString, ENCODING))
                .request(MediaType.APPLICATION_JSON)
                .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON + "; " + CHARSET );
    } catch (UnsupportedEncodingException ex) {
        throw new SpecificationException(ex.getMessage());
    }

...

服务器端:

@RequestMapping (method = RequestMethod.POST, value= "setConstraint")
@Consumes({ MediaType.APPLICATION_JSON + ";charset=UTF-8"})
@Produces({ MediaType.APPLICATION_JSON + ";charset=UTF-8"})
public RuleStruct setConstraint( 
        @RequestParam(value="constraint", required = true) @Encoded String constraint,
        @RequestParam(value="path", required = true) String strPath) throws SpecificationException, ProjectManagementException {

    logger.info("Set Constraint");

    RuleId path;
    try {
        path = strPath.isEmpty() ? null : mapper.readValue(URLDecoder.decode(strPath, ENCODING), RuleId.class);
        constraint = UriComponent.decode(constraint, UriComponent.Type.FRAGMENT);
    } catch (IOException e) {
        throw new SpecificationException(e.getMessage());
    }

    return getRuleEditorService(sessionId).editRule(path).setConstraint(constraint);
}

此致。

接着 post: http://jersey.576304.n2.nabble.com/QueryParam-and-character-encoding-td6928014.html

我搜索到 "tomcat query param utf encoding" 并找到了这个解决方案: How to set request encoding in Tomcat?

一旦我将 URIEncoding="UTF-8" 添加到 tomcat 的 server.xml 中的 标签中它工作的容器。