Reputation: 20329
I'm currently experimenting with some RESTful JAX and I want to validate a custom input. Normally regex would be fine but I need to do a more extensive check (~10 different regex patterns). I found this page when searching for jaxrs validation. I noted it says "Draft" but I thought I'd give it a try.
I wrote my parameter annotation like this:
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = FooBarValidator.class)
public @interface FooBarParam
{
}
The validator looks like this:
@Provider
public class FooBarValidator
implements ConstraintValidator<FooBar, Long>
{
@Override
public void initialize(FooBar constraintAnnotation)
{
}
@Override
public boolean isValid(Long value, ConstraintValidatorContext context)
{
// validation goes here, this is a test validation
return (value > 50);
}
}
The web service looks like this:
@javax.ejb.Stateless
@Path("test")
public class testRS
{
@GET
@Path("foobar/{fooBar: [0-9]+}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.TEXT_PLAIN)
public String testService(@FooBar @PathParam("fooBar") Long fooBar)
{
return "tested with: " + fooBar;
}
}
But if I call my web service with my browser using "http://localhost:8080/jaxtest/rest/test/foobar/11" the web service gets called and I'm presented with "tested with: 11". The web service works fine, except the validator doesn't get called.
I've tried setting breakpoints in the validator class and the annotation interface but none are hit.
I've got a sneaking suspicion that I'm doing something that isn't possible because of the "Draft" header in the referenced documentation. So if I'm doing something wrong or if there are alternatives, I'm glad to hear it.
Upvotes: 3
Views: 5400
Reputation: 20329
Thanks to the hint @PiotrKochański gave me I've successfully implemented exactly what I wanted. The biggest problem was that I'm bound to using Glassfish. By default Glassfish uses Jersey to handle JAX stuff.
It took me well over 10 hours of struggling to complete this so let this be a time saver for anyone who stumbles upon this.
First of all, use Maven, this makes your life so much easier.
Second step, add the JBoss repo to your pom.xml
<repositories>
<repository>
<id>jboss-public-repository-group</id>
<name>JBoss Public Maven Repository Group</name>
<url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url>
<layout>default</layout>
<releases>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
</snapshots>
</repository>
</repositories>
Third step, add dependencies to pom.xml
<!-- Needed for validator interceptors -->
<dependency>
<groupId>org.jboss.seam.rest</groupId>
<artifactId>seam-rest</artifactId>
<version>3.1.0.Final</version>
</dependency>
<!-- JBoss' RS implementation -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>2.3.4.Final</version>
</dependency>
<!-- Because I use JSON I need RESTeasy be able to handle this -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jettison-provider</artifactId>
<version>2.3.4.Final</version>
</dependency>
<!-- This is THE part that integrates validation in RESTeasy -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-hibernatevalidator-provider</artifactId>
<version>2.3.4.Final</version>
</dependency>
The last dependency took me quite a while. The docs @PiotrKochański pointed to didn't mention this. However in another version of the docs I found this:
The integration between the API implementation and RESTEasy is done through the resteasy-hibernatevalidator-provider component. In order to integrate, we need to add resteasy-hibernatevalidator-provider and hibernate-validator to the classpath. With maven it's just a matter of including the following dependency:
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-hibernatevalidator-provider</artifactId>
<version>2.3-RC1</version>
</dependency>
The fourth step was to add this to web.xml
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/rest</param-value>
</context-param>
<listener>
<listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>
<servlet>
<servlet-name>REST Service</servlet-name>
<servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
The fifth step was to modify the web service class like this:
@javax.ejb.Stateless
@Path("test")
public class testRS
{
@GET
@Path("foobar/{fooBar}")
@Produces(MediaType.APPLICATION_JSON)
@org.jboss.resteasy.spi.validation.ValidateRequest
public String testService(@FooBar @PathParam("fooBar") Long fooBar)
{
return "tested with: " + fooBar;
}
}
The sixth step was to modify the @interface
to this:
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = FooBarValidator.class)
public @interface FooBarParam
{
String message() default "{constraint.FooBar}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
Also as a bonus; I came across a presentation about Bean Validation by Emmanuel Bernard I thought I might share as this explains a lot of interesting stuff.
Upvotes: 2
Reputation: 7979
The page you found is one of the proposal on what should go into JAX-RS 2.0 (which is not final and there is no implementation of that). The plan is for JAX-RS 2.0 to integrate with Bean Validation - but as I said, that's not implemented yet.
Currently, if you want to validate input, you can declare the parameter as String (instead of Long) and do the validation as part of the resource method. Then convert to Long if the validation passes.
Upvotes: 0