Контроллер
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();
}
}
}
TempOAuthToken.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
метод для преобразования TempOAuthToken
модели в 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"])
Кажется, что-то ссылается на себя, создавая цикл. Может ли кто-нибудь помочь мне найти проблему?