繁体   English   中英

JAXB使用球衣未能成功编组

[英]JAXB Unmarshaling unsuccessful using jersey

我有以下代表POJO对象的类

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


// Class to marshall and unmarshall the XML to POJO

 // This is a class for the request 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 AllowedStoes){

        this.AllowedNames = AllowedNames;

    }

    @XmlElement
    public String  getConsumer(){

        return Consumer;
    }

    @XmlElement
    public String getAPI(){

        return API;
    }

    @XmlElement
    public String getAllowedNames(){

        return AllowedNames;
    }

}

我的课程中映射到请求的函数

@POST
 @Path("/request")
 @Consumes(MediaType.APPLICATION_XML)
 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" ;

 }

如果我改变

 public Response getRequest(KeyProvision keyInfo)

public Response getRequest(String keyInfo)

我可以看到请求已被接受但没有存储为POJO对象。

如果我将其保留为public Response getRequest(KeyProvision keyInfo)public Response getRequest(KeyProvision keyInfo)收到400错误,并显示以下消息<u>The request sent by the client was syntactically incorrect.</u>在我尝试进行请求时,我的REST客户端中。

这是我的请求正文:

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

我在这里缺少的是阻止从XML到POJO的成功解组

通过JAXB默认的命名规则,它将期望元素名称以小写字母开头。 您将需要在@XmlRootElement@XmlElement上指定名称以匹配您的文档。

@XmlRootElement(name="KeyProvision")
public class KeyProvision {

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

JAXB调试技巧

当解组编无法正常工作时,请尝试填充对象模型并进行编组以查看所需的文档结构。

暂无
暂无

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

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