2014-09-01 112 views
3

我正在嘗試創建一個web service,它將採用JSON response,然後使用它查詢數據庫以返回存儲詳細信息(JSON response)。我打算在稍後使用mobile app。但在開發過程中,我正在使用AJAX calls進行測試。我目前正在使用@GET請求。我能夠成功返回一個JSON響應。我現在面臨將JSON Object傳遞給@GET method的問題。在調試時,我發現輸入參數中有一個空值。有人可以看看我的代碼,並建議我做錯了什麼?Web服務的JSON參數

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 Aj 
* 
* 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(); 
     } 

     //TODO return proper representation object 
     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) { 
    } 
} 

這裏是offerProcess類 -

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; 
    } 
} 

對於它的價值,我現在用的是JSON.Simple庫

+0

可能是客戶端的問題。請添加帶有AJAX調用的代碼 – Ilya 2014-09-01 06:28:06

+0

我無法將編輯提交給我的原始帖子,所以在這裏 - 'code var requestData = {「longitude」:「77.681307」, 「latitude」:「12.8250278」, 「IMSI」:「404490585029957」}; $就({ 類型: 「POST」, 的contentType: 「應用程序/ JSON」, URL:URL1, 異步:真, 數據:的RequestData, 成功:功能(響應){ 的console.log (response.status); }' – 2014-09-01 07:57:12

回答

1

我能夠做的一樣,在以下問題來解決我的問題 - Json parameters are passed as null。也如建議,我從@GET請求更改爲@POST

我創建了一個新類,名爲jsonFormat它將採用@POST請求中傳遞的3個參數。

這是我這工作的最終代碼 -

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 javax.ws.rs.POST; 

    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 Aj 
    * 
    * 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() { 
     } 

     @POST 
     @Consumes("application/json") 
     @Produces("application/json") 
     public String getJson(jsonFormat jsonObj) { 

      String[] response = new String[5]; 

      offerProcess ofc = new offerProcess(); 

      try { 

       ofc.setLatitude(jsonObj.latitude); 
       ofc.setLongitude(jsonObj.longitude); 
       ofc.setIMSI(jsonObj.IMSI); 

      } catch (Exception e) { 
       JSONObject ser = new JSONObject(); 

       ser.put("status", "error"); 
       ser.put("reason", jsonObj.latitude); 

       return ser.toJSONString(); 
      } 

      //TODO return proper representation object 
      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", ofc.getIMSI()); 

      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) { 
     } 
    } 

這裏是jsonFormat類我創建 -

import javax.xml.bind.annotation.XmlRootElement; 

    /** 
    * 
    * @author Aj 
    * This class forms the format of the JSON request which will be recieved from the App 
    */ 
    @XmlRootElement 
    public class jsonFormat { 
     public double longitude; 
     public double latitude; 
     public long IMSI; 

     jsonFormat(){} 

     jsonFormat(double longitude,double latitude, long IMSI){ 
      this.longitude = longitude; 
      this.latitude = latitude; 
      this.IMSI = IMSI; 
     } 

    } 

最後,AJAX碼 -

<script type="text/javascript"> 

     var url1 = "http://localhost:8080/Offers/webresources/getOffers"; 

     var requestData = {"longitude": "77.681307", 
      "latitude": "12.8250278", 
      "IMSI": "404490585029957"}; 

     var jsonObj = JSON.stringify(requestData); 

     $.ajax({ 
      type: "POST", 
      contentType: "application/json", 
      url: url1, 
      async: true, 
      data: jsonObj, 
      success: function(response) { 

       //var obj = JSON.parse(response); 
       console.log(response.status); 
       console.log(response.reason); 
       console.log(response.Offers); 
      } 
     }); 
    </script> 

謝謝你f或者你所有的幫助和時間!我希望這對別人有用。

1

嘗試更改爲POST。您不會通過GET請求將JSON正文傳遞給服務器。

4

假設你input放慢參數爲GET請求的查詢參數,那麼你需要@QueryParam註釋添加到參數:

@GET 
    @Consumes("application/json") 
    @Produces("application/json") 
    public String getJson(@QueryParam("input") final String input) { 
      ... 
    } 

編輯:

然而,像@troylshields提到,如果您試圖發送JSON對象,則必須使用POST或PUT(取決於具體情況)。 GET請求只支持查詢參數,並試圖通過查詢參數發送JSON字符串不是一個好主意。

+0

好的,謝謝 - 我已經注意到了!我試着通過修改POST請求。我正在調整代碼並稍後發帖。 此外,是否有任何鏈接你知道我在哪裏可以閱讀這些請求類型? – 2014-09-01 07:12:37

+0

現在使用POST。現在我明白,字符串無法在Web服務中解析,這就是我在POST請求中傳遞的 - 'code requestData = {「東經 「:」 77。 681307" , 「緯度」: 「12.8250278」, 「IMSI」: 「404490585029957」}'。但由於某些原因,參數輸入包含 - 「經度= 77.681307&緯度= 12.8250278&IMSI = 404490585029957」,因此解析失敗,因爲很明顯,這不是JSON。我應該從String更改參數簽名嗎? – 2014-09-01 08:47:13

+0

你能用現在使用的代碼更新問題嗎?我希望看到您所調用方法的簽名。 – 2014-09-01 08:51:52

0

您的客戶代碼不正確。你應該發送請求作爲

var requestData = {"longitude" : "77.681307", "latitude" : "12.8250278", "IMSI": "404490585029957"};  
// ... 
$.ajax({ 
// ... 
    data: {input : requestData} // 'input' should be the root element 
// ... 
)}; 

在這種情況下,你的服務器部分input字符串將是正確的。

而且,因爲我看到你發POST請求,但請求GET預計將在服務器端