Here is the process which I have tried and got the result. I have implemented below code in my chaincode.
private Response getQueryHistory(ChaincodeStub chaincodeStub, List<String> args) {
// method for getting the history for key
QueryResultsIterator<KeyModification> queryResults = chaincodeStub.getHistoryForKey(args.get(0));
return newSuccessResponse(prepareJsonFromQueryResult(queryResults));
}
private String prepareJsonFromQueryResult(QueryResultsIterator<KeyModification> queryResults) {
// here build your json object from query result
return "your json object";
}
There are several already implemented method of ChaincodeStub
For more information of method, have look https://hyperledger.github.io/fabric-chaincode-java/release-1.4/api/org/hyperledger/fabric/shim/ChaincodeStub.html