【问题标题】:How can I return an object from a POST method with Spring Boot?如何使用 Spring Boot 从 POST 方法返回对象?
【发布时间】: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


【解决方案1】:

当你传输一些东西时,确保它是可序列化的,并且不要忘记添加没有 arg 构造函数。这应该可以。

   class Bid implements Serializable {
    
    private URI host;

    public Bid();

    public URI getHost() {
        return host;
    }

    public void setHost(URI host) {
        this.host = host;
    }
}

【讨论】:

    【解决方案2】:

    我设法通过将默认构造函数添加到 Bid 类来使其工作,如 cmets 中所述。

    class Bid{
        private URI host;
    
        public Bid(){}
        
        public Bid(URI host){ this.host = host; }
    
        public URI getHost() { return host; }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-18
      • 2018-06-19
      • 2021-12-18
      • 1970-01-01
      相关资源
      最近更新 更多