gpt4 book ai didi

java - jackson 枚举反序列化。 Spring rest模板

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:13:14 25 4
gpt4 key购买 nike

我在我的一个类中添加了一个枚举类型和变量。编写一个使用此类的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com