Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在RESTfulWebService中传递JSONObject_Java_Json_Rest - Fatal编程技术网

Java 在RESTfulWebService中传递JSONObject

Java 在RESTfulWebService中传递JSONObject,java,json,rest,Java,Json,Rest,我正在使用RestFul-Webservice和JBoss-Server部署应用程序,将JSONObject接收到我的web服务,以测试我是否创建了web服务,并为其编写了测试用例。现在我在将JSONObject从测试用例传递到web服务时挂断了,当我将json对象传递给@post service调用它响应空指针异常时,即使我尝试向它传递字符串,它也会响应空值。 我在webservice @consumes({Mediatype.APPLICATION_JSON}) @Consumes("ap

我正在使用
RestFul-Webservice
JBoss-Server
部署应用程序,将
JSONObject
接收到我的
web服务
,以测试我是否创建了
web服务
,并为其编写了测试用例。现在我在将
JSONObject
从测试用例传递到web服务时挂断了,当我将json对象传递给
@post service
调用它响应
空指针异常
时,即使我尝试向它传递字符串,它也会响应
值。 我在
webservice

@consumes({Mediatype.APPLICATION_JSON})

@Consumes("application/json")
测试用例为:

    @Test
   public void testgetmsg() {
    String msg = "{\"patient\":[{\"id\":\"6\",\"title\":\"Test\"}]}";
    try {
        JSONObject obj = new JSONObject(new JSONTokener(msg));
            WebResource resource = client.resource( "https://localhost:8443/../../resources/create");
        ClientResponse response = resource.type(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON).
                        entity(obj).post(ClientResponse.class,JSONObject.class);
                 }
    }
有人能指导我进一步吗


提前感谢

您不需要创建json对象,只需传递字符串即可

你应该

 ClientResponse response = resource.type(MediaType.APPLICATION_JSON)
                                   .accept(MediaType.APPLICATION_JSON)
                                   .post(ClientResponse.class, msg);
您可以使用firefox插件(POSTER)将值作为json或formparam传递,而不是使用代码客户端

  // create new student SERVER
   //class

   @POST
   @Path("post")
   @Consumes(MediaType.APPLICATION_JSON)
   @Produces(MediaType.APPLICATION_JSON)
   public Response createStudent(Student st) {
       // add new student 
       StudentDAO.instance.getModelStudent().put("8", st);
       // response status code
       return Response.status(200).entity(st).build();
    }
  // create new student SERVER
   //class

   @POST
   @Path("post")
   @Consumes(MediaType.APPLICATION_JSON)
   @Produces(MediaType.APPLICATION_JSON)
   public Response createStudent(Student st) {
       // add new student 
       StudentDAO.instance.getModelStudent().put("8", st);
       // response status code
       return Response.status(200).entity(st).build();
    }