I have a problem using the jax-rs API using the following code:

At the RESTful server I defined a Customer Service Class as follows:
@Path("/customerservice/")
public class CustomerService implements CustomerServiceInterface {
    long currentId = 123;
    Map<Long, Customer> customers = new HashMap<Long, Customer>();
    Map<Long, Order> orders = new HashMap<Long, Order>();

    public CustomerService() { 
        init();
    }
    
    @GET 
    public Map<Long, Customer> getCustomers(){
        return this.customers;
        
    }

    @GET
    @Path("/customers/{id}/")
    public Customer getCustomer(@PathParam("id") String id) {
        long idNumber = Long.parseLong(id);
        Customer c = customers.get(idNumber);
        return c;
    }
...
}

At the client side, I developed two applications:

1) First one which uses the old API:

        System.out.println("Sent HTTP GET request to query all customers info");
        URL url1 = new 
URL("http://localhost:8080/CustomerRestfullTest/jaxrs/customerservice/";);
        InputStream in1 = url1.openStream();
        System.out.println(getStringFromInputStream(in1));

 // Sent HTTP GET request to query customer info
        System.out.println("Sent HTTP GET request to query customer info having 
the id 123");
        URL url = new 
URL("http://localhost:8080/CustomerRestfullTest/jaxrs/customerservice/customers/123";);
        InputStream in = url.openStream();
        System.out.println(getStringFromInputStream(in));


This one works without any error

2) Second one which uses the JAX-RS API:

                CustomerServiceInterface s = 
JAXRSClientFactory.create("http://localhost:8080/CustomerRestfullTest/jaxrs/";, 
CustomerServiceInterface.class);
                Customer m = s.getCustomer("123");
                System.out.println("the result is m ID: "+ m.getId() + " m 
NAME: "+m.getName());
                
                Map<Long, Customer> map= s.getCustomers();
                System.out.println("Map is "+map);



which gives an error when executing s.getCustomers().
I can't understand the cause of this error. I tried to put a path to the method 
getCustomers() but it doesn't solve the problem.
Any idea please?

 The result of the execution is :

the result is m ID: 123 m NAME: John
Exception in thread "main" Status : 500
Headers : 
Content-Type : text/plain
Transfer-Encoding : chunked
Date : Tue, 12 Jun 2012 16:55:52 GMT
Connection : close
X-Powered-By : Servlet/3.0
Server : GlassFish Server Open Source Edition 3.0.1
Error message : 
javax.ws.rs.WebApplicationException: java.lang.NullPointerException
        at 
org.apache.cxf.jaxrs.provider.DataBindingProvider.writeTo(DataBindingProvider.java:101)
        at 
com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:289)
        at 
com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1029)
        at 
com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:941)
        at 
com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:932)
        at 
com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:384)
        at 
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:451)
        at 
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:632)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
        at 
org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
        at 
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
        at 
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
        at 
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
        at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
        at 
com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
        at 
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
        at 
org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:325)
        at 
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:226)
        at 
com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)
        at 
com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
        at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
        at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
        at 
com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
        at 
com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
        at 
com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
        at 
com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
        at 
com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
        at 
com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
        at 
com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
        at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
        at 
com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
        at 
com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
        at java.lang.Thread.run(Thread.java:680)
Caused by: java.lang.NullPointerException
        at 
org.apache.cxf.jaxrs.provider.DataBindingProvider.writeToWriter(DataBindingProvider.java:106)
        at 
org.apache.cxf.jaxrs.provider.DataBindingProvider.writeTo(DataBindingProvider.java:99)
        ... 32 more


        at 
org.apache.cxf.jaxrs.client.ClientProxyImpl.checkResponse(ClientProxyImpl.java:250)
        at 
org.apache.cxf.jaxrs.client.ClientProxyImpl.handleResponse(ClientProxyImpl.java:517)
        at 
org.apache.cxf.jaxrs.client.ClientProxyImpl.doChainedInvocation(ClientProxyImpl.java:487)
        at 
org.apache.cxf.jaxrs.client.ClientProxyImpl.invoke(ClientProxyImpl.java:188)
        at $Proxy18.getCustomers(Unknown Source)
        at demo.client.Client1.main(Client1.java:17)



On 12 juin 2012, at 15:49, Sergey Beryozkin wrote:

> Hi
> On 12/06/12 14:33, Diana ALLAM wrote:
>> Hello,
>> 
>> Actually I am using the following code example to request a POST method on 
>> my Restful service:
>> -----------------------------------------------------------------------------------------------------------------------------------------------
>>         System.out.println("Sent HTTP POST request to add customer");
>>         inputFile = 
>> client.getClass().getResource("add_customer.xml").getFile();
>>         resolver = new URIResolver(inputFile);
>>         input = new File(resolver.getURI());
>>         PostMethod post = new 
>> PostMethod("http://localhost:8080/CustomerRestfullTest/jaxrs/customerservice/customers";);
>>         post.addRequestHeader("Accept" , "text/xml");
>>         entity = new FileRequestEntity(input, "text/xml; 
>> charset=ISO-8859-1");
>>         post.setRequestEntity(entity);
>>         httpclient = new HttpClient();
>> 
>>         try {
>>             int result = httpclient.executeMethod(post);
>>             System.out.println("Response status code: " + result);
>>             System.out.println("Response body: ");
>>             System.out.println(post.getResponseBodyAsString());
>>         } finally {
>>             // Release current connection to the connection pool once you are
>>             // done
>>             post.releaseConnection();
>>         }
>> -----------------------------------------------------------------------------------
>> My request is thus build by using an XML data file "add-customer.xml" where 
>> I can put for example:
>> <Customer>
>>   <id>1</id>
>>   <name>toto</name>
>> </Student>
>> 
>> I would like to do a similar request but instead of using the xml file I 
>> prefer to have a java class,
>> "Customer", and to do then something like that:
>> Customer c = new Customer(1, "toto");
>> PostMethod post = new 
>> PostMethod("http://localhost:8080/CustomerRestfullTest/jaxrs/customerservice/customers";);
>> post.setRequest(c);
>> 
>> Is there a way to do that in cxf?
>> 
> The above is an old example code which uses HttpClient, try this api instead:
> 
> http://cxf.apache.org/docs/jax-rs-client-api.html
> 
> Cheers, Sergey
>> Regards,
>> 
>> Diana
>> 
>> 
> 
> 
> -- 
> Sergey Beryozkin
> 
> Talend Community Coders
> http://coders.talend.com/
> 
> Blog: http://sberyozkin.blogspot.com

Reply via email to