2013-01-03 43 views
0

我使用RestFul WebserviceJBoss Server部署的应用接收JSONObjectweb service,来测试我创建了web service和笔试情况下它。现在我得到了传递JSONobject挂了从测试用例到Web服务,当我将json对象传递给@post service时,调用它的响应即为Null Pointer Exception,即使我尝试将字符串传递给它,它也会响应null值。 我用Annotationswebservice在RESTful Web服务传递的JSONObject

@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); 
       } 
    } 

任何机构可以引导我继续办理?

由于提前

回答

0

你并不需要创建JSON对象,你可以把这个字符串。

你应该

ClientResponse response = resource.type(MediaType.APPLICATION_JSON) 
            .accept(MediaType.APPLICATION_JSON) 
            .post(ClientResponse.class, msg); 
0
//CLIENT 

public static void createStudent() { 
    String input = "{\"id\":12,\"firstName\":\"Fade To Black\",\"lastName\":\"Joy\"}"; 
    ClientResponse response = service.path("class/post") 
      .type("application/json").post(ClientResponse.class, input); 
    System.out.println("Output from Server .... \n"); 
    String output = response.getEntity(String.class); 
    System.out.println(output); 
    System.out.println(response.getStatus()+ "OK"); 
} 

而不是使用客户端的代码,你可以使用在Firefox(海报)增加值传递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(); 
    }