0

I'm creating a REST Service in JAva, and below is the code part:

@GET
@Path("/getsummary")
@Produces("application/json")

public Response summary()
{

        VoltDAOImpl voltDao = new VoltDAOImpl();
        Map<String ,List<HashMap<String,String>>> returnList=       voltDao.getOrderDetails("PEAKM" , "Hydra" ,
                 "" ,  voltDao.client,"notional" ,1000);
        List<HashMap<String,String>> totalSlpSummaryList = returnList.get("total_slp_summary");
        List<HashMap<String,String>> totalSlpSummaryBySideList = returnList.get("total_slp_summary_by_side");
        ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();

            String json1 = null;
            String json2 = null;
            try {
                json1 = ow.writeValueAsString(totalSlpSummaryList);
                json2 = ow.writeValueAsString(totalSlpSummaryBySideList);
                System.out.println(json1);
                System.out.println(json2);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


    return Response.status(200).entity(json1).build();
}

Right now, the above is only returning json1 object but I want it to return both json1 and json2. How do I achieve that?

5
  • That's not valid JSON, obviously. If you say you return JSON you need to return valid JSON. For example, wrap the objects into an array. Commented Oct 28, 2016 at 13:57
  • Make a list of the objects you are trying to return, and then return just that. Commented Oct 28, 2016 at 14:00
  • build a DTO object and include json1 and json2 into it and return this DTO Commented Oct 28, 2016 at 14:03
  • 1
    @PaoloMastrangelo Json string Commented Oct 28, 2016 at 14:05
  • Ok sorry i have delete the comment because i have understand it before :) Commented Oct 28, 2016 at 14:06

1 Answer 1

1
String bothJson = "["+json1+","+json2+"]"; //Put both objects in an array of 2 elements
return Response.status(200).entity(bothJson).build();

or

String bothJson = "["+json1+","+json2+"]"; //Put both objects in an array of 2 elements
return Response.getWriter().write(bothJson);

or

String[] str = new String[2];
str[0] = json1;
str[1] = json2;
return Response.status(200).entity(str).build();

Try this, the concept is create a matrix/array and send it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.