我已经编写了一个 REST Web 服务来接受来自客户端的 json 数组,但我得到了java.lang.IndexOutOfBoundsException
谁能告诉我一些解决方案
客户
public class Test {
public static void main(String[] args) {
try{
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
client.addFilter(new LoggingFilter());
HashMap dados=new HashMap();
WebResource service = client.resource("http://localhost:8224/SampleREST/REST/WebService/MyReceive");
JSONObject data= new JSONObject();
dados.put("Name", "John");
dados.put("Age", "30");
dados.put("City", "Tokyo");
data.accumulate("ss", dados);
ClientResponse client_response = service.accept(MediaType.APPLICATION_JSON).post(ClientResponse.class, data);
System.out.println("Status: "+client_response.getStatus());
client.destroy();
}catch(Exception s){s.printStackTrace();}
}
}
服务器
@POST
@Path("/MyReceive")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
//Receive and send a json object
public JSONObject receiveJSONObject(JSONObject json) throws JSONException
{
System.out.println(json.toString());
return json;
}
异常获取
1 * Out-bound request
1 > POST http://localhost:8224/SampleREST/REST/WebService/MyReceive
1 > Accept: application/json
1 >
com.sun.jersey.api.client.ClientHandlerException: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:128)
at com.sun.jersey.api.client.filter.LoggingFilter.handle(LoggingFilter.java:152)
at com.sun.jersey.api.client.Client.handle(Client.java:397)
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:557)
at com.sun.jersey.api.client.WebResource.access$300(WebResource.java:69)
at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:499)
at webService.Test.main(Test.java:35)
Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:604)
at java.util.ArrayList.get(ArrayList.java:382)
at com.sun.jersey.api.client.TerminatingClientHandler.writeRequestEntity(TerminatingClientHandler.java:293)
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler._invoke(URLConnectionClientHandler.java:179)
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:126)
... 6 more