我正在尝试使用 Spring Boot 连接到我的外部 ElasticSearch 服务器。
如果我从命令行执行 curl,我会得到预期的结果。
curl "http://ipAddr:9200/indexName/TYPE/_search?pretty=true"
但是当我尝试通过 Spring Boot 访问它时出现此错误。
<html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>Mon Sep 11 12:39:15 IST 2017</div><div>There was an unexpected error (type=Internal Server Error, status=500).</div><div>Could not write JSON: (was java.lang.NullPointerException); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: java.util.ArrayList[0]->org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl["facets"])</div></body></html>
不确定为什么会出现 NullPointerException
以及什么是 aggregartion.impl
这是我的 Spring 应用程序:
Controller :
@RestController
public class PojoController {
@Autowired
PojoService pojoService;
@RequestMapping(value = "/", method=RequestMethod.GET)
public @ResponseBody String index() {
return new String("Welcome:)");
}
@RequestMapping(value = "/all", method = RequestMethod.GET,
produces = { MediaType.APPLICATION_JSON_VALUE })
@ResponseBody List<POJO> findAll() {
try {
List<POJO> pojoObj = pojoService.findAll();
return pojoObj;
} catch (Exception exp) {
exp.printStackTrace();
return null;
}
}
}
存储库:
@Repository
public interface PojoRepository extends ElasticsearchRepository<POJO, Integer> {
List<POJO> findAll();
}
服务:
@Service
public class POJOServiceImpl implements POJOService{
private POJORepository pojoRepository;
private ElasticsearchTemplate elasticsearchTemplate;
@Autowired
public void setPojoRepository(PojoRepository pojoRepository) {
this.pojoRepository = pojoRepository;
}
public POJO findOne(String id) {
return pojoRepository.findOne(id);
}
public List<POJO> findAll() {
return (List<POJO>) pojoRepository.findAll();
}
}
POJO 类:
@Document(indexName = "INDEX", type = "TYPE")
public class POJO {
@Id
private Integer id;
private String name;
public POJO(){
// empty
}
public POJO(Integerid, String name) {
super();
this.id = id;
this.name = name;
}
// getters and setters
}
我应该可以查询索引中的所有文档。稍后,我会尝试使用过滤器等。
感谢任何帮助。谢谢:)
最佳答案
看起来 Jackson 在处理您的 POJO 时遇到了问题(可能与此问题有关:DATAES-274)- 有问题的部分是将存储库中的 Iterable
集合转换为 List
。
更新
对于存储库,spring-data-elasticsearch 的行为与您预期的有点不同。以你的例子为例:
@Repository
public interface PojoRepository extends ElasticsearchRepository<POJO, Integer> {
List<POJO> findAll();
}
在调用你的 rest Controller 之后:
List<POJO> pojoObj = pojoService.findAll();
在调试器中你会看到这样的东西:
您会期望 pojoObj
列表包含 POJO 类的对象。
惊喜来了 - pojoObj
ArrayList 包含一个 AggregatedPageImpl
类型的对象,它的 content
字段是包含您的 POJO 对象的正确列表。
这就是你得到的原因:
Could not write JSON: ... java.util.ArrayList[0]->org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl[\"facets\"])
如我之前所写,Jackson 在序列化 POJO 对象时无法处理此问题。
解决方案一
让存储库返回 Iterable
集合(默认情况下)。
@Repository
public interface PojoRepository extends ElasticsearchRepository<POJO, Integer> {
}
将转换部分移到服务中,但使用一些实用方法(这里使用 Guava)以使其像这样:
import com.google.common.collect.Lists;
public List<POJO> findAll() {
return Lists.newArrayList(pojoRepository.findAll());
}
方案二
在存储库中使用Page
(这里是没有参数的简化版本):
@Repository
public interface PojoRepository extends ElasticsearchRepository<POJO, Integer> {
Page<TestDto> findAll();
}
如果您仍想对列表进行操作 - 从服务中的页面获取内容:
public List<POJO> findAll() {
return testDtoRepository.findAll().getContent();
}
关于spring - 使用 spring-data-elasticsearch 从索引中获取所有文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46150275/