簡體   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