私はクラスの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);
mark ==> public static enum LocationType {..} 'Condo'のデフォルトコンストラクタを追加します – kuhajeyan
そしてJsonResponseとは何ですか?この問題は、レスポンスの非直列化によって発生する可能性が高いようです。「要求を送信するときではなく、com.springapp.models.common.Condoのインスタンスを構築できません。 –