【发布时间】:2017-09-11 07:25:01
【问题描述】:
我正在尝试使用 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 应用程序:
控制器:
@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
}
我应该能够查询索引中的所有文档。稍后,我将尝试使用过滤器等。
感谢任何帮助。谢谢:)
【问题讨论】:
标签: spring elasticsearch spring-data-elasticsearch