我想将一个JSONObject从restful服务传递给一个restful客户端。这是我的服务代码:
@GET
@Path("/getJson")
@Produces(MediaType.APPLICATION_JSON)
public JSONObject getJson() {
try {
JSONObject json=new JSONObject();
json.put("PoprocessingCode", "123456789");
json.put("TransactionAmount", "0000000");
System.out.println(json);
return json;
} catch (Exception e) {
System.err.println(e.getMessage());
return null;
}
}
这是我宁静的客户的一部分:
Client c = Client.create();
WebResource resource = c.resource("http://localhost:8080/TestJsonService/jaxrs/JsonService/getJson");
JSONObject json=new JSONObject();
json=resource.accept(MediaType.APPLICATION_JSON_TYPE).get(JSONObject.class);
try {
System.out.println("json.getString = "+json.getString("PoprocessingCode"));
} catch (JSONException e1) {
e1.printStackTrace();
}
当我运行它时,我收到此错误:
org.json.JSONException:找不到JSONObject [“PoprocessingCode”]。 at org.json.JSONObject.get(JSONObject.java:459) 在org.json.JSONObject.getString(JSONObject.java:640) 在JsonClient.main(JsonClient.java:30)
我在myeclipse中使用“REST Web服务资源管理器”测试我的服务,并且在服务T中识别出get方法返回此
{}
并且没有任何东西,它是空的,我不知道为什么?!
请指导我以解决它。
感谢...
答案 0 :(得分:1)
Please pass the JsonObject in this form, this will work :
@Path("Excel")
@POST
@GET
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public String ExportToExcel(String jsonString)
And post this using REST Console.
In case of :
JsonObject then use this --->
@Path("/rename")
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Event renameEvent(Event newEvent)
And the object that you pass from the REST CONSOLE should be of following pattern :
{"measures":null,"eventScenarioId":2016,"timeId":20378,"promoPrice":1.979734,"oiAllowancePerunit":0.0,"productId":7158,"scanAllowancePerUnit":0.0,"eventProductStrategy":[{"name":"FND","tsId":4,"tsACV":10.0,"eventProductScenarioId":5069,"id":3},{"name":"TPR","tsId":1,"tsACV":0.0,"eventProductScenarioId":5069,"id":4},{"name":"DISPLAY","tsId":3,"tsACV":2.0,"eventProductScenarioId":5069,"id":2},{"name":"FEATURE","tsId":2,"tsACV":9.0,"eventProductScenarioId":5069,"id":1}],"addEstimatedIncrementalUnits":0.0,"shippedBaseUnits":4365.000000000001,"edShelfPrice":1.979734,"eventFixedCost":0.0,"shippedUnitPct":100.0,"edlpPricePerUnit":0.0,"manuFixedCost":0.0,"customerId":null,"id":5069}