简体   繁体   English

在Content-Type中指定charset时,Jersey和@FormParam无法正常工作

[英]Jersey and @FormParam not working when charset is specified in the Content-Type

It seems like Jersey 2.0 (using servlet 3.1) is not able to decode a parameter when the charset property is specified in the Content-Type header. 看起来Jersey 2.0(使用servlet 3.1)在Content-Type标头中指定charset属性时无法解码参数。

For example considering the following endpoint: 例如,考虑以下端点:

@POST
@Path("/hello")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.APPLICATION_JSON)
public Response hello(@FormParam("name") String name) {
    System.out.println(name);
    return ok();
}

This curl request works: 这个卷曲请求有效:

curl -X POST -H "content-type: application/x-www-form-urlencoded" -d "name=tom" http://localhost:8080/sampleapp/hello

The following request instead doesn't work and the name parameter is null : 下面的请求,而不是工作,该name参数为null

curl -X POST -H "content-type: application/x-www-form-urlencoded; charset=UTF-8" -d "name=tom" http://localhost:8080/sampleapp/hello

I think the charset=UTF-8 addition in the content type breaks my code. 我认为内容类型中的charset=UTF-8添加会破坏我的代码。

EDIT: 编辑:

I've opened an official ticket just in case this is a bug: https://java.net/jira/browse/JERSEY-1978 我打开了官方机票以防这是一个错误: https//java.net/jira/browse/JERSEY-1978

I think it's a bug. 我认为这是一个错误。

There's a pull request open to support this use case: https://github.com/jersey/jersey/pull/24/files 有一个pull请求可以支持这个用例: https//github.com/jersey/jersey/pull/24/files

In the meantime I'd suggest to use a filter to remove the offending encoding. 与此同时,我建议使用过滤器来删除有问题的编码。

EDIT as per OP comments 根据OP评论编辑

I'm thinking on something along these lines: 我正在考虑这些方面的事情:

@Provider
@PreMatching
public class ContentTypeFilter implements ContainerRequestFilter{

    @Override
    public void filter(ContainerRequestContext requestContext)
            throws IOException {
        MultivaluedMap<String,String> headers=requestContext.getHeaders();
        List<String> contentTypes=headers.remove(HttpHeaders.CONTENT_TYPE);
        if (contentTypes!=null && !contentTypes.isEmpty()){
            String contentType= contentTypes.get(0);
            String sanitizedContentType=contentType.replaceFirst(";.*", "");
            headers.add(HttpHeaders.CONTENT_TYPE, sanitizedContentType);
        }
    }
}

Here is a simple work-around inspired by Carlo's post. 这是一个简单的解决方案,灵感来自Carlo的帖子。 The only modification is to match '; 唯一的修改是匹配'; charset=UTF-8'; 字符集= UTF-8' ; otherwise, 'multipart/form-data; 否则,'multipart / form-data; boundary=...' content types fail. boundary = ...'内容类型失败。

// IMPLEMENTATION NOTE: Resolves an issue with FormParam processing
// @see https://java.net/jira/browse/JERSEY-1978

@Provider
@PreMatching
public class ContentTypeFilter implements ContainerRequestFilter {

    @Override
    public void filter(ContainerRequestContext requestContext) throws IOException {
        MultivaluedMap<String,String> headers = requestContext.getHeaders();
        List<String> contentTypes = headers.remove(HttpHeaders.CONTENT_TYPE);
        if (contentTypes != null && !contentTypes.isEmpty()) {
            String contentType = contentTypes.get(0);
            String sanitizedContentType = contentType.replaceFirst("; charset=UTF-8", "");
            headers.add(HttpHeaders.CONTENT_TYPE, sanitizedContentType);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM