0
votes

I have a webservice constructed like this

@RestController
public class GreetingController {

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

When I consume the webservice I do like this

Foo f = new Foo("kkkkk");

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

But it returns the error:

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)]

My Foo class is like below:

public class Foo {

    private String nome;

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public Foo(String nome) {
        super();
        this.nome = nome;
    }

    @Override
    public String toString() {
        return "Foo [nome=" + nome + "]";
    }

}

Where I do the error?

1

1 Answers

1
votes

Ciao Romeo :-)

it looks like you are missing the default constructor in Foo and Spring can not deserialize the request.
Add:

public Foo() {
}