如何访问流中的获取器和设置器

我有一个方法,可以采用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不是功能性界面”,我怎么能让它工作。


繁星coding
浏览 76回答 1
1回答

侃侃尔雅

您不需要该操作,a可以在您的案例中工作。在这种情况下,s的使用也不是非常有效。您可以简单地编写一个循环并执行以下操作:mapforEachstreamforfor (Product product : products) {&nbsp; &nbsp; String json = objectMapper.writeValueAsString(product);&nbsp; &nbsp; // handle the exception as well above&nbsp; &nbsp; createDocument(ElasticSearchConstants.PRODUCTS_INDEX,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ElasticSearchConstants.PRODUCTS_DOCUMENT_TYPE,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; json, product.getPartNo());}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java