java Resteasy javax如何将contenttype*/*映射到服务器端的application/json?
问题:通过更改服务器代码而不是客户端代码,我无法在客户端消息中获取*/*
的内容类型,以映射到服务器端的application/json
。(部署的客户端太多,无法执行后者)
在WildFly 10上,一切都按原样工作(使用^{
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 { ... }
My net搜索的所有内容都指向客户端更改(这会起作用),但我们必须更改服务器端或保留WF10。我还尝试设置字符集行为或在网络中映射媒体类型。xml,但这没有什么区别。例如:
<?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
# 1 楼答案
我找到了一种方法,添加一个ContainerRequestFilter,在匹配之前更改标题。下面是诀窍:
我还必须在我的网站上添加以下内容。xml
# 2 楼答案
在客户端的“内容类型”中使用通配符毫无意义
客户端可以接受多种格式的服务器响应(使用“accept”头)
服务器可以接受(使用)多种媒体,但客户机必须知道他正在有效地发布什么样的数据。如果您不告诉服务器您在http正文中发送了什么,服务器(总是)无法猜到