2016-11-17 2 views
1

열거 형과 변수를 제 클래스 중 하나에 추가했습니다. 이 클래스를 사용하는 스프링 부트 테스트가 작성되었습니다.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); 
+0

mark ==> public static enum LocationType {..} 'Condo'에 대한 기본 생성자를 추가하십시오. – kuhajeyan

+0

그리고 JsonResponse 란 무엇입니까? 문제의 응답이 deserialization으로 인해 발생했을 가능성이 큽니다 : 요청을 보낼 때가 아니라 'com.springapp.models.common.Condo'인스턴스를 생성 할 수 없습니다. –

답변

1

이 컨트롤러 코드를 확인 :

요청, 도움이되지 않습니다. 로그에서 그렇게 보인다. Jackson은 기본 생성자가 없으므로 Condo의 HTTP 표현을 역 직렬화 할 수 없습니다.

+0

기본 생성자를 추가하여 문제가 해결되었습니다. – user1935987

관련 문제