春季启动消耗Web服务

我有一个这样构建的Web服务

@RestController
public class GreetingController {

    @PostMapping(path = "/greetingws")
    public Foo greeting(@RequestBody Foo dto) {
        return dto;
    }
}

当我使用网络服务时,我会这样

Foo f = new Foo("kkkkk");

ResponseEntity<String> t2 = restTemplate
          .exchange("http://localhost:8080/greetingws", HttpMethod.POST, new HttpEntity<Foo>(f), String.class);

但它返回错误

org.springframework.web.client.HttpClientErrorException$BadRequest: 400 : [{"timestamp":"2020-04-24T13:03:30.191+0000","status":400,"error":"Bad Request","message":"JSON parse error: Cannot construct instance of it.test.demo.controller.Foo (although at least one C... (7991 bytes)]

我在哪里出错?

谢谢

评论
  • 占领网吧
    占领网吧 回复

    Ciao Romeo :-)

    似乎您缺少Foo中的默认构造函数,而Spring无法反序列化请求。 加:

    public Foo() {
    }