超类的Bean验证

时间:2014-08-11 06:33:12

标签: java apache-camel

您好我有关于Bean Validation(JSR-303)的问题。我们可以针对bean中提供的注释集验证一个bean。如果我有一个扩展Bean Vehicle的Bean Car,如果我将Bean Car传递给验证器,如何使Bean Vehicle(即Super Class)也得到验证怎么办?

我在Camel中使用这个Bean验证。以下是我的代码

public void configure() throws Exception {
    from("file:data/source?noop=true").process(new Processor() {
        @Override
        public void process(Exchange exchange) throws Exception {
            System.out.println("Process 1");
            Car car = new Car();
            //this property is of Super Class
            //car.setVehicleId(1);
            car.setName("Swift");
            car.setCompany("Maruti");

            exchange.getIn().setBody(car);
        }
    }).to("bean-validator://v").process(new Processor() {
        @Override
        public void process(Exchange arg0) throws Exception {
            System.out.println("Exchange is : "+arg0.getIn().getBody(Car.class));
        }
    });
}

我的Car Bean是

public class Car extends Vehicle{
    @NotNull
    private String name;
    @NotNull
    private String company;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getCompany() {
        return company;
    }
    public void setCompany(String company) {
        this.company = company;
    }   
}   

我的车辆豆是

public class Vehicle {
    @NotNull
    private int vehicleId;

    public int getVehicleId() {
        return vehicleId;
    }

    public void setVehicleId(int vehicleId) {
        this.vehicleId = vehicleId;
    }   
}

谢谢。将非常感谢帮助。

1 个答案:

答案 0 :(得分:1)

我得到了这个工作。问题是我在Vehicle中使用了原始类型[private int vehicleId; ] 所以我用[private Integer vehicleId纠正了它; ]。所以在此之后我的汽车豆是

public class Vehicle {
    @NotNull
    private Integer vehicleId;

    public Integer getVehicleId() {
        return vehicleId;
    }

    public void setVehicleId(Integer vehicleId) {
        this.vehicleId = vehicleId;
    }   
}