我正在使用 Oval 进行验证。我正在使用弹簧批处理来填充(csv 到对象)一个对象并使用 Oval 对其进行验证。但我认为我的@javax.validation.constraints.Pattern 不起作用,因为我的控制台在我运行程序时没有显示错误。
这是我的课:
客户端.jaba
package com.mistaiker;
import net.sf.oval.constraint.Length;
import net.sf.oval.constraint.MinLength;
import net.sf.oval.constraint.NotEmpty;
import net.sf.oval.constraint.NotNegative;
import net.sf.oval.constraint.NotNull;
public class Client {
//Atributos
@NotNull
@NotEmpty
@MinLength(value = 1)
@Length(max=4)
@javax.validation.constraints.Pattern(regexp = "[a-zA-Z]")
private String title;
@NotNull
@NotEmpty
@MinLength(value = 1)
@Length(max=20)
@javax.validation.constraints.Pattern(regexp = "[a-zA-Z]")
private String firstName;
@NotNull
@NotEmpty
@MinLength(value = 1)
@Length(max=20)
@javax.validation.constraints.Pattern(regexp = "^[a-zA-Z]")
private String surname;
@NotNull
@NotEmpty
@NotNegative
@MinLength(value = 10)
@Length(max=10)
@javax.validation.constraints.Pattern(regexp = "[0-9]")
private long regNum;
@NotNull
@NotEmpty
@NotNegative
@MinLength(value = 2)
@Length(max=2)
@javax.validation.constraints.Pattern(regexp = "[0-9]")
private int assigment_1;
@NotNull
@NotEmpty
@NotNegative
@MinLength(value = 2)
@Length(max=2)
@javax.validation.constraints.Pattern(regexp = "[0-9]")
private int assigment_2;
@NotNull
@NotEmpty
@NotNegative
@MinLength(value = 2)
@Length(max=2)
@javax.validation.constraints.Pattern(regexp = "[0-9]")
private int assigment_3;
@NotNull
@NotEmpty
@MinLength(value = 1)
@Length(max=30)
@javax.validation.constraints.Pattern(regexp = "[0-9a-zA-Z]")
private String address;
//metodos
void showData(){
System.out.println(title + "\t" + firstName + "\t" + surname + "\t" +
regNum + "\t" + assigment_1 + "\t" + assigment_2 + "\t" +
assigment_3 + "\t" + address);
}
//Getters y setters
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public long getRegNum() {
return regNum;
}
public void setRegNum(long regNum) {
this.regNum = regNum;
}
public int getAssigment_1() {
return assigment_1;
}
public void setAssigment_1(int assigment_1) {
this.assigment_1 = assigment_1;
}
public int getAssigment_2() {
return assigment_2;
}
public void setAssigment_2(int assigment_2) {
this.assigment_2 = assigment_2;
}
public int getAssigment_3() {
return assigment_3;
}
public void setAssigment_3(int assigment_3) {
this.assigment_3 = assigment_3;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
ClientFieldSetMapper.java
package com.mistaiker;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.validation.BindException;
public class ClientFieldSetMapper implements FieldSetMapper<Client>{
@Override
public Client mapFieldSet(FieldSet fieldSet) throws BindException {
Client client = new Client();
client.setTitle(fieldSet.readRawString(0));
client.setFirstName(fieldSet.readRawString(1));
client.setSurname(fieldSet.readRawString(2));
client.setRegNum(fieldSet.readLong(3));
client.setAssigment_1(fieldSet.readInt(4));
client.setAssigment_2(fieldSet.readInt(5));
client.setAssigment_3(fieldSet.readInt(6));
client.setAddress(fieldSet.readRawString(7));
return client;
}
}
应用程序.java
package com.mistaiker;
import java.util.ArrayList;
import java.util.List;
import net.sf.oval.ConstraintViolation;
import net.sf.oval.Validator;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.core.io.FileSystemResource;
public class App {
public static void main(String[] args){
FlatFileItemReader<Client> itemReader = new FlatFileItemReader<Client>();
itemReader.setResource(new FileSystemResource("C:/Users/juan.m.palazzolo/Desktop/data.csv"));
DefaultLineMapper<Client> lineMapper = new DefaultLineMapper<Client>();
lineMapper.setLineTokenizer(new DelimitedLineTokenizer());
lineMapper.setFieldSetMapper(new ClientFieldSetMapper());
itemReader.setLineMapper(lineMapper);
itemReader.open(new ExecutionContext());
Client client = new Client();
ArrayList<Client> clients = new ArrayList<>();
try {
client = itemReader.read();
//validation!!
Validator validator = new Validator();//Validator object
List<ConstraintViolation> violations = validator.validate(client);
if(violations.size()>0)
{
// Log.severe("Object " + client + " is invalid.");
// throw new BussinessException(violations);
System.out.println("error en validacion");
}
while(client != null){
client.showData();
clients.add(client);
client = itemReader.read();
if(client != null){
violations = validator.validate(client);}
//validation!!
if(violations.size()>0)
{
//LOG.severe("Object " + client + " is invalid.");
//throw new BussinessException(violations);
System.out.println("error en validacion");
}
}
} catch (UnexpectedInputException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*for(Client x: clients){
x.showData();
}*/
}
}
这是我的 csv:
Mr,juan,mik2e,1234567891,75,84,80,Flat 1a\\The Street\\The Town
Mr,John,Smith,1234567892,68,57,72,Flat 1b\\The Street\\The Town
Mr,Zack,Watts,1234567893,32,43,49,Flat 1c\\The Street\\The Town
Mrs,Hillary,Rix,1234567894,87,88,86,Flat 1d\\The Street\\The Town
Miss,Zoe,Adams,1234567895,52,48,57,Flat 1e\\The Street\\The Town
Mr,Andy,Brown,1234567896,42,52,54,Flat 1f\\The Street\\The Town