球衣@NotNull没有检查输入

时间:2016-05-05 19:31:00

标签: jersey

我有一个泽西REST服务,我使用@NotNull来检查@pathparam,但它似乎无效。我在pom.xml中包含了jersey-bean-validation

我的代码在这里:

@Path("/resource")
public class MyResource extends AbstractResource {
    @POST
    @Path("/report")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ResponseClass(ReportAcknowledgement.class)
    public Response getGrowthResults(MyRequest request,
                                     @Context HttpHeaders headers) throws Exception {

        String organizationId = request.getOrganization(); <-- null here
        ..
        validateOrganization(organizationId);


public abstract class AbstractResource {
    ..
    protected void validateOrganization(@NotNull(message = "{org.string.null}")  @Valid String organizationId) throws Exception {
        ...
    } 

2 个答案:

答案 0 :(得分:0)

确保您使用的是jersey-bean-validation依赖项:

<dependency>
    <groupId>org.glassfish.jersey.ext</groupId>
    <artifactId>jersey-bean-validation</artifactId>
    <version>2.22.2</version>
</dependency>

根据documentation,具有上述类路径依赖性将激活Jersey上的Bean Validation功能。

答案 1 :(得分:0)

问题是超级老,所以你可能已经找到了答案......

但是你缺少参数的@Valid注释。

试试这个

@POST
@Path("/report")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@ResponseClass(ReportAcknowledgement.class)
public Response getGrowthResults(@javax.validation.Valid MyRequest request,
                                     @Context HttpHeaders headers) throws Exception {
        String organizationId = request.getOrganization(); <-- null here
        ..
        validateOrganization(organizationId);
}