gpt4 book ai didi

spring - 无法将源 {} 映射到实体

转载 作者:行者123 更新时间:2023-11-29 02:56:54 25 4
gpt4 key购买 nike

我在使用 Spring Data Elasticsearch 从 elasticsearch 读取文档时遇到以下错误:

错误 SearchController - 嵌套异常:无法将源 { 文档数据 } 映射到图书

@Entity
@Table(name="books")
@Document(indexName="booksearchserver",type="book")
public class Book {
@org.springframework.data.annotation.Id
@Id
@Column(name="bookIsbn")
@Field(type = FieldType.Long, store = true)
private String bookIsbn;

@Column(name="bookTitle")
@Field(type = FieldType.String, store = true)
private String bookTitle;


@Column(name="authorId")
@Field(type = FieldType.Integer, store = true)
private int bookAuthorId;

@Column(name="bookLanguage")
@Field(type = FieldType.String, store = true)
private String bookLanguage;

@Column(name="bookPublisherId")
@Field(type = FieldType.Integer, store = true)
private short bookPublisherId;

@Column(name="bookReleaseDate")
@Field(type = FieldType.Date, store = true, format = DateFormat.custom, pattern="yyyy-MM-dd HH:mm:ss +SSSS")
private Date bookReleaseDate;

@JsonIgnore
@Column(name="bookNoOfChapters")
private short bookNoOfChapters;

@JsonIgnore
@Column(name="bookNoOfPages")
private short bookNoOfPages;

@JsonIgnore
@Column(name="bookNoOfWords")
private int bookWordCount;

@JsonIgnore
@Column(name="bookPrecededByIsbn" )
private long bookPrecededBy;

@JsonIgnore
@Column(name="bookFollowedByIsbn")
private long bookFollowedBy;

@Column(name="bookDescription")
@Field(type = FieldType.String, store = true )
private String bookDescription;

@JsonIgnore
@Column(name="bookCoverImage")
private String bookCover;

我的 Controller

@RequestMapping(value="/isbnSearch" ,method = RequestMethod.POST)
public String isbnSearch(@RequestParam("bookIsbn") String bookIsbn, Model m) {
try{
logger.info("Search for Book by ISBN:" + bookIsbn);
Book book = searchServices.getBookByIsbn(bookIsbn);
int authorId = book.getBookAuthorId();
logger.info("Author ID is " + authorId );
m.addAttribute("Book", book);
return "showBook";
}catch(Exception e){
logger.error("Nested Exception : "+e.getMessage());
m.addAttribute("error", e.getMessage());
return "showError";
}
}

搜索服务器实现

public Book getBookByIsbn(String isbn) {
// try{
Book book = esdao.findBybookIsbn(isbn);
logger.info(""+ book.getBookTitle());
return book;
// }catch(ElasticsearchException ese){
// logger.error("Nested Exception : " + ese.getMessage());
// throw ese;
// }catch(NullPointerException npe){
// logger.error("Accessing NULL Class");
// throw npe;
// }
}

Esdao 仓库

package co.in.searchServer.repository;

import org.elasticsearch.index.query.QueryBuilder;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

import co.in.searchServer.model.Book;

public interface ESDao extends ElasticsearchRepository<Book,String> {
Iterable<Book> search(QueryBuilder arg0);
Book findBybookIsbn(String isbn);
// <S extends Book> S index(S arg0);
}

请给我一些解决这个问题的建议和经验

最佳答案

据我所知,抛出此错误是因为 Java 无法将 json 中的自定义日期格式映射到实体属性 bookReleaseDate 。因此,我们必须添加 @JsonProperty 注释,如下所示:

  @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss +SSSS")
@Column(name="bookReleaseDate")
@Field(type = FieldType.Date, store = true, format = DateFormat.custom, pattern="yyyy-MM-dd HH:mm:ss +SSSS")
private Date bookReleaseDate;

关于spring - 无法将源 {} 映射到实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36087569/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com