Akila Supun
Akila Supun

Reputation: 31

How to limit fields that map in @RequestBody

I'm trying to implement a very basic Spring Boot web application. In that I map a JSON object to an entity (says Customer Entity) with the help of @RequestBody.

In addCustomer method, I want to bind/map only the firstName & lastName fields and ignore Id field even if the client response JSON has that field.

And in updateCustomer method I need to map all the fields including Id because I need Id field to update the entity.

How can I ignore some or one field in auto-mapping process of the @RequestBody.

@RestController
@RequestMapping("/customer-service")
public class CustomerController {
    @Autowired
    CustomerServiceImpl customerService; 

    //This method has to ignore "id" field in mapping to newCustomer
    @PostMapping(path = "/addCustomer")
    public void addCustomer(@RequestBody Customer newCustomer) {
        customerService.saveCustomer(newCustomer);
    }

    //This method has to include "id" field as well to updatedCustomer
    @PostMapping(path = "/updateCustomer")
    public void updateCustomer(@RequestBody Customer updatedCustomer) {
        customerService.updateCustomer(updatedCustomer);
    }
}
@Entity
@Table(name = "CUSTOMER")
public class Customer {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long cusId;

    private String firstName;
    private String lastName;

    //Default Constructor and getter-setter methods after here
}

Upvotes: 0

Views: 5367

Answers (2)

Maciej Walkowiak
Maciej Walkowiak

Reputation: 12932

You can use multiple @JsonViews to use different mappings in each method.

  1. Define views:
public class Views {
    public static class Create {
    }
}
  1. Select which fields should be used in each view:
@Entity
@Table(name = "CUSTOMER")
public class Customer {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long cusId;

    @JsonView(Views.Create.class)
    private String firstName;
    @JsonView(Views.Create.class)
    private String lastName;
    ...
}
  1. Tell Spring MVC to use this view in particular method:
@PostMapping(path = "/addCustomer")
public void addCustomer(@RequestBody @JsonView(Views.Create.class) Customer newCustomer) {
    customerService.saveCustomer(newCustomer);
}

Upvotes: 7

Black Diamond
Black Diamond

Reputation: 503

TL;DR: Use one of the following:

  • @JsonIgnoreProperties("fieldname") on your class.
  • @JsonIgnore on your field to ignore its mapping.

Example:

@Getter
@Setter
@JsonIgnoreProperties("custId")
public class Customer {

@JsonIgnore
private String custId;
private String firstName;
private String lastName;

}

BUT, as you've the same POJO, it will skip "custId" mapping for both the requests.

AFAIK, you should not be receiving custId value for your @PostMapping (adding customer) so it'll be dynamically set to null or default value. And while creating a user, you should also create ID for it or let database take care of it.

And for @PutMapping (updating user) you must be getting the ID value which can be used to identify user and then make an update.

Upvotes: 2

Related Questions