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;
}
}
ValidationError應Errors
現場編組到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>
我在做什麼錯?