jeudi 26 juillet 2018

Validation not working at all in Spring Boot application

I have created a bean that contains some fields with the annotation @NotNull, however I am still able to insert this bean with the fields annotated null, and it seems the validations is just ignored. Bellow you can see some peaces of my code:

Document(collection = "events") public class Event implements Serializable {

private static final long serialVersionUID = 1L;

@Id
private String id;

@NonNull
private String name;

@NonNull
private String description;

@NonNull
private String placeId;

@NonNull
private String placeDescription;

@NonNull
private String colorCode;

@NonNull
private Period period;

public Event() {
    super();
}

public Event(String id, String name, String description, String placeId, String placeDescription, String colorCode,
        Period period) {
    super();
    this.id = id;
    this.name = name;
    this.description = description;
    this.placeId = placeId;
    this.placeDescription = placeDescription;
    this.colorCode = colorCode;
    this.period = period;
}

public String getId() {
    return id;
}

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

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public String getPlaceId() {
    return placeId;
}

public void setPlaceId(String placeId) {
    this.placeId = placeId;
}

public String getPlaceDescription() {
    return placeDescription;
}

public void setPlaceDescription(String placeDescription) {
    this.placeDescription = placeDescription;
}

public String getColorCode() {
    return colorCode;
}

public void setColorCode(String colorCode) {
    this.colorCode = colorCode;
}

public Period getPeriod() {
    return period;
}

public void setPeriod(Period period) {
    this.period = period;
}

}

@RestController public class EventController {

@Autowired
EventRepository eventRepository;

@RequestMapping(method = RequestMethod.GET, value = "/events")
public Iterable<Event> findEventsByPeriod(@RequestParam(value = "period") String periodInput)
        throws JsonParseException, JsonMappingException, IOException {

    ObjectMapper mapper = new ObjectMapper();

    mapper.registerModule(new JSR310Module());

    final Period period = mapper.readValue(periodInput, Period.class);

    return eventRepository.getEventsByPeriod(period);
}

@RequestMapping(method = RequestMethod.POST, value = "/events")
public String save(@Valid @RequestBody Event event) {
    eventRepository.save(event);
    return event.getId();
}

}

Am I missing something?




Aucun commentaire:

Enregistrer un commentaire