2016-11-17 12 views
1

私はクラスの1つに列挙型と変数を追加しました。 このクラスを使用しているスプリングブートテストを作成しました。Jackson enumの逆シリアル化。 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] 

クラス:

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** 
} 

私はまた、それがいくつかの他のSOスレッドで指摘されているよう@JsonCreator, @jsonValueを使用しようとしました。あなたが入力パラメータとして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とは何ですか?この問題は、レスポンスの非直列化によって発生する可能性が高いようです。「要求を送信するときではなく、com.springapp.models.common.Condoのインスタンスを構築できません。 –

答えて

1

は、あなたのコントローラのコードをチェックしてください:

要求、助けにはなりません。ログからそうそうだ。 Jacksonはデフォルトのコンストラクタを持たないため、CondoのHTTP表現を逆シリアル化できません。

+0

デフォルトのコンストラクタを追加して問題を解決しました – user1935987

関連する問題