열거 형과 변수를 제 클래스 중 하나에 추가했습니다. 이 클래스를 사용하는 스프링 부트 테스트가 작성되었습니다.Jackson enum deserialization. Spring restTemplate
열거 형을 추가하면 jacksonMapper
이 클래스를 변환 할 수 없습니다 (restTemplate
POST 요청의 경우).
2016-11-17 11:36:11.571 WARN 10000 --- [o-auto-1-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not construct instance of com.springapp.models.common.Condo: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)
at [Source: [email protected]; line: 1, column: 2]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.springapp.models.common.Condo: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)
at [Source: [email protected]; line: 1, column: 2]
2016-11-17 11:36:11.639 INFO 10000 --- [ main] c.s.controllers.api.CondoControllerTest : status: 400
2016-11-17 11:36:11.639 INFO 10000 --- [ main] c.s.controllers.api.CondoControllerTest : message: Could not read document: Can not construct instance of com.springapp.models.common.Condo: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)
at [Source: [email protected]; line: 1, column: 2]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.springapp.models.common.Condo: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)
at [Source: [email protected]; line: 1, column: 2]
클래스 : 나는 또한이 다른 SO 스레드에서 지적 된 바와 같이 @JsonCreator, @jsonValue
을 사용하려고
public class Condo {
**irrelevant fields**
public Condo(LocationType locationType) {
this.locationType = locationType;
}
public enum LocationType {
CONDO("condo"), MALL("mall"), STATION("station");
private String value;
private LocationType(String value) {
this.value = value;
}
public String stringValue() {
return this.value;
}
}
public LocationType getLocationType() {
return locationType;
}
LocationType locationType;
** getters/setters**
}
여기
는 오류입니다. 당신이 입력 매개 변수로Condo
이없는 경우
Condo condo = new Condo(Condo.LocationType.CONDO);
** setting fields for condo object **
//CREATE
ResponseEntity<JsonResponse> responseEntity = restTemplate.postForEntity(controllerPath+"/add_active", condo, JsonResponse.class);
mark ==> public static enum LocationType {..} 'Condo'에 대한 기본 생성자를 추가하십시오. – kuhajeyan
그리고 JsonResponse 란 무엇입니까? 문제의 응답이 deserialization으로 인해 발생했을 가능성이 큽니다 : 요청을 보낼 때가 아니라 'com.springapp.models.common.Condo'인스턴스를 생성 할 수 없습니다. –