Java 在控制器中接收请求时发生json解析错误
实际上,我有一个POJO,它包含整数变量,但从请求中,如果我发送字符串,它会在进入控制器之前抛出Json解析异常 我的控制器是:Java 在控制器中接收请求时发生json解析错误,java,spring-boot,annotations,Java,Spring Boot,Annotations,实际上,我有一个POJO,它包含整数变量,但从请求中,如果我发送字符串,它会在进入控制器之前抛出Json解析异常 我的控制器是: @PostMapping("/employee/w4/information") @ApiImplicitParams({ @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", para
@PostMapping("/employee/w4/information")
@ApiImplicitParams({
@ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header")})
public ResponseEntity saveEmployeeW4Information(@RequestBody EmployeeW4InformationInputBean employeeW4InformationInputBean,@RequestParam("empId") Long empId) throws DataException {
try {
return buildResponse(employeeW4InformationService.saveEmployeeW4Information(employeeW4InformationInputBean,empId,getLoggedInUser()));
} catch (DataException e) {
return buildError(e);
}
}
请求POJO为:
public class EmployeeW4InformationInputBean {
private String firstName;
private String middleName;
private String lastName;
private String email;
private Long dateOfBirth;
private Boolean isFullAmount;
private Integer partial;
private Integer partialPercentage;
private Integer fedFlatAndRate;
private Integer fedAdditionalPercentageAmt;
}
在上面的POJO“Integer partialPercentage”和“Integer fedFlatAndRate”中,它们是整数,但在发送请求时,如果我以“String”的形式发送,则会抛出JSON解析错误:
正确的JSON请求示例:
{
"partialPercentage" = "23";
"fedFlatAndRate" = "33";
}
不正确的JSON请求示例:
{
"partialPercentage" = "stack";
"fedFlatAndRate" = "overflow";
}
如果我发送了如上所述的错误请求,如何用异常处理程序捕获正确的消息。为什么要在POJO中使用wraper classed。您可以定义int partialPercentage而不是整数partialPercentage。对于int/Integer类型,正确的Json对象应该是{“partialPercentage”=33}好的,但我想知道我们是否在“int”变量中给出了“String”值,如何在控制器级别捕获IOException。对于控制器中的异常处理,可以使用ControllerAdvice和ExceptionHandler。你可以参考这个链接-