简体   繁体   English

Rest - 响应中的 MediaType 与请求中的相同

[英]Rest - Same MediaType in response as in request

I have a web service that consumes XML and JSON .我有一个 web 服务,它消耗XMLJSON
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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM