[英]How can I return an object from a POST method with Spring Boot?
I'm trying to create a communication between two services with Spring Boot, and I'm failing to return a custom object from a POST.我正在尝试使用 Spring Boot 在两个服务之间创建通信,但我无法从 POST 返回自定义 object。
I have tried something like我尝试过类似的东西
1st service:第一项服务:
@RestController
@RequestMapping("/broker")
class HostController{
@PostMapping(value = "/bid")
public Bid bid(@RequestBody Auction auction){
return new Bid(new URI("http://url:8080"));
}
}
2nd service:服务二:
ResponseEntity<Bid> response = rest.postForEntity(hostURL + "/bid", auction, Bid.class);
response.getBody();
The problem is I get an error message saying "cannot deserialize from Object value" for the Bid class, which makes me think the Auction is being sent but the Bid isn't sent back.问题是我收到一条错误消息,提示“无法从 Object 值反序列化”对于 Bid class,这让我认为正在发送拍卖,但没有发回 Bid。
I'm also not sure how the serialization happens for the auction, as I only put the "@RequestBody" and it started working.我也不确定拍卖的序列化是如何发生的,因为我只输入了“@RequestBody”,它就开始工作了。 The Auction class even has a Bid object inside, but that doesn't seem to be a problem.拍卖 class 甚至里面有一个出价 object,但这似乎不是问题。
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; }
}
The full stack trace is gigantic, but the part I think is relevant is this:完整的堆栈跟踪是巨大的,但我认为相关的部分是:
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"}"
Please try with adding @ResponseBody
annotation to your Controller method bid
.请尝试在您的 Controller 方法bid
中添加@ResponseBody
注释。 After doing this your HTTP response will have body with JSON representation of your object so it should be possible to serialize that back in your postForEntity.完成此操作后,您的 HTTP 响应将具有 JSON 表示的正文,表示您的 object,因此应该可以将其序列化回您的 postForEntity。
@PostMapping(value = "/bid")
@ResponseBody
public Bid bid(@RequestBody Auction auction){
return new Bid();
}
For more information check below link:有关更多信息,请查看以下链接:
How does the Spring @ResponseBody annotation work? Spring @ResponseBody 注释如何工作?
When you transfer some stuff, make sure its serializable and also dont forget to add no arg constructor.当您传输一些东西时,请确保其可序列化并且不要忘记添加无 arg 构造函数。 This should work.这应该有效。
class Bid implements Serializable {
private URI host;
public Bid();
public URI getHost() {
return host;
}
public void setHost(URI host) {
this.host = host;
}
}
I managed to make it work just by adding the default constructor to the Bid class, as discussed in the comments.正如评论中所讨论的,我设法通过将默认构造函数添加到 Bid class 来使其工作。
class Bid{
private URI host;
public Bid(){}
public Bid(URI host){ this.host = host; }
public URI getHost() { return host; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.