THIS IS A TEST INSTANCE. ALL YOUR CHANGES WILL BE LOST!!!!
...
You can create a JAX-RS RESTful service by using JAXRSServerFactoryBean:
Code Block | ||||
---|---|---|---|---|
| ||||
JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean(); sf.setResourceClasses(CustomerService.class); sf.setAddress("http://localhost:9000/"); sf.create(); |
A couple things to note:
- The JAXRSServerFactoryBean creates a Server inside CXF which starts listening for requests on the URL specified.
- By default, the JAX-RS runtime is responsible for the lifecycle of resource classes, default lifecycle is per-request. You can set the lifecycle to singleton by using following line:
Code Block java java sf.setResourceProvider(BookStore.class, new SingletonResourceProvider());
- If you prefer not to let the JAX-RS runtime to handle the resource class lifecycle for you (for example, it might be the case that your resource class is created by other containers such as Spring), you can do following:
Code Block java java JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean(); CustomerService cs = new CustomerService(); sf.setServiceBeans(cs); sf.setAddress("http://localhost:9080/"); sf.create();
Configuring the service in container with Spring configuration file.
...