REST Style Services with the HTTP Binding
CXF includes an "HTTP binding" which makes it easy to build REST style services. The HTTP binding allows you to take any operation and map it to arbitrary URIs and HTTP verbs (i.e. GET, PUT, POST, DELETE).
Convention based services
If you have a simple CRUD based Java class, CXF can try to build up a set of resources automatically for you with no annotations or configuration. This is best explained through an example. Lets take a look at a typical CRUD class:
...
The same rules here as #2 and #4 except we're looking for "delete" and "remove" in the operation and mapping it to an HTTP DELETE.
Configuring the service
You can create a service which uses the HTTP binding by using JaxWsFactoryBean:
...
- The JaxWsServerFactory bean creates a Server inside CXF which starts listening for requests on the URL specified.
- The HttpBindingInfoFactoryBean is responsible for configuring how your service operations get mapped to resources.
- We're telling the ServiceFactory to work in "wrapped" mode. This just means that our xml documents will be wrapped with the operation name, allowing us to have multiple input parameters. More on this further down.
Java REST Annotations
The Java REST Annotations are annotations which provide information to CXF on how to map operations to arbitrary URI/HTTP verb combinations.
...
This will allow users to do a POST to the /customers URI with their document and add it to the collection of customers contained there.
Configuring the Service
Configuration for JRA style services is exactly the same as the convention based services. However, in this example, the service is not in "wrapped" mode. So the configuration is slightly different as we don't need to explicitly set the wrapped setting:
Code Block | ||||
---|---|---|---|---|
| ||||
JaxWsServerFactoryBean sf = new JaxWsServerFactoryBean(); sf.setServiceClass(CustomerService.class); sf.setBindingFactory(new HttpBindingInfoFactoryBean()); sf.setAddress("http://localhost:9001/"); CustomerService customerService = new CustomerServiceImpl(); sf.getServiceFactory().setInvoker(new BeanInvoker(customerService)); Server svr = sf.create(); |
Wrapped vs. Unwrapped Mode
TODO