Когда я попытался преобразовать следующий экземпляр класса в JSON (используя Джексона)
public class RPCRespond<Result> {
private int code;
private Result result;
private boolean success;
private String failureReason;
public RPCRespond() {
this.code = 0;
this.success = true;
this.result = null;
}
public RPCRespond(Result result) {
this.code = 0;
this.success = true;
this.result = result;
}
public RPCRespond(int code, String failureReason) {
this.code = code;
this.success = false;
this.failureReason = failureReason;
}
public RPCRespond(int code, String failureReason, Object... args) {
this.code = code;
this.success = false;
this.failureReason = String.format(failureReason, args);
}
public Result getResult() {
return result;
}
public void setSuccess(boolean success) {
this.success = success;
}
public String getFailureReason() {
return failureReason;
}
public void setFailureReason(String failureReason) {
this.failureReason = failureReason;
}
public int getCode() {
return code;
}
public boolean getSuccess() {
return success;
}
@Transient
public String getAsJSON() {
String json = "";
ObjectMapper mapper = new ObjectMapper();
json = mapper.writeValueAsString(this) ;
return json ;
}
}
, он попал в бесконечный цикл:
at sun.reflect.GeneratedMethodAccessor48.invoke(Unknown Source) at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601) at
org.codehaus.jackson.map.ser.BeanPropertyWriter.get(BeanPropertyWriter.java:483)
at
org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:418)
at
org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150)
at
org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112)
at
org.codehaus.jackson.map.ser.StdSerializerProvider._serializeValue(StdSerializerProvider.java:610)
at
org.codehaus.jackson.map.ser.StdSerializerProvider.serializeValue(StdSerializerProvider.java:256)
at
org.codehaus.jackson.map.ObjectMapper._configAndWriteValue(ObjectMapper.java:2566)
at
org.codehaus.jackson.map.ObjectMapper.writeValueAsString(ObjectMapper.java:2088)
Инициирование RPCRespond сделано
User u = new User() ;
u.setFirstName("aaaa") ;
RPCRespond<User> result = new RPCRespond<User>(u) ;
result.setSuccess(true) ;
return result.getAsJSON() ;
Как я могу преобразовать RPCRespond в JSON?