简体   繁体   English

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

[英]@Consumes({“application/xml,application/json”}) how to program the return type

I have an application and I want it to accept both XML and JSON , how can I program the return type ? 我有一个应用程序,我希望它接受XML和JSON,我如何编程返回类型? for example this is my POJO 例如,这是我的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? 1)当我使用JSON时,我一直收到415错误,为什么这不能正确解组? 2) Is the reuturn type determined by JAXB? 2)JAXB是否确定了重新定型类型?

The 415 Unsupported Media Type is usually because, on your client request, you did not set the proper media type headers. 415 Unsupported Media Type通常是因为您的客户端请求未设置正确的媒体类型标头。 In this case, you need a Content-Type: application/xml or Content-Type: application/json in either your XML or JSON request. 在这种情况下,您需要在XML或JSON请求中使用Content-Type: application/xmlContent-Type: application/json

JAX-RS depends on the Content-Type request header to find the proper JAX-RS Provider to unmarshal the incoming request. JAX-RS依赖于Content-Type请求标头来查找正确的JAX-RS Provider以解组传入的请求。

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. 这是Jax-RS之美的一部分 - Jaxb注释你的POJO和jax-rs将处理来自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. 您不必这样做,提供程序应该处理已定义的子集,其中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”标头来说明他们想要响应的类型。如果没有来自客户端的“建议”,服务器将尝试选择合适的返回类型。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 消耗适用于应用程序/ json,但不适用于文本/纯文本 - consumes is working for application/json but not for text/plain 将字符串传递给使用应用程序/ json的REST API - Passing string to a REST API that consumes application/json 在Java中解析应用程序/ json Jersey @Consumes MIME类型的最佳方法是什么? - What is the best way to parse application/json Jersey @Consumes MIME type in Java? @Consumes(MediaType.APPLICATION_JSON) 注释但将请求正文作为字符串获取 - @Consumes(MediaType.APPLICATION_JSON) annotation but getting request body as string 在 RESTful Web 服务中使用 @Consumes(MediaType.APPLICATION_JSON) 的问题 - Problems using @Consumes(MediaType.APPLICATION_JSON) in RESTful web service MediaType.APPLICATION_JSON返回类型给出500内部错误 - MediaType.APPLICATION_JSON return type gives 500 Internal error 不受支持的媒体类型 - Spring 启动 Rest Controller 已消耗/生产 JSON 和 XML 但仅接受 Json - Unsupported Media Type - Spring Boot Rest Controller has consumes/produces JSON and XML but only accepts Json 静态返回类型xml或json - return type xml or json in restful JPA实体作为使用Jax-RS中的application / json的方法中的对象 - JPA Entity as an object in a method which consumes application/json in Jax-RS @ResponseBody 和 @PostMapping(path = &quot;/test&quot;,consumes =...,产生 = MediaType.APPLICATION_JSON_VALUE) 之间的冗余? - Redundancy between @ResponseBody and @PostMapping(path = "/test", consumes =..., produces = MediaType.APPLICATION_JSON_VALUE)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM