JAX-RS Advanced FeaturesPage added by Sergey Beryozkin
JAX-RS : Advanced Features
JMS SupportCXF has been designed such that multiple transports can be supported for a given endpoint. If you would like your JAXRS endpoint be capable of serving not only HTTP but also JMS requests then you need to specify a JMS transportId, example : <jaxrs:server serviceName="s:BookService" transportId="http://cxf.apache.org/transports/jms" address="/"> <jaxrs:serviceBeans> <bean class="org.apache.cxf.systest.jaxrs.JMSBookStore"/> </jaxrs:serviceBeans> </jaxrs:server>
If JMS messages are sent to topic destinations then one has to either set a "OnewayMessage" property or ensure that target JAXRS methods are annotated with org.apache.cxf.jaxrs.ext.Oneway. As far as REQUEST_URI is concerned, it is initially matched against a jaxrs:server/@address. So if REQUEST_URI is not set or set to "/" then jaxrs:server/@address has to be set to "/". If REQUEST_URI is set to "/bar/foo" and By referencing a bean such as 'org.apache.cxf.systest.jaxrs.JMSBookStore' from multiple jaxrs endpoints you can ensure that both HTTP and JMS requests are handled by the same service bean. In such cases you may want to use a CXF JAXRS specific ProtocolHeaders context which will let you get either HTTP or JMS headers. Oneway invocationsResource methods with an org.apache.cxf.jaxrs.ext.Oneway annotation will be invoked oneway with the original request returning 202 HTTP status. HTTP or JMS clients can also add a "OnewayRequest" header if adding Oneway annotations is not an option. RESTful services without annotationsOne of the latest CXF JAX-RS extensions allows users to provide external models with the information which the runtime typically gets from JAX-RS annotations like @Path, @PathParam, @Consumes, @Produces, etc. User model schema type is described in the jaxrs.xsd. The top-level 'model' element can have 'resource' children elements. A 'resource' element describes a resource class which can be either a root resource class or a sub-resource one and it can have attributes describing 'path', 'produces' and 'consumes' values and it has a 'name' attribute which identifies a fully-qualified resource class. Starting from CXF 2.3.2-SNAPSHOT a "oneway" attribute can also be applied to individual operations. Here is an example : <model xmlns="http://cxf.apache.org/jaxrs"> <resource name="org.apache.cxf.systest.jaxrs.BookStoreNoAnnotations" path="bookstore" produces="application/json" consumes="application/json"> <operation name="getBook" verb="GET" path="/books/{id}" produces="application/xml"> <param name="id" type="PATH"/> </operation> <operation name="getBookChapter" path="/books/{id}/chapter"> <param name="id" type="PATH"/> </operation> <operation name="updateBook" verb="PUT"> <param name="book" type="REQUEST_BODY"/> </operation> </resource> <resource name="org.apache.cxf.systest.jaxrs.ChapterNoAnnotations"> <operation name="getItself" verb="GET"/> <operation name="updateChapter" verb="PUT" consumes="application/xml"> <param name="content" type="REQUEST_BODY"/> </operation> </resource> </model> This model describes two resources, BookStoreNoAnnotations and ChapterNoAnnotations. The BookStoreNoAnnotations resource has three resource operations, 'getBook', 'getBookChapter' and 'updateBook'. Note that the 'getBookChapter' operation element (described in the model) has no 'verb' attribute so runtime will identify it as a subresource locator. Additionally the BookStoreNoAnnotations resource declares that all its resource methods produce 'application/json' mediaTypes, while its 'getBook' method overrides its with its own 'produces' value. BookStoreNoAnnotations resource also has a 'consumes' attribute which requires all of the resource methods (such as 'updateBook') to consume "application/json" formats. The ChapterNoAnnotations 'updateChapter' resource operation requires 'application/xml' formats. You can use a comma-seperated list of media type values if needed, for example, produces("application/xml;charset=utf-8,application/json") or consumes("application/xml;charset=utf-8,application/json"). Please also see this model file for an example. Providing this file will let all implementations of the interface described in this model instance be exposed as RESTful services supported by the JAX-RS runtime. ConfigurationA user model can be referenced in a number of ways. It can be embedded in a jaxrs:server endpoint definition or linked to through a jaxrs:server modelRef attribute as a classpath resource. Please see this bean Spring configuration file, look at jaxrs server beans with 'bookservice6' and 'bookservice7' names. Note that when registering a model from Spring you do not need to declare a jaxrs server serviceBeans section - the runtime will instantiate the beans itself. If you do need to inject certain properties into your service bean from Spring then you do need to declare a service bean too. In this case this bean will be instantiated twice - once by the runtime during the model introspection and once by Spring, however in the end it will be the bean created by Spring that will be used, the one created by the runtime will be removed. Please have a look at this Spring bean. The jaxrs endpoint with id 'bookservice2' will have BookStoreWithNoAnnotations created twice but it will be the Spring created BookStoreWithNoAnnotations bean that will serve as a resource class instance. The jaxrs endpoint with id 'bookservice3' will have BookStoreWithNoAnnotationsImpl class instantiated only by Spring, with the model describing BookStoreWithNoAnnotationsInterface only that this class implements. You can also register a model programmatically, for example : JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean(); sf.setAddress("http://localhost:9080/"); String modelRef = "classpath:/org/apache/cxf/systest/jaxrs/resources/resources2.xml"; sf.setModelRef(modelRef); // or if you have interface classes described in the model already loaded, ex : OSGI // sf.setModelRefWithServiceClass(modelRef, BookStoreNoAnnotationsInterface.class); // register an actual bean only if the model describes interfaces sf.setServiceBeans(new BookStoreNoAnnotationsImpl()); Please also see this system test for the example of how model beans like UserResource can be created and registered programmatically. Similarly, you can register a user model on the client side, either from jaxrs:client or programmatically, example : JAXRSClientFactoryBean cf = new JAXRSClientFactoryBean(); cf.setAddress("http://localhost:9080/"); String modelRef = "classpath:/org/apache/cxf/systest/jaxrs/resources/resources2.xml"; sf.setModelRef(modelRef); BookStoreNoAnnotations proxy = cf.create(BookStoreNoAnnotations.class); At the moment it is only possible to register a user model with CXFNonSpringJAXRSServlet using the latest 2.2.3-SNAPSHOT like the way it is done in this web.xml. See CXFServlet3 and CXFServlet4 servlet declarations. Note that CXFServlet4 registers a model containing interfaces so it also registers a BookStoreNoAnnotationsImpl service class. The workaround is to create a custom servlet : public class JAXRSUserModelServlet extends CXFNonSpringJaxrsServlet { @Override public void loadBus(ServletConfig servletConfig) throws ServletException { super.loadBus(servletConfig); JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean(); String address = servletConfig.getInitParameter(SERVICE_ADDRESS_PARAM); //jaxrs.address if (address == null) { address = "/"; } sf.setAddress(address); // modelRef needs to start from 'classpath:', ex 'classpath:/WEB-INF/models/model1.xml String modelRef = servletConfig.getInitParameter("user.model"); sf.setModelRef(modelRef); sf.create(); }
Change Notification Preferences
View Online
|
Add Comment
|
- [CONF] Apache CXF Documentation > JAX-RS Advanced Features confluence
