Member Avatar for alex9620

Hi,

I am trying to create a web service which will taken in JSON response and then query the DB with it to return store details (JSON response).

I intend to use this with a mobile app later on. But during the development I am testing using AJAX calls. I am using a @GET request at the moment. I was able to successfully return a JSON response. I am now facing a problem with passing the JSON Object to the @GET method. On debugging, I see that there is a null value in my input parameter. Can somebody take a look at my code and advise what I am doing wrong?

import javax.ws.rs.core.Context; import javax.ws.rs.core.UriInfo; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PUT; import java.util.Iterator; import org.json.simple.JSONObject; import org.json.simple.JSONArray; import org.json.simple.JSONValue; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; /** * REST Web Service * * @author Ajay * * This service will return the offers valid for the IMSI number passed */ @Path("getOffers") public class GetOffersResource { @Context private UriInfo context; /** * Creates a new instance of GetOffersResource */ public GetOffersResource() { } @GET @Consumes("application/json") @Produces("application/json") public String getJson(final String input) { JSONParser parser = new JSONParser(); String[] response = new String[5]; try { Object obj = parser.parse(input); JSONObject jsonObject = (JSONObject) obj; offerProcess ofc = new offerProcess(); ofc.setLatitude((double) jsonObject.get("latitude")); ofc.setLongitude((double) jsonObject.get("longitude")); ofc.setIMSI((long) jsonObject.get("IMSI")); response = ofc.fetchOffers(); } catch (ParseException e) { JSONObject ser = new JSONObject(); ser.put("status", "error"); ser.put("reason", "Bad request"); return ser.toJSONString(); } JSONObject ser = new JSONObject(); JSONArray arr = new JSONArray(); arr.add("456TYU"); arr.add("OLED TV"); arr.add("24-JUL-2014"); arr.add("XYZ Enterprises"); arr.add("Gachibowli"); arr.add("9911278366"); ser.put("status", "success"); ser.put("Offers", arr); System.out.println(ser); return ser.toJSONString(); } /** * PUT method for updating or creating an instance of GetOffersResource * * @param content representation for the resource * @return an HTTP response with content of the updated or created resource. */ @PUT @Consumes("application/json") public void putJson(String content) { } } 

Here is the offerProcess class -

public class offerProcess { private double longitude; private double latitude; private long IMSI; public double getLongitude() { return longitude; } public double getLatitude() { return latitude; } public void setLongitude(double longitude) { this.longitude = longitude; } public void setLatitude(double latitude) { this.latitude = latitude; } public long getIMSI() { return IMSI; } public void setIMSI(long IMSI) { this.IMSI = IMSI; } public String[] fetchOffers(){ String[] response = new String[5]; response[0] = "456TYU"; response[1] = "OLED TV"; response[2] = "24-JUL-2014"; response[3] = "XYZ Enterprises"; response[4] = "Gachibowli"; response[5] = "9980556990"; return response; } } 

For what it's worth, I am using the JSON.Simple library.

Member Avatar for alex9620

I changed from @GET request to @POST.

Next, I created a new class called jsonFormat which will take in the 3 parameters passed in the @POST request and changed the signature of my web service method to -

public String getJson(jsonFormat jsonObj) { //logic } 
commented: Thanks for sharing.+14
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.