How to map JSON field names to Java object field names

3.9k Views Asked by At

Currently, I'm creating REST Service with Spring.

My request handler in @RestController:

@RequestMapping(value = "employees/", method = RequestMethod.POST)
public Response setEmployees(@RequestBody Employee employee) {
    Response response = employeeManager.setEmployee(employee);
    return response;
}

Employee has fields like: login,tabNumber,firstName etc.

The real problem is that my REST service customers want to send request with another field names, that does not corresponds to Java naming conventions. Like TABNUMBER, UNITS_NAME etc. Jackson API converts JSON data to Java object corresponding to it field names... How to solve it? How can I bind custom JSON field names to my Java object field names?


1

There are 1 best solutions below

0
On BEST ANSWER

You can use JsonProperty annotation as below so your clients can send request field name as FIRST_NAME and it can mapped to Employee class :

@Data // comes from lombok
class Employee {
    @JsonProperty("FIRST_NAME")
    private String firstName;

//other fields
}