Jersey: Returning 400 error instead of 500 when given invalid request body

前端 未结 4 1931
予麋鹿
予麋鹿 2021-02-18 15:24

I\'m using Jersey\'s integrated Jackson processing to transform incoming JSON to a POJO, e.g.:

@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response newCus         


        
4条回答
  •  隐瞒了意图╮
    2021-02-18 16:05

    I've had this same problem... Unfortunately, there's no good way that I know of to intercept the Jackson exception and generate your own error code.

    One option you have is to use @JsonIgnoreProperties and then strictly validate the deserialized object. This won't tell you if your sender transmitted junk, but if they missed required fields, you'll catch that.

    I cannot find any way to access the actual JSON passed in, other than creating an @Provider class to trap the JSON, validate it, then pass it to Jackson for deserialization.

提交回复
热议问题