我使用带有WebFlux的Spring boot 2.0.1作为Rest服务器。
在我RestController我想自动反序列化一个对象(产品)。但是我得到一个杰克逊错误,好像参数名称模块没有注册。
原因:com.fasterxml.jackson.databind.exc.Invalid定义异常:无法构建com.truc.产品
的实例(没有像默认构造一样的Creator):无法从对象值(没有基于委托或属性的Creator)反序列化[Source: UNKNOWN; line:-1,列:-1]在com.fasterxml.jackson.databind.exc.Invalid定义xception.from(Invalid定义xception.java:67)在com.fasterxml.jackson.databind.DeseriazationContext.reportBad定义(DeseriazationContext.java:1451)在com.fasterxml.jackson.databind.DeseriazationContext.handleMisSingInstantiator(com.fasterxml.jackson.databind.deser.BeanDeseriazerBase.deserializeFromObjectUSingNonDefault(BeanDeseriazerBase.java:1290)com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:326)com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:159)在com. faerxml. jackson.数据库。ObjectReader._bind(ObjectReader. java: 1574),网址为com. fast erxml. jackson.数据库。ObjectReader. readValue(ObjectReader. java: 965)在org. springframework. http. codec. json。AbstractJackson2Decoder. lambda$decdeInternal0美元(AbstractJackson2Decoder. java: 113)...省略了287个常见帧
我的pom中有jackson模块参数名
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-parameter-names</artifactId>
</dependency>
这是RestController中的路径
@ResponseStatus(HttpStatus.CREATED)
@RequestMapping(method = RequestMethod.POST)
public Mono<Entity<Product>> postProduct(@RequestBody Product product) {
return productService.insert(product);
}
如果我尝试手动反序列化,它会起作用:
@Autowired
private ObjectMapper objectMapper;
...
@ResponseStatus(HttpStatus.CREATED)
@RequestMapping(method = RequestMethod.POST)
public Mono<Entity<Product>> postProduct(ServerWebExchange exchange) {
return exchange.getRequest().getBody()
.last().flatMap(buffer -> {
try {
Product product = objectMapper.readValue(buffer.asInputStream(), Product.class);
return productService.insert(product);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}
这是我的产品。班
package com.truc;
import java.time.LocalDate; import java.util.Optional;
public final class Product {
public static final String PREFIX = "PT";
public final String description;
public final String shortDescription;
public final String sku;
public final float weight;
public final LocalDate newSince;
public final LocalDate newUntil;
public final Status status;
public final Visibility visibility;
public final String metaKeywords;
public Product(String description, String shortDescription, String sku, Float weight,
LocalDate newSince, LocalDate newUntil, Status status, Visibility visibility, String metaKeywords) {
this.description = description;
this.shortDescription = shortDescription;
this.sku = sku;
this.weight = Optional.ofNullable(weight).orElse(0f);
this.newSince = newSince;
this.newUntil = newUntil;
this.status = status;
this.visibility = visibility;
this.metaKeywords = metaKeywords;
}
public enum Status {
ACTIVE, INACTIVE
}
public enum Visibility {
FULL, CATALOG, SEARCH, NONE
}
}
如果我明白了jackson-数据库2.9之后不需要@JsonCreator
,我使用jackson2.9.5
如果我添加@JsonCreator
我得到一个新的错误415不支持的内容类型'应用程序/json'
...
我不明白我错在哪里?
谢啦
您必须在RequestBody类中添加默认构造函数,即ProductClass,然后它才能工作
正如这里解释的那样,@EnableWebFlux
停用所有webflux自动配置。我在配置类中使用它,而不使用webfluxconfig
扩展类。
当我删除@EnableWebFlux
它再次工作时,ObjectMapper按预期配置。
没关系,谢谢