2017-08-23 90 views
0

当我进入我的网址http://localhost:8081/projectName/pathh/param告诉我的JSON对象创建获取响应JSON对象的RESTful Web服务的Java

"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状态500 - java.lang.IllegalArgumentException异常:错号码的 参数

类型异常报告

消息java.lang.IllegalArgumentException异常:参数数量错误

description s erver遇到了一个内部错误,该错误阻止了它实现此请求 。

+1

你在同一个方法中有两个'@Path'注解吗? – alayor

+0

是的,我有那个注释..在调试模式下一切都没问题,但与回报线似乎有一些错误.. –

+0

我发现问题..我应该把toString()方法后jsonarray返回响应。.STATUS(200).entity(jsonarray.toString())建立(); –

回答

0

如果你可以调试它并找到它失败的那一行,或者它永远不会进入方法,并且你的应用程序连线/注释有问题,它将会有所帮助。

这么说,我怀疑如果你删除或注释掉

System.out.println(Response.status(200).entity(jsonMain).build()); 

它会奏效。

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

}

我用球衣作为JAX-RS API实现。球衣的配置可以在这里的教程中找到。 jersey-server和jersey-servlet是作为依赖包含的,web.xml有一个用于jersey servlet映射的条目。