Jersey bean验证器未在Spring / Jersey / Jetty envrionment中初始化

时间:2017-02-23 21:27:35

标签: spring rest jersey

我在Spring / Jetty / Jersey环境中尝试这个。在我的Jersey资源配置中,我确保设置正确的属性。我看到notNull是一个特殊情况,作为空字符串而不是null,但超过最大字符应该至少导致错误的请求异常正确?

发布超过10个字符的字符串会产生200.

Business Objects

public class TestRequest {

    private TestBo businessObject;

    public TestBo getBusinessObject() {
        return businessObject;
    }

    public void setBusinessObject(TestBo businessObject) {
        this.businessObject = businessObject;
    }

}



public class TestBo {

    @NotNull
    private String requiredValue;
    private String notRequiredValue;
    @Size(max=10)
    private String max10Chars;

    //getters/setters
    //....

}

资源接口

@Path("/testJaxb")
public interface DiagnosticResourceApi {

    @Path("/test")
    @POST
    @ApiOperation(value="Test JaxB", notes="Testing jaxb annotation." )
    public Response testJaxb(@Valid TestRequest request);

}

资源实施

public class DiagnosticResourceImpl implements DiagnosticResourceApi{

    public Response testJaxb(TestRequest request){
        return Response.ok().build();
    }
}

泽西岛资源配置

@Component
@ApplicationPath("/api")
public class JerseyResourceConfig extends ResourceConfig {

    public JerseyResourceConfig() {
        register(DiagnosticResourceImpl.class);
        property(org.glassfish.jersey.server.ServerProperties.BV_SEND_ERROR_IN_RESPONSE, true);

    }

}

1 个答案:

答案 0 :(得分:0)

固定。我在请求对象BO中添加了@valid。显然,对象属性不会继承其父级验证约束。效果很好。

public class TestRequest {
    @Valid
    private TestBo businessObject;

    public TestBo getBusinessObject() {
        return businessObject;
    }

    public void setBusinessObject(TestBo businessObject) {
        this.businessObject = businessObject;
    }

}