首页 > 解决方案 > 在 Spring Controller 中捕获无效参数

问题描述

如果我们有这样的控制器动作

@GetMapping("/oauth2/token")
public ResponseEntity<Boolean> isTokenValid(@RequestParam("token") String token) {
//... code
}

那么我该如何处理用户未提交令牌时发生的异常。


当我有以下控制器动作时

@PostMapping("/account/{id}")
public ResponseEntity<String> updateAccount(@PathVariable("id") long id, @RequestBody AccountDTO dto) {

当无法解析 DTO 时,我该如何处理异常,并且 Jackson 在尝试从中提取 DTO 时是否验证数据?

标签: javaspringspring-boot

解决方案


推荐阅读