mercredi 25 février 2015

How to manually trigger spring validation?

The annotated spring validation on fields of a POJO works when it is created from json request body. However, when I create the same object manually (using setters) and want to trigger validation, I'm not sure how to do that.


Here is the Registration class, which has Builder inner class that can build the object. In the build method I would like to trigger spring validation. Please scroll to the bottom and check Builder.build() and Builder.valiate() methods to see current implementation. I'm using javax.validation.Validator to trigger validation, but I prefer to leverage spring validation if possible.



package com.projcore.dao;

import com.projcore.util.ToString;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.hibernate.validator.constraints.NotEmpty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.validation.ConstraintViolation;
import javax.validation.Valid;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.constraints.Size;
import java.util.List;
import java.util.Set;

/**
* The data transfer object that contains the information of a Registration
* and validation rules for attributes.
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public final class Registration {
private static final Logger LOGGER = LoggerFactory.getLogger(Registration.class);

private String id;

@NotEmpty
@Size(max = 255)
private String messageId;

@NotEmpty
@Size(max = 255)
private String version;

@Size(max = 255)
private String system;

public Registration() {
}

private Registration(Builder builder) {
this.id = builder.id;
this.messageId = builder.messageId;
this.version = builder.version;
this.system = builder.system;
}

public static Builder getBuilder() {
return new Builder();
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getMessageId() {
return messageId;
}

public void setMessageId(String messageId) {
this.messageId = messageId;
}

public String getVersion() {
return version;
}

public void setVersion(String version) {
this.version = version;
}

public String getSystem() {
return system;
}

public void setSystem(String system) {
this.system = system;
}

@Override
public String toString() {
return ToString.create(this);
}

/**
* Builder pattern makes the object easier to construct in one line.
*/
public static class Builder {

private String id;

private String messageId;

private String version;

private String system;

private Builder() {}

public Builder id(String id) {
this.id = id;
return this;
}

public Builder messageId(String messageId) {
this.messageId = messageId;
return this;
}


public Builder version(String version) {
this.version = version;
return this;
}

public Builder system(String system) {
this.system = system;
return this;
}

public Registration build() {
Registration entry = new Registration(this);

// *** Would like to trigger spring validation here ***
Set violations = validate(entry);
if (violations.isEmpty())
return entry;
else
throw new RuntimeException(violations.toString());
}

private Set validate(Registration entry) {
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
Set<ConstraintViolation<Registration>> constraintViolations = validator.validate(entry);
return constraintViolations;
}

}
}


Validation works fine here:



@RequestMapping(method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
Registration create(@RequestBody @Valid Registration registration)

Aucun commentaire:

Enregistrer un commentaire