使用netbeans测试Web服务

时间:2012-05-20 01:31:59

标签: web-services java-ee netbeans jax-rs jackson

我是webservices的新手,我想知道我在做错了什么是我的代码来获取我的所有listCustomers

@Path("/allCustomers")
@GET
@Produces("application/xml")
public List<Customer> listAllCustomers(){
    return customerDao.listAllCustomers();
}

为了测试我的服务,我使用netbeans工具(TEST RESTFUL Web服务),我收到此错误

 Avertissement: StandardWrapperValve[ServletAdaptor]: PWC1406: Servlet.service() for  servlet ServletAdaptor threw exception
 java.lang.NullPointerException
 at com.supinfo.supinbank.service.CustomerService.listAllCustomers(CustomerService.java:45)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)

PS:我不知道是否必须使用XmlRootElement注释客户实体,但我做到了......

2 个答案:

答案 0 :(得分:0)

例外是NullPointerException,这意味着您的对象customerDao为null,这就是您遇到问题的原因。

答案 1 :(得分:0)

经过长时间的搜索后,我发现了一种使用netbeans自动创建webservices的懒惰方法。 首先,我通过使用@WebService注释我的类和使用@WebMethod的方法创建一个简单的Web服务。然后我右键单击我的服务并使用netbeans生成restful服务,结果如下所示:

@GET
@Produces("application/xml")
@Consumes("text/plain")
@Path("listallcustomers/")
public JAXBElement<ListAllCustomersResponse> getListAllCustomers() {
    try {
        // Call Web Service Operation
        if (port != null) {
            java.util.List<com.supinfo.supinbank.service_client.Customer> result = port.listAllCustomers();

            class ListAllCustomersResponse_1 extends com.supinfo.supinbank.service_client.ListAllCustomersResponse {

                ListAllCustomersResponse_1(java.util.List<com.supinfo.supinbank.service_client.Customer> _return) {
                    this._return = _return;
                }
            }
            com.supinfo.supinbank.service_client.ListAllCustomersResponse response = new ListAllCustomersResponse_1(result);
            return new com.supinfo.supinbank.service_client.ObjectFactory().createListAllCustomersResponse(response);
        }
    } catch (Exception ex) {
        // TODO handle custom exceptions here
    }
    return null;
}

现在效果很好......