Java @Consumes({"application/xml,application/json"}) 如何编程返回类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21559185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:22:37  来源:igfitidea点击:

@Consumes({"application/xml,application/json"}) how to program the return type

javajsonrestjaxbjersey

提问by user1801279

I have an application and I want it to accept both XML and JSON , how can I program the return type ? for example this is my POJO

我有一个应用程序,我希望它同时接受 XML 和 JSON,我该如何编程返回类型?例如这是我的 POJO

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;


// Class to marshall and unmarshall the XML and JSON to POJO

 // This is a class for the request JSON and XML


@XmlRootElement
public class KeyProvision {

    private String Consumer ; 
    private String API ; 
    private String AllowedNames ; 


    public void setConsumer( String Consumer)
    {
        this.Consumer= Consumer;

    }


    public void setAPI( String API){

        this.API = API;

    }


    public void setAllowedNames(String AllowedNames){

        this.AllowedNames = AllowedNames;

    }

     @XmlElement(name="Consumer")
    public String  getConsumer(){

        return Consumer;
    }

     @XmlElement(name="API")
    public String getAPI(){

        return API;
    }

     @XmlElement(name="AllowedNames")
    public String getAllowedNames(){

        return AllowedNames;
    }

}

My rest interface is

我的休息界面是

    import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@POST
     @Path("/request")
     @Consumes({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
     @Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
     public Response getRequest(KeyProvision keyInfo){

    /* StringReader reader = new StringReader(keyInfo); // this code just leads to an execution failure for some reason 
     try{
         JAXBContext jaxbContext = JAXBContext.newInstance(KeyProvision.class);

         Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
         KeyProvision api = (KeyProvision) jaxbUnmarshaller.unmarshal(reader);
         System.out.println(api);

     }   catch(JAXBException e){
         e.printStackTrace();

     }
      */

     String result = "Track saved : " + keyInfo;
     return Response.status(201).entity(result).build() ;

  //   return "success" ;

 }

my XML is

我的 XML 是

<?xml version="1.0" encoding="UTF-8"?>
<KeyProvision>
<Consumer> testConsumer </Consumer>
<API>posting</API>
<AllowedNames> google</AllowedNames>
</KeyProvision>

my JSON is

我的 JSON 是

{
    "KeyProvision": {
        "Consumer": "testConsumer",
        "API": "posting",
        "AllowedNames": "google",

    }
}

My problems/questions are

我的问题/问题是

1) I keep getting an 415 error when I use the JSON , why is this not unmarshalling properly? 2) Is the reuturn type determined by JAXB?

1) 我在使用 JSON 时不断收到 415 错误,为什么这不能正确解组?2)返回类型是由JAXB决定的吗?

回答by mikemil

That's part of the beauty of Jax-RS - Jaxb annotate your POJO and jax-rs will handle the marshalling and unmarshalling to/from xml/json. You don't have to do that, the provider is supposed to handle a defined subset, of which, JSON and XML are part of.

这就是 Jax-RS 美妙之处的一部分——Jaxb 注释您的 POJO,而 jax-rs 将处理与 xml/json 之间的编组和解组。您不必这样做,提供程序应该处理定义的子集,其中 JSON 和 XML 是其中的一部分。

To answer your second part of the question - the return type is determined by content negotiation process. The client can send the "Accept" header to say what type they want the response in. Without a 'suggestion' from the client, the server is left to try and pick a suitable return type.

回答问题的第二部分 - 返回类型由内容协商过程确定。客户端可以发送“Accept”标头来说明他们想要响应的类型。如果没有来自客户端的“建议”,服务器将尝试选择合适的返回类型。

回答by Bryant Luk

The 415 Unsupported Media Typeis usually because, on your client request, you did not set the proper media type headers. In this case, you need a Content-Type: application/xmlor Content-Type: application/jsonin either your XML or JSON request.

415不支持的媒体类型通常是因为,在您的客户端请求,您没有设置正确的介质类型头。在这种情况下,你需要一个Content-Type: application/xmlContent-Type: application/json在任何您的XML或JSON请求。

JAX-RS depends on the Content-Typerequest header to find the proper JAX-RS Provider to unmarshal the incoming request.

JAX-RS 依赖于Content-Type请求头来找到合适的 JAX-RS 提供者来解组传入的请求。