2016-12-17 77 views
0

不知道爲什麼,但我的Spring Boot web/REST應用程序中的編組忽略javax.xml.bind.annotation.*註釋。爲什麼Spring Boot不能正確編碼XML /忽略XML javax.xml.bind.annotation。*?

ValidationErrorResponse應該編組爲Errors XML字段。

@XmlRootElement(name = "Errors") 
@XmlAccessorType(XmlAccessType.FIELD) 
@XmlSeeAlso(ValidationError.class) 
public class ValidationErrorResponse { 

    @XmlElementWrapper(name = "Errors") 
    private List<ValidationError> errors = new ArrayList<ValidationError>(); 

    @XmlAttribute(name = "count") 
    public int getCount() { 
     return this.errors.size(); 
    } 

    public void addError(ValidationError error) { 
     this.errors.add(error); 
    } 

    @XmlElement(name = "Errors") 
    public List<ValidationError> getErrors() { 
     return errors; 
    } 
} 

ValidationErrorErrors現場編組到Error XML子。

import javax.xml.bind.annotation.*; 

    @XmlRootElement(name = "Error") 
    @XmlAccessorType(XmlAccessType.FIELD) 
    public class ValidationError { 

     @XmlAttribute(name = "field") 
     private final String field; 

     @XmlAttribute(name = "message") 
     private String message; 

     public ValidationError(String field) { 
      this.field = field; 
     } 

     public void setMessage(String message) { 
      this.message = message; 
     } 

     public String getField() { 
      return field; 
     } 

     public String getMessage() { 
      return message; 
     } 
    } 

但它返回的XML字符串

<ValidationErrorResponse> 
    <errors> 
     <errors> 
      <field>transaction</field> 
      <message>message...</message> 
     </errors> 
     <errors> 
      <field>transaction</field> 
      <message>message...</message> 
     </errors> 
     <errors> 
      <field>transaction</field> 
      <message>message...</message> 
     </errors> 
    </errors> 
    <count>3</count> 
</ValidationErrorResponse> 

,並應

<Errors count=3> 
<Error field="..." message="...."> 
<Error field="..." message="...."> 
<Error field="..." message="...."> 
</Errors> 

我在做什麼錯?

回答

0

問題對我的類路徑依賴jackson-dataformat-xml

<dependency> 
      <groupId>com.fasterxml.jackson.dataformat</groupId> 
      <artifactId>jackson-dataformat-xml</artifactId> 
      <version>2.8.5</version> 
     </dependency> 

當我把它關掉所有的作品如預期。