Cześć, mam taki oto kod:
Akcja kontrolera oznaczonego jako @RestController:
@RequestMapping(method = RequestMethod.PUT)
public ResponseEntity<List<Card>> put(@RequestBody CardDTO cardDTO) {
Card card = new Card();
//kilkanaście linii kodu
card.setName(cardDTO.getName());
card.setDescription(cardDTO.getDescription());
//kilkanaście linii kodu
if(id != null) {
card.setId(id);
cardService.updateCard(card);
} else {
cardService.saveCard(card);
}
}
Czyli z formularza binduję do obiektu CardDTO, który wygląda tak:
public class CardDTO {
private Integer id;
private String description;
@Length(min = 5)
private String name;
private Integer project;
@Length(min = 5)
private Integer category;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Integer getProject() {
return project;
}
public void setProject(Integer type) {
this.project = type;
}
public Integer getCategory() {
return category;
}
public void setCategory(Integer category) {
this.category = category;
}
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}
i później za pomocą jakiejś tam logiki przerabiam na encję Card:
@Entity
@Table(name="card")
public class Card {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@OneToOne
@NotNull
@JoinColumn(name = "fk_user_id")
private User owner;
@OneToOne
@NotNull
@JoinColumn(name = "fk_category_id")
private CardCategory category;
@OneToOne
@NotNull
@JoinColumn(name = "fk_project_id")
private Project project;
@Column(name = "card_name")
@Length(min = 5)
private String name;
@NotNull
@Column(name = "card_description")
@Length(min = 5)
private String description;
public Card() {}
public Card(User owner, Project project, CardCategory cardCategory, String cardDescription) {
this.owner = owner;
this.project = project;
this.category = cardCategory;
this.description = cardDescription;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public User getOwner() {
return owner;
}
public void setOwner(User owner) {
this.owner = owner;
}
public CardCategory getCategory() {
return category;
}
public void setCategory(CardCategory category) {
this.category = category;
}
public Project getProject() {
return project;
}
public void setProject(Project type) {
this.project = type;
}
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}
Dodatkowo mam napisany handler
@ExceptionHandler(BindException.class)
public ResponseEntity<RestApiError> handleBindException(BindException e) {
logger.error("Api Error caused by exception", e);
return restApiErrorFromBindingResult(e, ApiErrorCodes.BIND_EXCEPTION);
}
Wysyłam w requeście name oraz description o długości 3 znaków - błędne dane. Spodziewam się, że dostanę wyjątek BindException, ale dostaję wyjątek ConstraintValidationException przez co zadziała handler dla Exception.class.
Co w tym przypadku robię źle lub dlaczego nie dostaję takiego wyjątku? O ile dobrze zrozumiałem logi tomcata to wyjątek dostaję w momencie próby zapisu encji Card, a nie bindowania CardDTO. Dodałem @Length do DTO, ale nadal to samo. Próbowałem również @Valid, ale z @RequestBody nie można jej dodawać.