【发布时间】:2016-11-17 05:30:15
【问题描述】:
我在我的一个类中添加了一个枚举类型和变量。 编写一个使用此类的 Spring Boot 测试。
添加枚举后,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: java.io.PushbackInputStream@6393fabb; 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: java.io.PushbackInputStream@6393fabb; 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: java.io.PushbackInputStream@6393fabb; 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: java.io.PushbackInputStream@6393fabb; 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**
}
我还尝试使用@JsonCreator, @jsonValue,因为它指向其他一些 SO 线程。没用,
请求:
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'添加一个默认构造函数
-
什么是 JsonResponse?似乎问题更可能是由响应的反序列化引起的:“无法构造 com.springapp.models.common.Condo 的实例”,而不是在发送请求时。
标签: java json spring enums jackson