【问题标题】:@Consumes({"application/xml,application/json"}) how to program the return type@Consumes({"application/xml,application/json"}) 如何编程返回类型
【发布时间】:2014-02-28 19:37:31
【问题描述】:

我有一个应用程序,我希望它同时接受 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;
    }

}

我的休息界面是

    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" ;

 }

我的 XML 是

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

我的 JSON 是

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

    }
}

我的问题/疑问是

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

【问题讨论】:

  • 与您的问题无关,但您应该考虑仔细查看您的 setAllowedNames(String) 方法。

标签: java json rest jaxb jersey


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    415 Unsupported Media Type 通常是因为在您的客户端请求中,您没有设置正确的媒体类型标头。在这种情况下,您需要在 XML 或 JSON 请求中使用 Content-Type: application/xml 或 Content-Type: application/json。

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

    【讨论】:

    • 其实我应该早点提到这个。我确实在我的请求中设置了内容类型,它仍然抛出错误,这就是为什么它让我感到困惑
    • JSON 的类路径中是否包含所有 JAR?我不知道您使用的是哪个 JSON 库(Jackson、Jettison 或其他),但可能是 JAX-RS JSON 提供程序未正确注册到运行时。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-21
    • 2013-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-21
    • 2021-08-31
    相关资源
    最近更新 更多