Java service return the Result message along with Object class

Mohan vel :

Hi i have to return the ResultMessage along with my object class in my below code.

public List<EmployeeInformation> findAll(String IndexName) throws Exception {

        String ResultMessage = new String();
        if (Strings.isNullOrEmpty(IndexName)) {
            ResultMessage= "Index name is null or empty";
        } else if (isTextContainUpperCase(IndexName)) {
            ResultMessage= "Index name should be in lowercase";
        } else if (!checkEsConnection(client)) {
            ResultMessage= "Elasticsearch deployment is not reachable";
        }
        SearchRequest searchRequest = new SearchRequest(IndexName);
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        searchRequest.source(searchSourceBuilder);
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        return getSearchResult(searchResponse);
    }



private List<EmployeeInformation> getSearchResult(SearchResponse response) {

        SearchHit[] searchHit = response.getHits().getHits();

        List<EmployeeInformation> EmployeeDocuments = new ArrayList<>();

        for (SearchHit hit : searchHit) {
            EmployeeDocuments
                    .add(objectMapper
                            .convertValue(hit
                                    .getSourceAsMap(), EmployeeInformation.class));
        }

        return EmployeeDocuments;
    }

What is the best way to return the ResultMessage along with my employeeinformation object. Could some one Modify the above code accordingly and post your answer or suggest me how to achieve this? thanks in advance

Deepak Kharpuriya :

You can return ImmutablePair<ResultMessage, List<EmployeeInformation>>. This objects then be accessed by getLeft() and getRight() methods. However, you will need org.apache.commons lib for this. It is worth adding this lib as it has many more such utils and wrapper classes. https://mvnrepository.com/artifact/org.apache.commons/commons-lang3

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=420268&siteId=1