简体   繁体   中英

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.

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 :

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.

EDIT:

I've opened an official ticket just in case this is a bug: 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

In the meantime I'd suggest to use a filter to remove the offending encoding.

EDIT as per OP comments

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. The only modification is to match '; charset=UTF-8'; otherwise, 'multipart/form-data; boundary=...' content types fail.

// 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);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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