作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在我的一个类中添加了一个枚举类型和变量。编写一个使用此类的 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);
最佳答案
如果您没有 Condo
作为输入参数,请检查您的 Controller 代码。从日志看来是这样。 Jackson 无法反序列化 Condo
的 HTTP 表示,因为它没有默认构造函数。
关于java - jackson 枚举反序列化。 Spring rest模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40647348/
我是一名优秀的程序员,十分优秀!