spring-boot - Spring Boot 休息请求数据类型验证
问题描述
我正在对 Spring Boot Rest 中的请求对象进行验证。我必须验证请求的数据类型。该请求具有多个布尔值,并尝试验证是否为布尔数据类型传递了字符串。我在我的 ControllerAdvice 类中处理了 HttpMessageNotReadableException 并发送了错误消息列表。但在我的回应中,只有第一个字段抛出异常。如果有线索,请帮忙。
解决方案
@Vishnu Dubey 使用这个.....
@RestControllerAdvice
public class ServiceControllerAdvice {
private static final Logger log = LoggerFactory.getLogger(ServiceControllerAdvice.class);
@ExceptionHandler(value = { ConstraintViolationException.class })
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
public ServiceResponse<?> constraintViolationException(final ConstraintViolationException ex) {
log.error("Validation failed", ex);
final ServiceResponse<?> response = new ServiceResponse<>(-1);
final Error error = new Error();
error.setCode("PS01");
error.setContext(ex);
error.setMessage(ex.getMessage());
response.setError(error);
return response;
}
}
推荐阅读
- python - 盈透证券 accountSummary() 更改价值货币
- php - 如何从数组中提取百分比
- python - 使用分类列根据数组对数据框进行排序
- javascript - 如何获得点击 Razorpay 支付失败按钮的失败响应?
- google-cloud-platform - BigQuery - 预定查询更新通知电子邮件
- javascript - D3.js 折线图 - 将网格添加到背景并制作折线
- react-native - App() 的重新渲染会在导航中自动重新渲染屏幕吗?
- firebase - 每次我使用futurebuilder登录时从firestore中检索用户名并输入appbar标题
- python - ModuleNotFoundError:RStudio 中没有名为“pandas”的模块
- sql - 使用变量作为表名动态sql不工作选择到表中