当我访问我的 URL http://localhost:8081/projectName/pathh/param 时向我展示创建的 JSON 对象
"Employee": [{
"id": "param"
}]
这是我的 Java 代码。我使用 Eclipse + Tom Cat 服务器。
@Path("/pathh")
@GET
@Path("{parameter}")
public Response getJSONObj(@PathParam("parameter") String parameter) {
JSONObject jsonObj = new JSONObject();
JSONArray jsonarray = new JSONArray();
jsonObj.put("id", parameter);
jsonarray.put(jsonObj);
System.out.println(jsonarray);
JSONObject jsonMain = new JSONObject();
jsonMain.put("Employee", jsonarray);
System.out.println(jsonMain.toString());
System.out.println(Response.status(200).entity(jsonMain).build());
return Response.status(200).entity(jsonMain).build();
}
我收到这个错误:
HTTP Status 500 - java.lang.IllegalArgumentException: wrong number of arguments
type Exception report
message java.lang.IllegalArgumentException: wrong number of arguments
description The server encountered an internal error that prevented it from fulfilling this request.
最佳答案
package com.tests;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
@Path("/path/{parameter}")
public class Test1 {
@GET
public Response getJSONObj(@PathParam("parameter") String parameter) {
JsonObject jsonObj = new JsonObject();
JsonArray jsonarray = new JsonArray();
jsonObj.addProperty("id", parameter);
jsonarray.add(jsonObj);
System.out.println(jsonarray);
JsonObject jsonMain = new JsonObject();
jsonMain.add("Employee", jsonarray);
System.out.println(jsonMain.toString());
System.out.println(Response.status(200).entity(jsonMain).build());
return Response.status(200).entity(jsonMain.toString()).build();
}
}
我使用 Jersey 作为 Jax-RS API 实现。 jersey-server 和 jersey-servlet 作为依赖项包含在内,并且 web.xml 有一个用于 jersey servlet 映射的条目。
关于java - 获取响应 JSON 对象 webservice RESTful java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45848585/