ExceptionHandler类(带有@ControllerAdvice)扩展了BasicApiExceptionHandler类(HttpRequestMethodNotSupportedException,HttpMediaTypeNotSupportedException,BindException,Exception)。当字段的@Pattern无效时,如何返回带有错误消息的标准错误消息和错误消息?
public class GeneralRequest {
@Pattern(regexp = "(?i)(en|fr)")
private String lang;
//getter/setter
}
public class MyRequest extends GeneralRequest {
@Pattern(regexp = "^\\d{12}$")
private String contractID;
//getter/setter
}
和控制器中的端点:
@GetMapping("/contracts/contract")
public Response<DTOClass> getContract(@Valid MyRequest request) throws Exception {
return new Response<>(errorOK.toString(),
ResponseCode.MESSAGE, contractService.getData(reqest));
}
当lang参数空/空(不存在)时,我必须指定默认值“ en”。空可以是任何参数lang和contractID(默认值111-如果不存在该参数,则默认值为def.value。