繁体   English   中英

杰克逊enum反序列化。 Spring restTemplate

[英]Jackson enum deserialization. Spring restTemplate

我在我的一个类中添加了一个枚举类型和变量。 编写一个使用此类的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作为输入参数,请检查您的控制器代码。 从日志看来似乎如此。 Jackson无法反序列化Condo的HTTP表示,因为它没有默认构造函数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM