简体   繁体   中英

Jersey bean validator not initializing in Spring/Jersey/Jetty envrionment

I am attempting this in a Spring/Jetty/Jersey environment. In my Jersey resource config, I make sure to set the correct properties. I see that notNull is a special case, coming in as an empty string instead of null, but exceeding the max characters should at least result in a bad request exception correct?

Posting strings longer than 10 chars results in a 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
    //....

}

Resource Interface

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

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

}

Resource Implementation

public class DiagnosticResourceImpl implements DiagnosticResourceApi{

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

Jersey Resource Configuration

@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);

    }

}

Fixed. I added @valid to the request objects BO. Apparently an objects properties do not inherit their parents validation constraint. Works perfect.

public class TestRequest {
    @Valid
    private TestBo businessObject;

    public TestBo getBusinessObject() {
        return businessObject;
    }

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM