• <i id='lCVhY'><tr id='lCVhY'><dt id='lCVhY'><q id='lCVhY'><span id='lCVhY'><b id='lCVhY'><form id='lCVhY'><ins id='lCVhY'></ins><ul id='lCVhY'></ul><sub id='lCVhY'></sub></form><legend id='lCVhY'></legend><bdo id='lCVhY'><pre id='lCVhY'><center id='lCVhY'></center></pre></bdo></b><th id='lCVhY'></th></span></q></dt></tr></i><div id='lCVhY'><tfoot id='lCVhY'></tfoot><dl id='lCVhY'><fieldset id='lCVhY'></fieldset></dl></div>
    <legend id='lCVhY'><style id='lCVhY'><dir id='lCVhY'><q id='lCVhY'></q></dir></style></legend><tfoot id='lCVhY'></tfoot>
      <bdo id='lCVhY'></bdo><ul id='lCVhY'></ul>

      <small id='lCVhY'></small><noframes id='lCVhY'>

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

        Jersey and @FormParam not working when charset is specified in the Content-Type(在 Content-Type 中指定字符集时,Jersey 和 @FormParam 不起作用)

          • <bdo id='YtD7U'></bdo><ul id='YtD7U'></ul>

                  <tfoot id='YtD7U'></tfoot>
                  <i id='YtD7U'><tr id='YtD7U'><dt id='YtD7U'><q id='YtD7U'><span id='YtD7U'><b id='YtD7U'><form id='YtD7U'><ins id='YtD7U'></ins><ul id='YtD7U'></ul><sub id='YtD7U'></sub></form><legend id='YtD7U'></legend><bdo id='YtD7U'><pre id='YtD7U'><center id='YtD7U'></center></pre></bdo></b><th id='YtD7U'></th></span></q></dt></tr></i><div id='YtD7U'><tfoot id='YtD7U'></tfoot><dl id='YtD7U'><fieldset id='YtD7U'></fieldset></dl></div>
                  <legend id='YtD7U'><style id='YtD7U'><dir id='YtD7U'><q id='YtD7U'></q></dir></style></legend>

                  <small id='YtD7U'></small><noframes id='YtD7U'>

                    <tbody id='YtD7U'></tbody>
                  本文介绍了在 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 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Compiling C++ for the JVM(为 JVM 编译 C++)
                  Compile to java bytecode (without using Java)(编译成java字节码(不使用Java))
                  How to drive C#, C++ or Java compiler to compute 1+2+3+...+1000 at compile time?(如何在编译时驱动 C#、C++ 或 Java 编译器计算 1+2+3+...+1000?)
                  Java ClassLoader: load same class twice(Java ClassLoader:两次加载相同的类)
                  How to debug .class files in ECLIPSE?(如何在 ECLIPSE 中调试 .class 文件?)
                  Java quot;The blank final field may not have been initializedquot; Anonymous Interface vs Lambda Expression(Java“可能尚未初始化空白的最终字段匿名接口与 Lambda 表达式)
                  • <i id='1qvt1'><tr id='1qvt1'><dt id='1qvt1'><q id='1qvt1'><span id='1qvt1'><b id='1qvt1'><form id='1qvt1'><ins id='1qvt1'></ins><ul id='1qvt1'></ul><sub id='1qvt1'></sub></form><legend id='1qvt1'></legend><bdo id='1qvt1'><pre id='1qvt1'><center id='1qvt1'></center></pre></bdo></b><th id='1qvt1'></th></span></q></dt></tr></i><div id='1qvt1'><tfoot id='1qvt1'></tfoot><dl id='1qvt1'><fieldset id='1qvt1'></fieldset></dl></div>

                    <small id='1qvt1'></small><noframes id='1qvt1'>

                    1. <legend id='1qvt1'><style id='1qvt1'><dir id='1qvt1'><q id='1qvt1'></q></dir></style></legend>

                      <tfoot id='1qvt1'></tfoot>
                          <bdo id='1qvt1'></bdo><ul id='1qvt1'></ul>
                            <tbody id='1qvt1'></tbody>