我有一个rest
在tomcat
.
回复时 - 类型的对象ActionError
作为json
字符串发送给客户端。
ActionError
将传递给客户
Array
Strings
2个Array
的FieldError
_- 任意
Object
有效负载(定义为泛型类型)。
在我的测试中,我作为payload
类型的对象传递Employee
。
通过上述安排 - 一切正常。我得到的结果如下:
{"actionErrors":["everything is ok"],"fieldErrors":null,"payload":{"empName":"Jony","empNo":"E3","position":"Manager"}}
但是,如果我删除Array
of FieldError
( private ArrayList<FieldError> fieldErrors;
) 部分,那么 -现在 ActionError
只包含:
1.Array
的Strings
2.Object
payload
然后奇怪的payload
是没有正确序列化,我得到:
{"actionErrors":["everything is ok"],"payload":"com.mycompany.mavenwebproject.model.Employee@41a8e0d9"}
是什么原因造成的?关于如何修复它的任何想法?
谢谢。
以下是类定义:
动作错误.java
@XmlRootElement(name = "actionError")
@XmlAccessorType(XmlAccessType.FIELD)
public class ActionError<T> {
private ArrayList<String> actionErrors;
private ArrayList<FieldError> fieldErrors;
private T payload;
/**
* @return the actionErrors
*/
public ArrayList<String> getActionErrors() {
return actionErrors;
}
/**
* @param actionErrors the actionErrors to set
*/
public void setActionErrors(ArrayList<String> actionErrors) {
this.actionErrors = actionErrors;
}
/**
* @return the fieldErrors
*/
public ArrayList<FieldError> getFieldErrors() {
return fieldErrors;
}
/**
* @param fieldErrors the fieldErrors to set
*/
public void setFieldErrors(ArrayList<FieldError> fieldErrors) {
this.fieldErrors = fieldErrors;
}
public void addFieldError(String fieldName,String errorMessage){
if (this.fieldErrors==null)
this.fieldErrors = new ArrayList<>();
this.fieldErrors.add(new FieldError(fieldName, errorMessage));
}
public void addActionError(String errorMessage){
if (this.actionErrors==null)
this.actionErrors = new ArrayList<>();
this.actionErrors.add(errorMessage);
}
public boolean hasErrors(){
return (fieldErrors!=null && !fieldErrors.isEmpty()) || (actionErrors!=null && !actionErrors.isEmpty());
}
/**
* @return the payload
*/
public T getPayload() {
return payload;
}
/**
* @param payload the payload to set
* @return
*/
public ActionError setPayload(T payload) { //Class<T> payloadClass
this.payload = payload;
return this;
}
}
FieldError.java
@XmlRootElement(name = "fieldError")
@XmlAccessorType(XmlAccessType.FIELD)
public class FieldError {
private String fieldName;
private String fieldError;
/**
* @return the fieldName
*/
public String getFieldName() {
return fieldName;
}
/**
* @param fieldName the fieldName to set
*/
public void setFieldName(String fieldName) {
this.fieldName = fieldName;
}
/**
* @return the fieldError
*/
public String getFieldError() {
return fieldError;
}
/**
* @param fieldError the fieldError to set
*/
public void setFieldError(String fieldError) {
this.fieldError = fieldError;
}
public FieldError(String fieldName, String fieldError) {
this.fieldName = fieldName;
this.fieldError = fieldError;
}
}
雇员.java
@XmlRootElement(name = "employee")
@XmlAccessorType(XmlAccessType.FIELD)
public class Employee {
private String empNo;
private String empName;
private String position;
// This default constructor is required if there are other constructors.
public Employee() {
}
public Employee(String empNo, String empName, String position) {
this.empNo = empNo;
this.empName = empName;
this.position = position;
}
public String getEmpNo() {
return empNo;
}
public void setEmpNo(String empNo) {
this.empNo = empNo;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
}