java我得到了org。springframework。网状物servlet。处理程序。尝试保存书本时的AbstractHandlerExceptionResolver
我正在尝试将新书添加到数据库中。我有作者表和书桌。它们与@manytomy有关。我也使用DTO。有人能帮我解决这个问题吗
控制器
@GetMapping("/create")
public String createBook(Model model) {
model.addAttribute("genre", genreService.findAll());
model.addAttribute("authors", authorService.findAll());
model.addAttribute("createBook", new BookDTO());
return "/book/createBook";
}
@PostMapping("/create")
public String createBook(@ModelAttribute("createBook") BookDTO bookDTO) {
bookService.create(bookDTO);
return "redirect:/book/all";
}
DTO
@Data
public class BookDTO {
private int id;
private String title;
private int copies;
private GenreDTO genre;
private List<AuthorDTO> author;
private List<BookOrder> bookOrderList;
}
jsp
<div>
<span id="inputGroup-sizing-small">Author</span>
<form:checkboxes path="author" items="${authors}"></form:checkboxes>
</div>
<input type="submit" value="Submit">
结果
18-Nov-2021 15:18:55.316 WARNING [http-nio-8090-exec-2] org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.logException Resolved [org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'createBook' on field 'author': rejected value [AuthorDTO(id=1, name=Joanne, surname=Rowling),AuthorDTO(id=7, name=Dan, surname=Brown)]; codes [typeMismatch.createBook.author,typeMismatch.author,typeMismatch.java.util.List,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [createBook.author,author]; arguments []; default message [author]]; default message [Failed to convert property value of type 'java.lang.String[]' to required type 'java.util.List' for property 'author'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.web.club3.dto.AuthorDTO' for property 'author[0]': no matching editors or conversion strategy found]]
共 (0) 个答案