@NotNull 注释未按预期工作 Java Jersey [英] @NotNull annotation not working as expected Java Jersey

查看:28
本文介绍了@NotNull 注释未按预期工作 Java Jersey的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个端点实现,我在参数列表中将一个对象传递给它.我正在尝试使用 @NotNull 注释验证此对象不为空.

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(); 
} 

如果验证对象不为空,则端点将仅返回 200 OK 响应.但是,当我使用指定的路径向此端点发出请求时,正文中没有任何内容,不会引发任何错误.相反,我能够检索 200 响应(即使我在返回响应之前检查对象是否为空,它也表明情况确实如此).

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?

推荐答案

如果你对类路径 (org.glassfish.jersey.ext:jersey-bean-validation:2.22.2) 有 bean-validation 依赖,它应该被选中通过框架并开箱即用.

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.

还要确保你的@NotNull 注释来自 javax.validation.constraints 包

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

这篇关于@NotNull 注释未按预期工作 Java Jersey的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆