如何在Spring REST控制器上使用SerializableEntity?

时间:2017-10-09 17:25:47

标签: java spring serialization

所以我有一些包含以下POST请求的标准客户端代码:

...
httpPost.addHeader("content-type", "application/x-java-serialized-object");
SerializableEntity entity = new SerializableEntity(mySerialObject);
httpPost.setEntity(entity);
httpClient.execute(httpPost);
...

所以上面的代码看起来很好,做我想做的事。但是我在服务器端消耗我的SerializableEntity时遇到了麻烦。这是我的控制器的样子:

@PostMapping("/myEndpoint")
@ResponseBody
public <T extends Serializable & Comparable<T>> ResponseEntity<Integer> balancingPost(HttpResponse response) {
    try(InputStream inputStream = response.getEntity().getContent()) {
      MySerialObject<T> = (MySerialObject<T>) SerializationUtils.deserialize(inputStream);

       ... do business logic calls here...
      return new ResponseEntity<>(someResult, HttpStatus.OK);
    } catch(Exception e) {
       ... exception handling and such....
    }
}

所以我觉得HttpResponse是我控制器的错误类型的参数,但我不知道该使用什么。也许我的@ResponseBody应该是param中的@RequestBody?

0 个答案:

没有答案