【问题标题】:Jackson enum deserialization. Spring restTemplate杰克逊枚举反序列化。弹簧休息模板
【发布时间】: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


【解决方案1】:

如果您没有 Condo 作为输入参数,请检查您的控制器代码。从日志看来是这样。 Jackson 无法反序列化 Condo 的 HTTP 表示,因为它没有默认构造函数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-18
    • 2012-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-10
    • 1970-01-01
    相关资源
    最近更新 更多