[英]Rest - Same MediaType in response as in request
I have a web service that consumes XML
and JSON
.我有一个 web 服务,它消耗
XML
和JSON
。
The output is currently always in XML
. output 当前始终位于
XML
中。
Is it possible to produce the response in the same MediaType
as the MediaType
that was consumed?是否可以在与所使用的
MediaType
相同的MediaType
中生成响应?
What i need is:我需要的是:
The request is XML
, the response is XML
too.请求是
XML
,响应也是XML
。
The request is JSON
, the response is JSON
too.请求是
JSON
,响应也是JSON
。
My Code:我的代码:
@Path("/calculate")
public class CalculationService
{
@POST
@Path("/magic")
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Output calculate(Input input)
{
Output output = new Output();
output.setValue1(...);
output.setValue2(...);
output.setValue3(...);
return output;
}
}
By default, the Web service delivers the response as MediaType.APPLICATION_XML
.默认情况下,Web 服务将响应作为
MediaType.APPLICATION_XML
传递。
The MediaType can be controlled by setting the Accept-Header . MediaType 可以通过设置Accept-Header来控制。
By setting the value Accept: application/json
, the response is delivered as MediaType.APPLICATION_JSON
.通过设置值
Accept: application/json
,响应作为MediaType.APPLICATION_JSON
传递。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.