jeudi 23 octobre 2014

@Validated not taken into account


Vote count:

0




I have a pretty simple REST service set up, and was starting to validate it using Bean validation. Everything worked fine, until I noticed I needed different Validations on the same object for different methods.


I tried group Validation. My problem is, when I replace my functionning @Valid annotations with @Validated(Group.class) annotations, no more validation is done.


Here are my objects and configuration files:


End point:



<jaxrs:server id="myRestServices" address="/v1">
<jaxrs:serviceBeans>
<ref bean="rentalsService"/>
</jaxrs:serviceBeans>
<jaxrs:providers>
<ref bean="jsonProvider"/>
</jaxrs:providers>
<jaxrs:inInterceptors>
<ref bean="validationInInterceptor" />
</jaxrs:inInterceptors>
</jaxrs:server>
<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>


Service:



public interface RentalsService {

public Rental createRental(@Validated({ CreateValidationGroup.class }) Rental rental);

public Rental updateRental(String RentalId, @Validated({ UpdateValidationGroup.class }) Rental rental);


ServiceImpl



@Path("/rentals")
@Produces(MediaType.APPLICATION_JSON)
public class RentalServiceImpl implements RentalsService {

private RentalBusinessService rentalBusinessService;

@POST
@Override
@Consumes(MediaType.APPLICATION_JSON)
public Rental createRental(@Validated({ CreateValidationGroup.class }) Rental rental) {
return rentalBusinessService.createRental(rental);
}

@PUT
@Override
@Path("/{RentalId}")
@Consumes(MediaType.APPLICATION_JSON)
public Rental updateRental(@PathParam("RentalId") String RentalId, @Validated({ UpdateValidationGroup.class }) Rental rental) {
return rentalBusinessService.updateRental(RentalId, rental);
}


` Said groups:



public interface CreateValidationGroup {

} public interface UpdateValidationGroup {

}


And finally the Object:



public class Rental {

@Size(min = 2, max = 14, groups = { UpdateValidationGroup.class }, message = " 2 < Status < 4 ")
private String status;


@NotNull
@Size(min = 2, max = 14, message = " 2 < Product < 4 ")
private String product;

@Size(min = 2, max = 14, groups = { CreateValidationGroup.class }, message = " 2 < ID < 4 ")
private String RentalId;


Can i not use Group Validation this way? Switching the @Validated back to @Valid triggers validation.



asked 2 mins ago

Winzu

205






@Validated not taken into account

Aucun commentaire:

Enregistrer un commentaire