gpt4 book ai didi

java - @RequestBody 对象不会自动反序列化

转载 作者:太空宇宙 更新时间:2023-11-04 10:32:37 26 4
gpt4 key购买 nike

我使用 Spring-boot 2.0.1 和 WebFlux 作为 Rest 服务器。

在我的 RestController 中,我想自动反序列化一个对象(产品)。但我收到 Jackson 错误,就好像 ParameterNamesModule 未注册一样。

Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of com.truc.Product (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator) at [Source: UNKNOWN; line: -1, column: -1] at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67) at com.fasterxml.jackson.databind.DeserializationContext.reportBadDefinition(DeserializationContext.java:1451) at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1027) at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1290) at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:326) at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:159) at com.fasterxml.jackson.databind.ObjectReader._bind(ObjectReader.java:1574) at com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:965) at org.springframework.http.codec.json.AbstractJackson2Decoder.lambda$decodeInternal$0(AbstractJackson2Decoder.java:113) ... 287 common frames omitted

我的 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);
}
});
}

这是我的产品类

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

如果我了解 @JsonCreatorjackson-databind 2.9 之后不再需要,我会使用 jackson 2.9.5

如果我添加@JsonCreator,我会收到一个新错误415 Unsupported Content-Type 'application/json' ...

我不明白我错在哪里?

谢谢

最佳答案

您必须在 RequestBody 类(即 ProductClass)中添加默认构造函数,然后它才能工作

关于java - @RequestBody 对象不会自动反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49831458/

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