我有一个方法,可以采用json对象并将其插入到aws弹性搜索服务中,但我想编写另一个方法,该方法采用多个json对象并将其插入到aws弹性搜索中
/**
* Create the Product in ElasticSearch
*
* @param product The Product
* @return The response string
* @throws JsonProcessingException Throws JsonProcessingException when response cannot be parsed
*/
public String createNewProduct(Product product) throws JsonProcessingException {
final ObjectMapper objectMapper = new ObjectMapper();
final String json = objectMapper.writeValueAsString(product);
if (json != null) {
AwsResponse response = createDocument(ElasticSearchConstants.PRODUCTS_INDEX,
ElasticSearchConstants.PRODUCTS_DOCUMENT_TYPE,
json,
product.getPartNo());
// Creating a new document not seen before results in a 201 status, where as overwriting a previous document results in a 200
if (response != null && (response.getHttpResponse().getStatusCode() == HttpStatus.CREATED.value()
|| response.getHttpResponse().getStatusCode() == HttpStatus.OK.value())) {
LOGGER.info("Successfully created new Product", product.getPartNo(), product.getLevelOne());
return product.getPartNo();
}
}
return null;
}
/**
* Create new Products in ElasticSearch
* @param products The product
* @throws JsonProcessingException Throws JsonProcessingException when response cannot be parsed
*/
public String createNewProducts(ArrayList<Product> products) throws JsonProcessingException{
final ObjectMapper objectMapper = new ObjectMapper();
products.stream()
.map(product -> {
try {
return objectMapper.writeValueAsString(product);
} catch (JsonProcessingException e) {
return new JsonProcessingException(e){};
}
})
如您所见,createDocument方法需要4个参数,其中4个是字符串 第一种方法工作得很好,但第二种方法有问题。当我试图得到partNo's时,它给了我一个错误,因为正常的“void不是功能性界面”,我怎么能让它工作。
侃侃尔雅
相关分类