验证spring rest api中的实体列表

时间:2018-05-30 07:40:53

标签: java spring hibernate rest spring-boot

我正在编写一个控制器,它将带有酒店的json数组并在保存之前对其进行验证。例如:

[
{   
    "name": "Hotel_1",
    "address": "address_1",
    "starsNumber": 3
},
{   
    "name": "Hotel_2",
    "address": "address_2",
    "starsNumber": 4
},
{   
    "name": "Hotel_2",
    "address": "address_2",
    "starsNumber": 3
}
]

一开始我使用List<hotel>代替WrapList<Hotel>作为RequestBody,但它没有用。所以当我在本主题stackoverflow topic中阅读时,我尝试将其包装在wrap对象中,但它不起作用,我总是在WrapList对象中得到空列表。所以问题是如何以优雅的方式验证实体列表。

如果我在控制器签名中使用List而不是WrapList,那么我的BindingResult永远不会出错而不是发送无效数据。

这是我的酒店模型:

@Entity(name = "Hotel")
public class Hotel {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @NotBlank
    @Column(unique = true)
    private String name;

    private String address;

    @Range(min = 0, max = 5)
    private int starsNumber;

    @OneToMany(mappedBy = "hotel", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @Valid
    private List<Reservation> reservationList = new ArrayList<>();

    public Hotel() {
    }

    public Hotel(String name, String address, int starsNumber, ArrayList<Reservation> reservationList) {
        this.name = name;
        this.address = address;
        this.starsNumber = starsNumber;
        this.reservationList = reservationList;
    }


    public List<Reservation> getReservationList() {
        return reservationList;
    }

    public void setReservationList(List<Reservation> reservationList) {
        this.reservationList = reservationList;
    }

    @Override
    public String toString() {
        return String.format(
                "Hotel[id=%d, name='%s', address='%s', starts=%d]\"", id, name, address, starsNumber
        );
    }

    public void addReservation(Reservation reservation){
        reservationList.add(reservation);
        reservation.setHotel(this);
    }
}

这是我的WrapList类:

class WrapList<E> implements List<E>{

@Valid
@NotEmpty
private List<E> list = new ArrayList<>();


public WrapList() {
}

public WrapList(@Valid List<E> hotelList) {
    this.list = hotelList;
}

public List<E> getList() {
    return list;
}

public void setList(List<E> list) {
    this.list = list;
}


@Override
public int size() {
    return list.size();
}

@Override
public boolean isEmpty() {
    return list.isEmpty();
}

@Override
public boolean contains(Object o) {
    return false;
}

@Override
public Iterator<E> iterator() {
    return list.iterator();
}

@Override
public Object[] toArray() {
    return list.toArray();
}

这是我的休息控制器:

@PostMapping("/create-many-test/")
    public ResponseEntity createManyTest(@Valid @RequestBody WrapList<Hotel> wrapHotelList, BindingResult bindingResult) {

        System.out.println(wrapHotelList.getList().toString());


        if (bindingResult.hasErrors()){

            return new ResponseEntity<>(bindingResult.getFieldErrors(), HttpStatus.BAD_REQUEST);

        }

        return new ResponseEntity<>(wrapHotelList.getList(), HttpStatus.CREATED);
    }

0 个答案:

没有答案