我正在嘗試使用 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 類的“無法從物件值反序列化”,這讓我認為 Auction 正在發送,但 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"}"
uj5u.com熱心網友回復:
當你傳輸一些東西時,確保它是可序列化的,并且不要忘記添加任何 arg 建構式。這應該有效。
class Bid implements Serializable {
private URI host;
public Bid();
public URI getHost() {
return host;
}
public void setHost(URI host) {
this.host = host;
}
}
uj5u.com熱心網友回復:
正如評論中所討論的,我設法通過將默認建構式添加到 Bid 類來使其作業。
class Bid{
private URI host;
public Bid(){}
public Bid(URI host){ this.host = host; }
public URI getHost() { return host; }
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/364048.html
上一篇:KafkaClassisnotinthetrustedpackage錯誤
下一篇:Django如何超越創建日期
