小编Aki*_*pun的帖子

如何限制@RequestBody中映射的字段

我正在尝试实现一个非常基本的 Spring Boot Web 应用程序。我在@RequestBody.

addCustomer方法中,我只想绑定/映射firstNamelastName字段并忽略Id字段,即使客户端响应JSON 具有该字段也是如此。

updateCustomer方法中,我需要映射包括Id在内的所有字段,因为我需要Id字段来更新实体。

我怎样才能忽略@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);
    }
} …
Run Code Online (Sandbox Code Playgroud)

jackson spring-boot spring-restcontroller

0
推荐指数
1
解决办法
4771
查看次数