【发布时间】:2022-01-01 08:42:24
【问题描述】:
我正在尝试使用 Spring Boot 在两个服务之间创建通信,但我无法从 POST 返回自定义对象。
我尝试过类似的东西
第一次服务:
@RestController
@RequestMapping("/broker")
class HostController{
@PostMapping(value = "/bid")
public Bid bid(@RequestBody Auction auction){
return new Bid(new URI("http://url:8080"));
}
}
第二次服务:
ResponseEntity<Bid> response = rest.postForEntity(hostURL + "/bid", auction, Bid.class);
response.getBody();
问题是我收到一条错误消息,指出 Bid 类的“无法从对象值反序列化”,这让我认为正在发送拍卖,但未发回投标。
我也不确定拍卖的序列化是如何发生的,因为我只输入了“@RequestBody”,它就开始工作了。 Auction 类内部甚至还有一个 Bid 对象,但这似乎不是问题。
class Bid{
private URI host;
public Bid(URI host){ this.host = host; }
public URI getHost() { return host; }
}
class Auction{
URI host;
private Bid winner; //Not defined when the problem happens
public Auction(URI host){ this.host = host; }
public URI getHost(){ return host; }
}
完整的堆栈跟踪是巨大的,但我认为相关的部分是:
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.gustavovbs.microservicesoffloading.Bid` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)\n at [Source: (PushbackInputStream); line: 1, column: 2]\n\tat
com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67)\n\tat
com.fasterxml.jackson.databind.DeserializationContext.reportBadDefinition(DeserializationContext.java:1764)\n\tat
com.fasterxml.jackson.databind.DatabindContext.reportBadDefinition(DatabindContext.java:400)\n\tat
com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1209)\n\tat
com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1415)\n\tat
com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:362)\n\tat
com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:195)\n\tat
com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:322)\n\tat
com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4593)\n\tat
com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3601)\n\tat
org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:378)\n\t... 63 more\n","message":"Type definition error: [simple type, class com.gustavovbs.microservicesoffloading.Bid];
nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.gustavovbs.microservicesoffloading.Bid` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)\n at [Source: (PushbackInputStream); line: 1, column: 2]","path":"/broker/broadcast"}"
【问题讨论】:
-
请edit 问题,添加整个
@Controller和完整的堆栈跟踪。如果可能,请提供minimal reproducible example(例如作为 git 存储库)。 -
确保
Bid是一个可序列化的类,并且它具有适当的getter 和setter -
不清楚您期望什么序列化。亚美尔? JSON? XML?二进制?
-
@RazvanFulea 该类不必
implements Serializable。如果缺少 getter,我们至少应该收到一个空 JSON,但不是异常。 -
我用更多的代码编辑了这个问题。我不完全理解我必须以正确的方式自动序列化对象,但它与拍卖类没有太大区别。
标签: java spring-boot rest microservices