![](/img/trans.png)
[英]How can i return a JSON object and a zipped folder simultaneously from on end point in spring boot
[英]How can I return an object from a POST method with Spring Boot?
我正在嘗試使用 Spring Boot 在兩個服務之間創建通信,但我無法從 POST 返回自定義 object。
我嘗試過類似的東西
第一項服務:
@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();
問題是我收到一條錯誤消息,提示“無法從 Object 值反序列化”對於 Bid class,這讓我認為正在發送拍賣,但沒有發回 Bid。
我也不確定拍賣的序列化是如何發生的,因為我只輸入了“@RequestBody”,它就開始工作了。 拍賣 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; }
}
完整的堆棧跟蹤是巨大的,但我認為相關的部分是:
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"}"
請嘗試在您的 Controller 方法bid
中添加@ResponseBody
注釋。 完成此操作后,您的 HTTP 響應將具有 JSON 表示的正文,表示您的 object,因此應該可以將其序列化回您的 postForEntity。
@PostMapping(value = "/bid")
@ResponseBody
public Bid bid(@RequestBody Auction auction){
return new Bid();
}
有關更多信息,請查看以下鏈接:
當您傳輸一些東西時,請確保其可序列化並且不要忘記添加無 arg 構造函數。 這應該有效。
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; }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.