在 Content-Type 中指定字符集时,Jersey 和 @FormParam 不起作用

2023-10-13Java开发问题
1

本文介绍了在 Content-Type 中指定字符集时,Jersey 和 @FormParam 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

Content-Type 标头中指定 charset 属性时,Jersey 2.0(使用 servlet 3.1)似乎无法解码参数.

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.

例如考虑以下端点:

@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();
}

此 curl 请求有效:

This curl request works:

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

以下请求不起作用,并且 name 参数为 null:

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

我认为内容类型中的 charset=UTF-8 添加会破坏我的代码.

I think the charset=UTF-8 addition in the content type breaks my code.

我已经打开了官方票,以防这是一个错误:https://java.net/jira/browse/JERSEY-1978

I've opened an official ticket just in case this is a bug: https://java.net/jira/browse/JERSEY-1978

推荐答案

我认为这是一个错误.

有一个拉取请求开放以支持此用例:https://github.com/jersey/jersey/pull/24/files

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.

编辑根据 OP 评论

我正在考虑以下几点:

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

这篇关于在 Content-Type 中指定字符集时,Jersey 和 @FormParam 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13