我有一个 api,它在处理 XML post 请求时运行良好。 XML 在 Post 正文中接收并进行相应处理。这是方法的标题
@POST
@Path ("{"+num+"}/"+STATUS+"."+XML)
@Consumes (MediaType.APPLICATION_XML)
@Produces (MediaType.APPLICATION_XML)
public Response getStatusXML (@PathParam(num) String num,
JAXBElement<OrderStatusRequestType> jaxbOrderStatusRequestType,
@Context UriInfo requestUriInfo,
@Context SecurityContext securityContext){
OrderStatusRequestType orderStatusRequestType = jaxbOrderStatusRequestType.getValue();
return processRequest (num, XML, orderStatusRequestType, securityContext);
}
这个 WS 是用这个 POST 主体调用的
<orderStatusRequest>
<vendor>32658</vendor>
<key>232X1</key>
</orderStatusRequest>
我有相应的 XML 模式,这个请求按照要求工作正常。我得到了 jaxbObject,也得到了 orderStatusRequest。但是,当我尝试调用我的 json web 服务时,我得到 null orderStatusRequest
。我有一个单独的方法来使用 json。
@POST
@Path ("{"+num+"}/"+STATUS+"."+JSON)
@Consumes (MediaType.APPLICATION_JSON)
@Produces (MediaType.APPLICATION_JSON)
public Response getStatusJSON(@PathParam(num) String num,
JAXBElement<OrderStatusRequestType> jaxbOrderStatusRequestType,
@Context UriInfo requestUriInfo,
@Context SecurityContext securityContext){
OrderStatusRequestType orderStatusRequestType= jaxbOrderStatusRequestType.getValue();
return processRequest (num, JSON, orderStatusRequestType, securityContext);
}
我在 post 请求中发送以下 json
{"orderStatusRequest":{"vendor":"32658","key":"232X1"}}
我在 jaxbElement 中得到空对象。我不明白为什么。为方便起见,这是我的 XML 模式
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:complexType name="orderStatusRequestType">
<xs:sequence>
<xs:element name="vendor" type="xs:string" minOccurs="1" maxOccurs="1"/>
<xs:element name="key" type="xs:string" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
<xs:element name="orderStatusRequest" type="orderStatusRequestType"/>
</xs:schema>
我写了一个客户端,发送JSON请求
ClientResponse clientResponse = service.path("api")
.path("v1")
.path("personal")
.path("orders")
.path(num)
.path("status.json").accept(MediaType.APPLICATION_JSON).post(ClientResponse.class, jaxbOst);
它给了我错误状态 415,即不支持内容类型。然而,只有 2 个修改,即 status.xml
和 MediaType.APPLICATION_XML
工作正常。
更新 2:
使用 Fiddler,我可以使用 JSON 访问 Web 服务。但是 Jersey 并没有将输入的 POST 正文 marchling 到特定的对象中。我在 vendor
和 key
字段中得到 null。
更新 3
我能够使用来自 Fiddler 的 JSON。 JSON 是 {"vendor":"32658","key":"232X1"}
。我之前将它包裹在另一个物体下。 但是 我仍然无法使用 Jersey Client 发送请求。我猜是我的 JAXB 对象在我尝试发布时给出了 XML
。我如何确保当我使用 objectFactory.createOderRequestStatus
时,它会被翻译成 JSON 而不是 XML?
最佳答案
415 表示您的客户端未在请求中发送正确的内容类型。如果您查看您的路径,对于 XML 和 JSON 方法,它们是相同的,因此您需要确保您的客户端发送正确的内容路径,以便区分哪种方法将为您的请求提供服务。否则 Jersey 只会选择第一个,这就是这里发生的情况。
另一个thread中有关于这个主题的讨论.我自己还没有尝试过,但是您可以向您的客户端添加一些 header 以查看是否可行吗?也许像下面这样:
ClientResponse clientResponse = service.path("api")
.path("v1")
.path("personal")
.path("orders")
.path(num)
.path("status.json")
.accept(MediaType.APPLICATION_JSON)
.header("content-type", MediaType.APPLICATION_JSON)
.post(ClientResponse.class, jaxbOst);
关于java - 无法使用 JSON REST WS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12486347/