Search code examples
jax-rsejbbean-validationwebsphere-8

IBM Websphere 8.5 bean validation issue in JAX-RS resources


Here is the example of the code that I use:

@Stateless
@Path("/rest")
public class MyResouce{
    @POST
    @Path("/test")
    public Response test(@Valid Test t){
        return Response.ok().build();
    }
}

public class Test {
    @Size(max = 3)
    private String val;

    public String getVal() {
        return val;
    }

    public void setVal(String val) {
        this.val = val;
    }
}

I expect a ValidationException when I pass an invalid object (length of val more than 3) but the exception doesn't occur. When I inject the validator and to do validation programmatically:

@Path("/rest")
public class MyResouce{

    @Resource
    private Validator validator;

    @POST
    @Path("/test")
    public Response test(@Valid Test t){
        Set<ConstraintViolation<Test>> violations = validator.validate(t); // size = 1, means t object is invalid
        return Response.ok().build();
    }
}

the result of the validation has 1 ConstraintViolation item that means the object is invalid and it looks like the annotation @Valid is ignored. How to validate objects non programmatically? Here is my validation.xml descriptor:

<?xml version="1.0" encoding="UTF-8"?>
<validation-config
        xmlns="http://jboss.org/xml/ns/javax/validation/configuration"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/configuration validation-configuration-1.0.xsd">

</validation-config>

Websphere version 8.5.5.11, JAX-RS 1.1


Solution

  • JAX-RS 1.1 does not automatically integrate with Bean Validation, so you would be required to perform your own validation (as you did in your second example).

    JAX-RS 2.0 (available in WebSphere v9) or 2.1 (available in WebSphere Liberty) both support automatic integration with bean validation. You can find more info on this post.

    Hope this helps, Andy