Web服务的java JSON参数
我试图创建一个web service
,它将接收JSON response
,然后用它查询数据库以返回存储详细信息(JSON response)
我打算稍后将其用于mobile app
。但是在开发过程中,我使用AJAX calls
进行测试。我现在正在使用@GET
请求。我能够成功地返回JSON响应。我现在面临着将JSON Object
传递给@GET method
的问题。调试时,我看到输入参数中有一个空值。谁能看看我的代码,告诉我哪里做错了
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。简单库
# 1 楼答案
我可以通过以下问题中的相同操作来解决我的问题-Json parameters are passed as null。同样按照建议,我从
@GET
请求更改为@POST
我创建了一个名为jsonFormat的新类,它将接受在
@POST
请求中传递的3个参数这是我最后的代码
下面是我创建的jsonFormat类-
最后,AJAX代码
谢谢你的帮助和时间!我希望这对某人有用
# 2 楼答案
试着换个帖子。您不会通过GET请求将JSON正文传递给服务器
# 3 楼答案
你的客户代码不正确。您应该将请求作为
在这种情况下,在服务器部件上
input
字符串将是正确的另外,正如我看到的,您发送了
POST
请求,但服务器端需要GET
请求