简体   繁体   English

JAXB使用球衣未能成功编组

[英]JAXB Unmarshaling unsuccessful using jersey

I have the following class which represents a POJO object 我有以下代表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;
    }

}

A function from my class to which requests are mapped 我的课程中映射到请求的函数

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

 }

if I change 如果我改变

 public Response getRequest(KeyProvision keyInfo)

to

public Response getRequest(String keyInfo)

I can see that the request is accepted but not stored as a POJO object . 我可以看到请求已被接受但没有存储为POJO对象。

if I leave it as public Response getRequest(KeyProvision keyInfo) , I get a 400 error with the following message <u>The request sent by the client was syntactically incorrect.</u> in my REST client when I try making the request. 如果我将其保留为public Response getRequest(KeyProvision keyInfo)public Response getRequest(KeyProvision keyInfo)收到400错误,并显示以下消息<u>The request sent by the client was syntactically incorrect.</u>在我尝试进行请求时,我的REST客户端中。

this is my request body: 这是我的请求正文:

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

What am I missing here that is preventing a successfull Unmarshalling from XML to POJO 我在这里缺少的是阻止从XML到POJO的成功解组

By JAXB defaults naming rules it will expect the element names to start with lower case. 通过JAXB默认的命名规则,它将期望元素名称以小写字母开头。 You will need to specify the names on @XmlRootElement and @XmlElement to match your document. 您将需要在@XmlRootElement@XmlElement上指定名称以匹配您的文档。

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

And

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

JAXB Debugging Trick JAXB调试技巧

When unmarshal isn't working correctly try populating the object model and marshalling it out to see the expected document structure. 当解组编无法正常工作时,请尝试填充对象模型并进行编组以查看所需的文档结构。

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

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