1

Controller:-

@RestController
public class MyController {

private String url = "https://scoreboard-backend-dev.herokuapp.com/scoreBoard";

@Autowired
private RestTemplate restTemplate;

@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
    return builder.build();
}

@PostMapping("/add/score")
public ScoreBoard addScore(@RequestBody ScoreBoard scoreBoard){
    ResponseEntity<ScoreBoard> responseEntity =  restTemplate
                       .postForEntity(url+"/add/score", scoreBoard, ScoreBoard.class );
    return responseEntity.getBody();

}

Model:-

public class ScoreBoard {

    private long id;
    private String team;
    private int point;

    public ScoreBoard(){

    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getTeam() {
        return team;
    }

    public void setTeam(String team) {
        this.team = team;
    }

    public int getPoint() {
        return point;
    }

    public void setPoint(int point) {
        this.point = point;
    }
}

I tried to test it by Postman with the following body:- http://localhost:8080/add/score (POST)

{
    "id": 1,
    "team": "new team",
    "point": 3030
}

I got the following error:

2020-06-10 21:19:17.303 ERROR 19926 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.RestClientException: Error while extracting response for type [class com.example.resttemplatedemo.model.ScoreBoard] and content type [application/json]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unrecognized token 'ScoreBoard': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false'); nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'ScoreBoard': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')
 at [Source: (PushbackInputStream); line: 1, column: 12]] with root cause

com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'ScoreBoard': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')
 at [Source: (PushbackInputStream); line: 1, column: 12]
    at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1851) ~[jackson-core-2.11.0.jar:2.11.0]
Smile
  • 3,832
  • 3
  • 25
  • 39
masiboo
  • 4,537
  • 9
  • 75
  • 136
  • Use `String.class` and check what is exact response may be there is something else – Eklavya Jun 10 '20 at 18:54
  • The payload cannot be successfully deserialized to your object without getters/setters, DO NOT remove them! BTW, it is weird to see the verb `/add` in URI. (HTTP method POST already means to add or create resource) – LHCHIN Jun 11 '20 at 02:52
  • I have all public getter and setter and no-arg constructor. In actual code, just for this question, I remove from the question. – masiboo Jun 11 '20 at 05:52
  • I'd try to set a breakpoint in `JsonParser.java:1851` and dig around in the stack to see what value (String) is actually send to your server. Write a UT with this exact value and see what happens. – dpr Jun 12 '20 at 05:48

1 Answers1

0

In order to deserialize your JSON HTTP request body into your class, ScoreBoard must have a constructor without arguments

Fabien
  • 974
  • 9
  • 14
  • I have all public getter and setter and no-arg constructor. In actual code, just for this question, I remove from the question. – masiboo Jun 11 '20 at 05:51