Reputation:
I am new to Spring Boot Rest and trying to use post ab object as a rest request. But it is not working and I am getting null return:
This is my Object that I want to post:
public class Flight {
private String code;
private Route route;
private LocalDateTime departure;
private LocalDateTime arrival;
//Constructor
//Getters and Setters
}
This is Route class:
public class Route {
private final Airport from;
private final Airport to;
//Constructor
//Getters
}
This is my Airport class:
public class Airport {
private final String code;
private final String name;
//Constructor
//Getters
}
This is my controller:
@RequestMapping(value = "/testflight", method = RequestMethod.POST)
public Flight createFlight(@RequestBody Flight inputPayload) {
Flight response = inputPayload;
response.setCode(inputPayload.getCode());
response.setRoute(inputPayload.getRoute());
response.setDeparture(inputPayload.getDeparture());
response.setArrival(inputPayload.getArrival());
System.out.println("Flight details are:" + "\n" + inputPayload.getCode() + "\n" + inputPayload.getRoute() + "\n" + inputPayload.getDeparture() + "\n" + inputPayload.getArrival());
return response;
}
And this is my JSON:
{ "flight": {
"code" : "THY1770",
"route": {
"airport": [
{ "code": 1, "name": "IST" },
{ "code": 2, "name": "STG" }
],
"departure" : "2014-01-01",
"arrival" : "2014-01-01"
}
}
}
When I post this JSON then I just get this:
Flight details are: null null null null
Upvotes: 1
Views: 2053
Reputation: 1410
As the controller is posted only partially (that is: only the method, not the full class), I will assume that it's as simple as it gets and that it has been annotated as a @RestController.
It would appear that you have several discrepancies between the objects and the JSON payload you're passing.
An easy way to generate a JSON that your controller will accept is to expose a method in the controller that takes no parameter as input and generates a Flight as output. That output you can then use as a template to build your subsequent JSON requests.
Try posting the following JSON:
{
"code" : "THY1770",
"route": {
"from": {
"code": "ABC",
"name": "My Airport 1"
},
"to": {
"code": "DEF",
"name": "My Airport 2"
}
}
"departure" : null,
"arrival" : null
}
Note that in my answer I've set the dates to null. For those you'll have to make sure that the format you provided is accepted. Use the trick I explained at the beginning to try and figure out one of the possible date formats.
Also, remember that dates are a very difficult problem to tackle. In your specific case, you'll probably want to make sure that dates are sent along with their time and timezone.
Also, help yourself with Project Lombok for the getters and setters when you get the time. It will save you lots of boilerplate code, which in the case of getters and setters is all too often the source of unexpected problems.
Upvotes: 1
Reputation: 42431
The code inside the controller itself is probably wrong:
It doesn't make sense to copy the reference of inputPayload
and then call the setter on the same object:
I think, given the toString
implemented in Flight
class you can:
@PostMapping("/testflight") // this is just a better syntax than @RequestMapping
public Flight createFlight(@RequestBody Flight inputPayload) {
System.out.println(inputPayload); // just to test yourself
return inputPayload;
}
Now having said that, all the mappings are done correctly and the annotations are also put as required so if it doesn't work, the error might be somewhere else (not posted in the question).
Upvotes: 0