我使用Spring-boot 2.0.1和WebFlux作为Rest服务器。
在我的RestController中我想自动反序列化一个对象(Product)。但是我得到了Jackson错误,好像没有注册ParameterNamesModule。
引起: com.fasterxml.jackson.databind.exc.InvalidDefinitionException:不能 构造
com.truc.Product
的实例(没有创建者,就像默认 构造,存在):不能从Object值反序列化(没有委托 - 或者基于财产的创作者)[来源:UNKNOWN; line:-1,column:-1] 在 com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67) 在 com.fasterxml.jackson.databind.DeserializationContext.reportBadDefinition(DeserializationContext.java:1451) 在 com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1027) 在 com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1290) 在 com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:326) 在 com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:159) 在 com.fasterxml.jackson.databind.ObjectReader._bind(ObjectReader.java:1574) 在 com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:965) 在 org.springframework.http.codec.json.AbstractJackson2Decoder.lambda $ decodeInternal $ 0(AbstractJackson2Decoder.java:113) ...省略了287个常见帧
我的pom中有jackson-module-parameter-names
<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);
}
});
}
这是我的Product.class
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
}
}
如果我理解@JsonCreator
2.9之后不需要jackson-databind
,我会使用jackson 2.9.5
如果我添加@JsonCreator
,我会收到新错误415 Unsupported Content-Type 'application/json'
...
我不明白我哪里错了?
由于
答案 0 :(得分:0)
解释here @EnableWebFlux
停用所有webflux自动配置。我在Configuration类中使用它而不使用WebFluxConfigurer
扩展类。
当我删除@EnableWebFlux
它再次工作时,ObjectMapper按预期配置。
所以没关系 感谢
答案 1 :(得分:0)
您必须在RequestBody类(即ProductClass)中添加默认构造函数,然后它将起作用