我正在使用Spring开发RESTful应用。我想处理在POST请求正文中数据类型错误时的情况

时间:2019-05-08 09:49:31

标签: spring web-services

我正在使用Spring Boot制作Web服务,其中之一从中获取对象:

public class GroupRouteRequestDTO {
    private Long groupID;
    private String userToken;
    private Long pageIndex; 

    private Long pageSize;
    private String search;
    }

课程

在邮递员中,我向身体提出请求

{
    "groupID":"11AA",
    "userToken": "9a",
    "pageIndex":0,
    "pageSize":12,
    "search":"A"

}

我知道

{
    "timestamp": 1557340656686,
    "status": 400,
    "error": "Bad Request",
    "exception": "org.springframework.http.converter.HttpMessageNotReadableException",
    "message": "Could not read document: Can not deserialize value of type java.lang.Long from String \"11AA\": not a valid Long value\n at [Source: java.io.PushbackInputStream@1226796e; line: 2, column: 12] (through reference chain: com.ntgclarity.ngnts.datatransferobject.GroupRouteRequestDTO[\"groupID\"]); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not deserialize value of type java.lang.Long from String \"11AA\": not a valid Long value\n at [Source: java.io.PushbackInputStream@1226796e; line: 2, column: 12] (through reference chain: com.ntgclarity.ngnts.datatransferobject.GroupRouteRequestDTO[\"groupID\"])",
    "path": "/toDoList/Employee"
} 

邮递员的回复

Web服务是

@PostMapping
    @RequestMapping("/Employee")
    @ResponseStatus(HttpStatus.CREATED)
    @PreAuthorize("hasAuthority('ToDoList_Access')")
    public Object getEmployeesRoutList(@Valid @RequestBody GroupRouteRequestDTO groupRouteRequest,HttpServletRequest request)
            throws EntityNotFoundException {
        return toDoListService.getEmployeesRoutList(groupRouteRequest,request);
    } 

问题:我可以从Web服务中自定义错误消息,以便在请求正文具有错误的数据类型时进行处理吗?

2 个答案:

答案 0 :(得分:0)

我通过在控制器类中添加此方法来解决此问题

    @ResponseBody
    public ResponseEntity<Object> MessageNotReadableException(HttpMessageNotReadableException ex,HttpServletResponse response){
        ex.printStackTrace();
        return new ResponseEntity<Object>("Bad Request Please Check Your Inputs",HttpStatus.BAD_REQUEST);
    }```

答案 1 :(得分:-1)

您可以使用Bean验证。请参阅link

例如:

import javax.validation.constraints.AssertTrue;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.validation.constraints.Email;

    public class User {

        @NotNull(message = "Name cannot be null")
        private String name;

        @AssertTrue
        private boolean working;

        @Size(min = 10, max = 200, message 
          = "About Me must be between 10 and 200 characters")
        private String aboutMe;

        @Min(value = 18, message = "Age should not be less than 18")
        @Max(value = 150, message = "Age should not be greater than 150")
        private int age;

        @Email(message = "Email should be valid")
        private String email;

        // standard setters and getters 
    }
相关问题