...
Bean
...
Validation
...
Feature
...
Table of Contents |
Introduction
...
- properties of Java Beans
- method and constructor parameters
- method return values
For example:
Code Block |
public class Person {
@NotNull private String firstName;
@NotNull private String lastName;
@Valid @NotNull private Person boss;
public @NotNull String saveItem( @Valid @NotNull Person person, @Max( 23 ) BigDecimal age ) {
// ...
}
}
|
...
Bean Validation support in Apache CXF is implementation-independent and is built solely using Bean Validation API. The required dependencies are:
Code Block |
|
|
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<!-- use 3.0-b02 version for Java 6 -->
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.el</artifactId>
<!-- use 3.0-b01 version for Java 6 -->
<version>3.0.0/version>
</dependency>
|
...
Hibernate Validator is a mature and feature-rich validation provider with the full Bean Validation 1.1 support (as of version 5.x.x which is the reference implementation for JSR 349 - Bean Validation 1.1 API). Add the following dependency:
Code Block |
|
|
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.0.1.Final</version>
</dependency>
|
...
Current stable version of Apache BVal (0.5) doesn't support Bean Validation 1.1 but the upcoming 1.1.0 will have it fully implemented (at the moment 1.1.0-alpha-SNAPSHOT could be used).
No Format |
<dependency>
<groupId>org.apache.bval</groupId>
<artifactId>bval-jsr</artifactId>
<version>1.1.0-alpha-SNAPSHOT</version>
</dependency>
|
...
The following snippets show how to get Bean Validation 1.1 interceptors activated for both JAX-RS and JAX-WS services using Spring:
Code Block |
|
|
// Interface implemented by both JAX-RS and JAX-WS services:
@WebService(targetNamespace = "http://bookworld.com")
@Path("/")
public interface BookWorld {
@POST
@Produces("text/xml")
@Consumes("text/xml")
@Valid
BookWithValidation echoBook(@Valid BookWithValidation book);
}
@WebService(endpointInterface = "org.apache.cxf.systest.jaxrs.validation.spring.BookWorld",
serviceName = "BookWorld")
public class BookWorldImpl implements BookWorld {
@Override
public BookWithValidation echoBook(BookWithValidation book) {
return book;
}
}
|
Code Block |
|
|
<!-- JAX-RS and JAX-WS endpoints -->
<jaxrs:server address="/bwrest">
<jaxrs:serviceBeans>
<ref bean="bookWorldValidation"/>
</jaxrs:serviceBeans>
<jaxrs:providers>
<ref bean="exceptionMapper"/>
</jaxrs:providers>
<jaxrs:features>
<ref bean="commonValidationFeature" />
</jaxrs:features>
</jaxrs:server>
<jaxws:endpoint xmlns:s="http://bookworld.com"
serviceName="s:BookWorld"
endpointName="s:BookWorldPort"
implementor="#bookWorldValidation"
address="/bwsoap">
<jaxws:features>
<ref bean="commonValidationFeature" />
</jaxws:features>
</jaxws:endpoint>
<bean id="bookWorldValidation" class="org.apache.cxf.systest.jaxrs.validation.spring.BookWorldImpl"/>
<bean id="commonValidationFeature" class="org.apache.cxf.validation.BeanValidationFeature"/>
|
...
Code Block |
JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
sf.setResourceClasses( ... );
sf.setResourceProvider( ... );
sf.setProvider(new ValidationExceptionMapper());
sf.setInInterceptors(Arrays.< Interceptor< ? extends Message > >asList(new new JAXRSBeanValidationInInterceptor()));
sf.setOutInterceptors(Arrays.< Interceptor< ? extends Message > >asList(new JAXRSBeanValidationOutInterceptor()));
sf.create();
|
...
Following the similar approach as for JAXRSServerFactoryBean, in case of Spring respective bean definitions should be added under <jaxrs:outInterceptors>, <jaxrs:inInterceptors> and <jaxrs:providers> sections, f.e.:
Code Block |
|
|
<jaxrs:server address="/">
<jaxrs:inInterceptors>
<ref bean="validationInInterceptor" />
</jaxrs:inInterceptors>
<jaxrs:outInterceptors>
<ref bean="validationOutInterceptor" />
</jaxrs:outInterceptors>
<jaxrs:serviceBeans>
...
</jaxrs:serviceBeans>
<jaxrs:providers>
<ref bean="exceptionMapper"/>
</jaxrs:providers>
</jaxrs:server>
<bean id="exceptionMapper" class="org.apache.cxf.jaxrs.validation.ValidationExceptionMapper"/>
<bean id="validationProvider" class="org.apache.cxf.validation.BeanValidationProvider" />
<bean id="validationInInterceptor" class="org.apache.cxf.jaxrs.validation.JAXRSBeanValidationInInterceptor">
<property name="provider" ref="validationProvider" />
</bean>
<bean id="validationOutInterceptor" class="org.apache.cxf.jaxrs.validation.JAXRSBeanValidationOutInterceptor">
<property name="provider" ref="validationProvider" />
</bean>
|
...
Validating simple input parameters
Code Block |
@POST
@Path("/books")
public Response addBook(
@NotNull @Pattern(regexp = "\\d+") @FormParam("id") String id,
@NotNull @Size(min = 1, max = 50) @FormParam("name") String name) {
// do some work
return Response.created().build();
}
|
Validating complex input parameters
Code Block |
@POST
@Path("/books")
public Response addBook( @Valid Book book ) {
// do some work
return Response.created().build();
}
|
This example assumes that class Book has validation constraints defined:
Code Block |
public class Book {
@NotNull @Pattern(regexp = "\\d+") private String id;
@NotNull @Size(min = 1, max = 50) private String name;
// ...
}
|
Validating return values (non-Response)
Code Block |
@GET
@Path("/books/{bookId}")
@Override
@NotNull @Valid
public Book getBook(@PathParam("bookId") String id) {
return new Book( id );
}
|
...
Validating return values (Response)
Code Block |
@GET
@Path("/books/{bookId}")
@Valid @NotNull
public Response getBookResponse(@PathParam("bookId") String id) {
return Response.ok( new Book( id ) ).build();
}
|
...