java - 使用 jaxb 进行 JSON 解码

标签 java json rest maven jaxb

我有一个应用程序,我希望它接受 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 错误,为什么不能正确解码?

我的依赖项是

   <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-server</artifactId>
        <version>1.8</version>

    </dependency>

    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-client</artifactId>
        <version>1.8</version>
    </dependency>

我将 header 设置为 Content-Type:application/json

最佳答案

我猜测您没有在发送到服务的 http 请求上正确设置内容类型。

我不知道您使用什么工具来创建我推荐的请求:

  • Cmd 行 - curl 。你就是无法击败它! Chrome
  • Advanced Rest Client 。与休息服务合作时,这就是最好的选择。

这两个都可以让您查看原始的 http 请求和响应,从而诊断您的问题。

根据经验,设置请求 header 时,冒号和值之间最好有空格。即

Content-Type: application/json;

因此示例 curl 请求如下所示:

curl -X POST -H "Content-Type: application/json" -d '{"KeyProvision":{"Consumer":"testConsumer","API":"posting","AllowedNames":"google"}}' http://localhost:8080/request

然后,Curl 会输出原始请求和响应,以便您可以清楚地看到发生了什么。高级休息客户端执行相同的操作,但还提供了许多用于记住和查看响应/请求的工具。

关于java - 使用 jaxb 进行 JSON 解码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21588727/

相关文章:

java - 在 Julia 套装中制作不同颜色的色调。

android - 改造 - 解析 JSON

python - 使用 Tornado 通过 GET 处理所有 REST 请求

jquery - 代码未循环遍历整个 json 数据

json - 如何解析golang中的json数组?

java - 构建我们的 Java 包

rest - Powershell:Out-File - 访问路径被拒绝

java - 如何检查设备上是否存在应用程序?

java - com.google.inject.Key 无法解析

java - "Display over other apps"