如何将内容类型*/*Map到服务器端的application/json?

zi8p0yeb  于 2021-07-09  发布在  Java
关注(0)|答案(3)|浏览(346)

问题:我无法获取 */* 在要Map到的客户端消息中 application/json 在服务器端,通过更改服务器代码而不是客户端代码(部署的客户端太多,无法执行后者)
在wildfly 10上,一切都按原样工作(使用 */* ),但在wildfly 14/18上它失败了

RESTEASY002010: Failed to execute: javax.ws.rs.NotSupportedException: RESTEASY003200: Could not find message body reader for type: class xxxx of content type: */* at org.jboss.resteasy.resteasy-
jaxrs@3.9.1.Final//org.jboss.resteasy.core.interception.ServerReaderInterceptorContext.throwReaderNotFound

以上都是在Java8上实现的。下面的代码显示了客户端更改,该更改将使wf14+在没有服务器端编辑的情况下工作:

HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "*/*");  // works for WF10, not WF14+
// conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); // works for all

适用的服务器端代码如下:

@POST
@Path("fileDownload")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_OCTET_STREAM)
// Consume options tried that still failed:
// @Consumes({ MediaType.APPLICATION_JSON, MediaType.WILDCARD, MediaType.APPLICATION_OCTET_STREAM, "*/*", "*/*\r\n", "*/*; charset=UTF-8" } )
public Response fileDownload(@HeaderParam("Range") String range, FileDownloadRequest fileDwnReq) throws IOException { ... }

我的网络搜索都指向客户端的变化(这将工作),但我们必须改变服务器端或留在wf10。我也尝试过在web.xml中设置字符集行为或Map媒体类型,但没有什么不同。例如:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>MyRestServices</display-name>
    <listener>
    <listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
    </listener-class>
    </listener>

    <!--   
    To specify the previous behavior, in which UTF-8 was used for text media types, but 
    the explicit charset was not appended, the context parameter "resteasy.add.charset" may be 
    set to "false". It defaults to "true". 
    See https://docs.jboss.org/resteasy/docs/3.1.2.Final/userguide/html_single/index.html
    This did not work.
     -->
<!--     <context-param> -->
<!--        <param-name>resteasy.add.charset</param-name> -->
<!--        <param-value>false</param-value> -->
<!--     </context-param> -->

     <context-param>
        <param-name>resteasy.media.type.mappings</param-name>
        <param-value>*/* : application/json</param-value>
<!-- <param-value>html : text/html, json : application/json, xml : application/xml</param-value> -->
    </context-param>

我被难住了。如有任何建议/建议,将不胜感激。
编辑:下面是失败的http请求的转储:

URI=/xxx/fileDownload
 characterEncoding=null
     contentLength=94
       contentType=[*/*]
            header=Accept=text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
            header=Connection=keep-alive
            header=Content-Type=*/*
            header=Content-Length=94
            header=User-Agent=Java/1.8.0_152
            header=Host=127.0.0.1:8014
            locale=[]
            method=POST
          protocol=HTTP/1.1
       queryString=
        remoteAddr=/127.0.0.1:59867
        remoteHost=127.0.0.1
            scheme=http
              host=127.0.0.1:8014
        serverPort=8014
          isSecure=false
--------------------------RESPONSE--------------------------
     contentLength=0
       contentType=null
            header=Connection=keep-alive
            header=Content-Length=0
            header=Date=Thu, 20 Aug 2020 13:36:44 GMT
            status=415
0ejtzxu1

0ejtzxu11#

在客户端的“content type”中使用通配符毫无意义。
客户端可以接受多种格式的服务器响应(使用“accept”头)
服务器可以接受(使用)多种媒体,但是客户机必须知道他正在有效地发布什么样的数据。如果您不告诉服务器您在http主体中发送什么,服务器就不能(总是)猜测它。

j0pj023g

j0pj023g2#

根据文档,您应该能够获得作为字符串参数的请求体,然后手动将其封送到所需的对象形式。

e0bqpujr

e0bqpujr3#

我找到了一种方法,添加一个containerrequestfilter,在匹配之前更改标题。下面是技巧:

import javax.ws.rs.core.UriInfo;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.ext.Provider;
import javax.ws.rs.container.PreMatching;

@Provider
@PreMatching
// Mapp */* to JSON for /filedownload
public final class ContentFilter implements ContainerRequestFilter {
   @Override
   public void filter(ContainerRequestContext ctx) throws IOException {
      UriInfo uri = ctx.getUriInfo();
      if ((uri != null) && uri.getPath().toLowerCase().contains("filedownload")) {
          String ctp = ctx.getHeaderString("Content-Type");
          if ("*/*".equals(ctp))
             ctx.getHeaders().putSingle("Content-Type", "application/json; charset=UTF-8");
      }
   }
}

我还必须在web.xml中添加以下内容

<context-param>
    <param-name>resteasy.providers</param-name>
    <param-value>xxx.yyy.ContentFilter</param-value>
</context-param>

相关问题