我有一个带有一些注释的pojo,如下所示。
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import com.fasterxml.jackson.annotation.JsonProperty;
import javax.validation.constraints.*;
import java.util.Date;
@XmlRootElement()
@XmlAccessorType(XmlAccessType.FIELD)
public class Author {
@NotNull
private int contactId;
@Min(value = 1000000000000L, message = "EAN is less than 13 digits")
@Max(value = 9999999999999L, message = "EAN is greater than 13 digits")
@JsonProperty("EAN")
private long EAN;
@NotNull(message = "First name is compulsory")
private String person_firstname;
@NotNull(message="Email Address is compulsory")
@Pattern(regexp = "^([a-zA-Z0-9_\\-\\.]+)@([a-zA-Z0-9_\\-\\.]+)\\.([a-zA-Z]{2,5})$", message = "Email Address is not a valid format")
private String person_email;
// getters and setters
}
下面是我的验证员。
public class AuthorValidator implements Processor {
public void process(Exchange exchange) throws Exception {
String exceptions="Exception Occured";
try {
String jsonBody = exchange.getIn().getBody(String.class);
ObjectMapper objectMapper = new ObjectMapper();
TypeReference<List<Author>> mapType = new TypeReference<List<Author>>() {};
List<Author> authorList = objectMapper.readValue(jsonBody, mapType);
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
for(Author au : authorList){
Set<ConstraintViolation<Author>> violations = validator.validate(au);
for (ConstraintViolation<Author> violation : violations) {
exceptions+=violation.getMessage();
}
}
}catch(Exception e)
{
exceptions=e.getMessage();
}
exchange.getOut().setBody(exceptions);
}
当我在eclipse中测试类时,它给了我一个正确的验证输出。但是当我试图从servicemix调用它时抛出以下错误。
Unable to create a Configuration, because no Bean Validation provider could
be found. Add a provider like Hibernate Validator (RI) to your classpath.
我尝试安装所有捆绑包但仍然存在错误。 Apache ServiceMix :: Bundles :: hibernate-validator:5.0.2 Hibernate验证器引擎 骆驼豆验证 Bean Validation API
所有必需的罐子都在构建路径中
是否可以让我知道我需要在servicemix上安装哪些软件包才能使验证工作。
答案 0 :(得分:0)
我已经在apache骆驼中实现了同样的效果。使用以下链接表示相同。
答案 1 :(得分:0)
具有验证者和杰克逊的依赖项:
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-bean-validator</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jackson</artifactId>
<version>${camel.version}</version>
</dependency>
和带注释的类:
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.NotBlank;
public class Author {
@NotBlank
@Size(min = 1, max = 20)
private int contactId;
}
我肯定会使用unmarshall + bean-validator端点,而忘了unmarshall-validation Processor:
<unmarshal>
<json library="Jackson"
unmarshalTypeName="Author" />
</unmarshal>
<to uri="bean-validator://author" />
...