使用Spring Data Elasticsearch从弹性搜索中读取文档时出现以下错误:
错误SearchController - 嵌套异常:无法将源{文档数据}映射到Book
@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;
我的控制器
@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";
}
}
SearchServerImpl
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);
}
请提供一些解决此问题的建议和经验
答案 0 :(得分:1)
我理解这个错误是因为Java无法将json中的自定义日期格式映射到实体属性bookReleaseDate
。因此我们必须添加@JsonProperty Annotation如下:
@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;