在控制器中接受请求时,发生json解析错误

时间:2018-08-17 11:16:27

标签: java spring-boot annotations

实际上我有一个POJO,它包含Integer变量,但是从请求(如果我发送String),它将在进入控制器之前引发Json解析异常。

我的控制器是:

@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";
}

如果我如上所述发送不正确的请求,那么如何使用异常处理程序以正确的消息来捕获它。

0 个答案:

没有答案