簡體   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