@NotNull annotation not working as expected Java J

2019-02-14 21:01发布

I have an endpoint implementation, that I am passing an object to in the parameter list. I am trying to verify that this object is not null, using the @NotNull annotation.

@POST 
@Path("path") 
public Response endpointMethod(@NotNull @ApiParam(value = "abc", required = true) Object object) { 
        return Response.status(Status.OK).build(); 
} 

If the object is verified to be not null, then the endpoint will just return a 200 OK response. However, when I fire a request to this endpoint, with the specified path, and nothing in the body, there are no errors that are thrown. Instead, I am able to retrieve the 200 response (even when I check if the object is null before return the response, it shows that that is the case).

Can someone guide me on how to verify whether the object is null in the correct way?

标签: java api rest http
2条回答
Melony?
2楼-- · 2019-02-14 21:39

Not sure about the @ApiParam (from swagger?), but the following works for me with Jersey 2.22:

@GET
@Path("/hz-test")
public Response proc(
            @NotNull @QueryParam("hz") String myvalue) {

I suspect also that you would need to use the @NotNull with one of the other Jersey parameter annotations (e.g. @QueryParam, @HeaderParam, etc.).

BTW, what version of jersey are you using ? (@NotNull is supported only in > 2.x AFAIK)

查看更多
来,给爷笑一个
3楼-- · 2019-02-14 21:51

If you have bean-validation dependency on classpath (org.glassfish.jersey.ext:jersey-bean-validation:2.22.2) it should be picked up by the framework and work out of the box.

Also make sure that your @NotNull annotation comes from javax.validation.constraints package

查看更多
登录 后发表回答