简体   繁体   English

杰克逊enum反序列化。 Spring restTemplate

[英]Jackson enum deserialization. Spring restTemplate

I'm added an enum type and variable to one of my classes. 我在我的一个类中添加了一个枚举类型和变量。 Have a spring boot tests written which are using this class. 编写一个使用此类的spring boot测试。

Once the enum was added, jacksonMapper isn't able to convert the class (for the restTemplate POST request). 一旦添加了枚举, jacksonMapper就无法转换类(用于restTemplate POST请求)。

Here is the error: 这是错误:

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]

the class: 班级:

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**
}

i also tried to use @JsonCreator, @jsonValue as it is pointed at some other SO threads. 我也尝试使用@JsonCreator, @jsonValue因为它指向其他一些SO线程。 Doesn't help, 没有帮助,

request: 请求:

Condo condo = new Condo(Condo.LocationType.CONDO);
** setting fields for condo object ** 

//CREATE
ResponseEntity<JsonResponse> responseEntity = restTemplate.postForEntity(controllerPath+"/add_active", condo, JsonResponse.class);

Check your controller code if you don't have Condo as input parameter. 如果您没有Condo作为输入参数,请检查您的控制器代码。 From the log it seems so. 从日志看来似乎如此。 Jackson cannot deserialize the HTTP representation of Condo because it does not have default constructor. Jackson无法反序列化Condo的HTTP表示,因为它没有默认构造函数。

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

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