Java 由于循环引用,Spring模型到JsonObject的转换失败
控制器Java 由于循环引用,Spring模型到JsonObject的转换失败,java,spring,jackson,Java,Spring,Jackson,控制器 import com.google.gson.JsonObject; import javax.ws.rs.core.Response; @RestController @RequestMapping(value = "/auth") public class OAuthController { @Autowired private OAuthService oAuthService; @RequestMapping(value = "/authoriza
import com.google.gson.JsonObject;
import javax.ws.rs.core.Response;
@RestController
@RequestMapping(value = "/auth")
public class OAuthController {
@Autowired
private OAuthService oAuthService;
@RequestMapping(value = "/authorization-url", method = RequestMethod.GET)
public Response getAuthorizationUrl() {
try {
TempOAuthToken tempOAuthToken = oAuthService.getTemporaryOAuthToken();
return Response.status(Response.Status.OK).entity(tempOAuthToken.toJsonObject()).build();
} catch (Exception e) {
e.printStackTrace();
JsonObject errResponse = new JsonObject();
errResponse.addProperty("error", e.getMessage());
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errResponse).build();
}
}
}
tempouthToken.java
public class TempOAuthToken {
private String requestToken;
private String tokenSecret;
private String authorizationUrl;
private String verificationSecret;
public JsonObject toJsonObject() {
JsonObject json = new JsonObject();
json.addProperty("requestToken", this.requestToken);
json.addProperty("tokenSecret", this.tokenSecret);
json.addProperty("authorizationUrl", this.authorizationUrl);
return json;
}
}
我使用javax.ws.rs.core.Response
作为返回类型。因此,我可以返回包含任何内容的JsonObject。我编写了toJsonObject
方法来将tempouthToken
模型转换为JsonObject。我得到了以下错误:
HTTP Status 500 - Could not write content: Direct self-reference leading to cycle (through reference chain:
com.sun.jersey.core.spi.factory.ResponseImpl["entity"]-com.google.gson.JsonObject["asJsonObject"]);
nested exception is com.fasterxml.jackson.databind.JsonMappingException: Direct self-reference leading to cycle (through reference chain:
com.sun.jersey.core.spi.factory.ResponseImpl["entity"]-com.google.gson.JsonObject["asJsonObject"])
似乎有什么东西在自我参照,创造一个循环。有人能帮我找到问题吗