如何使用 Spring Boot return 来自 POST 方法的对象?
How can I return an object from a POST method with Spring Boot?
我正在尝试使用 Spring Boot 创建两个服务之间的通信,但我无法 return 来自 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();
问题是我收到一条错误消息,指出出价 class 的“无法从对象值反序列化”,这让我觉得拍卖正在发送,但出价没有发回。
我也不确定拍卖的序列化是如何发生的,因为我只输入了“@RequestBody”,它就开始工作了。 Auction class 甚至在里面有一个 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"}"
当你传输一些东西时,确保它是可序列化的并且不要忘记添加无参数构造函数。这应该有效。
class Bid implements Serializable {
private URI host;
public Bid();
public URI getHost() {
return host;
}
public void setHost(URI host) {
this.host = host;
}
}
我设法通过将默认构造函数添加到 Bid class 来使其工作,如评论中所述。
class Bid{
private URI host;
public Bid(){}
public Bid(URI host){ this.host = host; }
public URI getHost() { return host; }
}
我正在尝试使用 Spring Boot 创建两个服务之间的通信,但我无法 return 来自 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();
问题是我收到一条错误消息,指出出价 class 的“无法从对象值反序列化”,这让我觉得拍卖正在发送,但出价没有发回。
我也不确定拍卖的序列化是如何发生的,因为我只输入了“@RequestBody”,它就开始工作了。 Auction class 甚至在里面有一个 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"}"
当你传输一些东西时,确保它是可序列化的并且不要忘记添加无参数构造函数。这应该有效。
class Bid implements Serializable {
private URI host;
public Bid();
public URI getHost() {
return host;
}
public void setHost(URI host) {
this.host = host;
}
}
我设法通过将默认构造函数添加到 Bid class 来使其工作,如评论中所述。
class Bid{
private URI host;
public Bid(){}
public Bid(URI host){ this.host = host; }
public URI getHost() { return host; }
}