Spring Boot Automatic JSON to Object at Controller
Spring boot comes with Jackson out-of-the-box which will take care of un-marshaling JSON request body to Java objects
You can use @RequestBody Spring MVC annotation to deserialize/un-marshall JSON string to Java object... For example.
Example
@RestController
public class CustomerController {
//@Autowired CustomerService customerService;
@RequestMapping(path="/customers", method= RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
public Customer postCustomer(@RequestBody Customer customer){
//return customerService.createCustomer(customer);
}
}
Annotate your entities member elements with @JsonProperty with corresponding json field names.
public class Customer {
@JsonProperty("customer_id")
private long customerId;
@JsonProperty("first_name")
private String firstName;
@JsonProperty("last_name")
private String lastName;
@JsonProperty("town")
private String town;
}