将数据从Angular 窗体发送到spring引导控制器时遇到问题:出现以下错误:warn 15020---[nio-8181-exec-2].c.j.mappingjackson2httpmessageconverter:无法计算类型simple type,class com.biblio.fr.biblio.entite.bookdto:org.springframework.beans.factory.beancreationexception:创建名为“com.biblio.fr.biblio.entite.bookdodeserializer”的bean时出错:bean示例化失败;嵌套异常为org.springframework.beans.beaninstantiationexception:未能示例化[com.biblio.fr.biblio.entite.bookdodeserializer]:未找到默认构造函数;嵌套的异常是java.lang.nosuchmethodexception:com.biblio.fr.biblio.entite.bookdtodeserializer.()
warn 15020---[nio-8181-exec-2].c.j.mappingjackson2httpmessageconverter:无法评估类型simple type,class com.biblio.fr.biblio.entite.bookdto的jackson反序列化:com.fasterxml.jackson.databind.jsonmappingexception:class com.biblio.fr.biblio.entite.bookdodeserializer没有默认(no arg)构造函数
warn 15020---[nio-8181-exec-2].w.s.m.s.defaulthandlerexceptionresolver:已解析[org.springframework.web.httpmediatypenotsupportedexception:内容类型'application/json;字符集=utf-8'不支持]*
这是我的java代码:
@RequestMapping(value = "/addBook", method = RequestMethod.POST, produces = "application/json")
@ApiOperation(value = "Add a new Book in the Library", response = BookDTO.class)
@ApiResponses(value = { @ApiResponse(code = 409, message = "Conflict: the book already exist"),
@ApiResponse(code = 201, message = "Created: the book is successfully inserted"),
@ApiResponse(code = 304, message = "Not Modified: the book is unsuccessfully inserted") })
public ResponseEntity<BookDTO> createNewBook(@RequestBody BookDTO bookDTORequest) {
// , UriComponentsBuilder uriComponentBuilder
Book existingBook = bookService.findBookByIsbn(bookDTORequest.getIsbn());
if (existingBook != null) {
return new ResponseEntity<BookDTO>(HttpStatus.CONFLICT);
}
Book bookRequest = mapBookDTOToBook(bookDTORequest);
Book book = bookService.saveBook(bookRequest);
if (book != null && book.getId() != null) {
BookDTO bookDTO = mapBookToBookDTO(book);
return new ResponseEntity<BookDTO>(bookDTO, HttpStatus.CREATED);
}
return new ResponseEntity<BookDTO>(HttpStatus.NOT_MODIFIED);
}
private BookDTO mapBookToBookDTO(Book book) {
ModelMapper mapper = new ModelMapper();
BookDTO bookDTO = mapper.map(book, BookDTO.class);
if (book.getCategory() != null) {
bookDTO.setCategory(new CategoryDTO(book.getCategory().getCode(), book.getCategory().getLabel()));
}
return bookDTO;
}
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonDeserialize(using = BookDTODeserializer.class)
@Data
@AllArgsConstructors
public class BookDTO implements Comparable<BookDTO> {
@ApiModelProperty(value = "Book id")
private Integer id;
@ApiModelProperty(value = "Book title")
private String title;
@ApiModelProperty(value = "Book isbn")
private String isbn;
@ApiModelProperty(value = "Book release date by the editor")
private LocalDate releaseDate;
@ApiModelProperty(value = "Book register date in the library")
private LocalDate registerDate;
@ApiModelProperty(value = "Book total examplaries")
private Integer totalExamplaries;
@ApiModelProperty(value = "Book author")
private String author;
@ApiModelProperty(value = "Book category")
private CategoryDTO category;
@Override
public int compareTo(BookDTO o) {
return title.compareToIgnoreCase(o.getTitle());
}
public BookDTO() {
super();
}
}
@Entity
@Data
@AllArgsConstructors
public class Book {
private static final long serialVersionUID = 425345L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String title;
private String author;
private String publisher;
private String publicationDate;
private String language;
private String category;
private int numberOfPages;
private String format;
private String isbn;
private double shippingWeight;
private double listPrice;
private double ourPrice;
private boolean active = true;
@Column(columnDefinition = "text")
private String description;
private int inStockNumber;
@Transient
private MultipartFile bookImage;
}
@Data
@AllArgsConstructors
@JsonDeserialize(using = CategoryDTODeserializer.class)
public class CategoryDTO implements Comparable<CategoryDTO> {
public CategoryDTO() {
}
public CategoryDTO(String code, String label) {
super();
this.code = code;
this.label = label;
}
@ApiModelProperty(value = "Category code")
private String code;
@ApiModelProperty(value = "Category label")
private String label;
}
@Entity
@Table(name = "CATEGORY")
public class Category {
public Category() {
}
public Category(String code, String label) {
super();
this.code = code;
this.label = label;
}
private String code;
private String label;
@Id
@Column(name = "CODE")
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
@Column(name = "LABEL", nullable = false)
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
}
public class BookDTODeserializer extends StdDeserializer<BookDTO> {
@Override
public BookDTO deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
// TODO Auto-generated method stub
JsonNode node = p.getCodec().readTree(p);
Integer id = (Integer) ((IntNode) node.get("id")).numberValue();
String title = node.get("title").asText();
String isbn = node.get("isbn").asText();
LocalDate releaseDate = LocalDate.parse(node.get("releaseDate").asText());
LocalDate registerDate = LocalDate.parse(node.get("registerDate").asText());
Integer totalExamplaries = (Integer) ((IntNode) node.get("totalExamplaries")).numberValue();
String author = node.get("author").asText();
String codeCategory = node.get("code").asText();
String labelCategory = node.get("label").asText();
return new BookDTO(id, title, isbn, releaseDate, registerDate, totalExamplaries, author,
new CategoryDTO(codeCategory, labelCategory));
// return null;
}
public BookDTODeserializer(Class<?> vc) {
super(vc);
// TODO Auto-generated constructor stub
}
public BookDTODeserializer(JavaType valueType) {
super(valueType);
// TODO Auto-generated constructor stub
}
public BookDTODeserializer(StdDeserializer<?> src) {
super(src);
// TODO Auto-generated constructor stub
}
}
public class CategoryDTODeserializer extends StdDeserializer<CategoryDTO> {
@Override
public CategoryDTO deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
JsonNode node = p.getCodec().readTree(p);
String codeCategory = node.get("code").asText();
String labelCategory = node.get("label").asText();
return new CategoryDTO(codeCategory, labelCategory);
}
public CategoryDTODeserializer(Class<?> vc) {
super(vc);
// TODO Auto-generated constructor stub
}
public CategoryDTODeserializer(JavaType valueType) {
super(valueType);
// TODO Auto-generated constructor stub
}
public CategoryDTODeserializer(StdDeserializer<?> src) {
super(src);
// TODO Auto-generated constructor stub
}
}
还有我的Angular 代码
saveBook(book: Book): Observable<Book>{
let headers = new HttpHeaders();
headers.append('content-type', 'application/json');
headers.append('accept', 'application/json');
return this.http.post<Book>(environment.apiUrl+'/rest/book/api/addBook', book, {headers: headers});
}
有什么想法吗?
2条答案
按热度按时间guykilcj1#
根据警告/错误消息,您缺少以下内容:
找不到默认构造函数;嵌套的异常是java.lang.nosuchmethodexception:com.biblio.fr.biblio.entite.bookdodeserializer
用@noargsconstuctor声明bookdodeserializer
添加消费也作为json@requestmapping(value=“/addbook”,method=requestmethod.post,products=“application/json”,consumes=“application/json”)
我不明白为什么您需要这个“手动”反序列化程序,因为您已经在请求体中接收到作为json的dto。
sg24os4d2#
内容类型'application/json;charset=utf-8'不支持spring引导控制器调用
问题不在这里吗
问题是:
com.biblio.fr.biblio.entite.bookdodeserializer没有默认(无参数)构造函数
只需添加
@NoArgsConstructor
对您没有帮助,错误将显示: