Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

 

 

Wiki Markup
{span:style=font-size:2em;font-weight:bold} JAX-RS (JSR-339) {span}

 

 

Table of Contents

Introduction

...

CXF 3.0.0 completely implements JAX-RS 2.0 including new Client API and has been fully tested against the first JAX-RS 2.0 TCK which became available to Apache (jaxrstck-2.0_26-Feb-2013).

Note: CXF 3.0.0 can not claim at the moment the formal JAX-RS 2.0 compliance due to the fact that the final JAX-RS 2.0 TCK has not been available to Apache.    

 

Existing JAX-RS 1.1 applications can be run with CXF 3.0.0.

...

For example, instead of

Code Block
java
java


public class CustomRequestHandler implements RequestHandler {
    public Response handleRequest(Message message, ClassResourceInfo cri) {
    }
}

public class CustomResponseHandler implements ResponseHandler {
    public Response handleResponse(Message message, OperationResourceInfo ori, Response response) {
    }
}

do

Code Block
java
java


public class CustomRequestFilter implements ContainerRequestHandler {
    public void filter(ContainerRequestContext context) {
        Message message = JAXRSUtils.getCurrentMessage();
        ClassResourceInfo cri = message.getExchange().get(OperationResourceInfo.class).getResourceClass();

        // or consider using JAX-RS 2.0 ResourceInfo context

        // finally use context.abortWith(Response) if you need to block the request 
    }
}

public class CustomResponseFilter implements ContainerResponseFilter {
    public void filter(Message message, OperationResourceInfo ori, Response response) {
        public class CustomRequestFilter implements ContainerRequestHandler {
    public void filter(ContainerRequestContext inContext, ContainerResponseContext outContext) {
        Message message = JAXRSUtils.getCurrentMessage();
        OperationResourceInfo cri = message.getExchange().get(OperationResourceInfo.class);

        // or consider using JAX-RS 2.0 ResourceInfo context 
        
        // finally, work with ContainerResponseContext to modify specific Response properties
    }
}

...

2. CXF org.apache.cxf.jaxrs.ext.form.Form has been dropped, please use JAX-RS 2.0 Form instead. For example, use:

Code Block
java
java

import javax.ws.rs.core.Form;

Form form = new Form().param("a", "b");

instead of

Code Block
java
java

import org.apache.cxf.jaxrs.ext.form.Form;

Form form = new Form().set("a", "b");

...

6. JAX-RS 2.0 introduces a controversial requirement that the default built-in JAX-RS MessageBodyWriter and JAX-RS MessageBodyReader providers are preferred to custom providers supporting the same types unless the custom providers are precisely typed, for example, if you have a custom InputStream reader properly implementing isReadable:

Code Block
java
java

public class MyStreamProvider implements MessageBodyReader<Object> {
    public boolean isReadable(Class<?> cls, ...) {
        return InputStream.class.isAssignableFrom(cls) || Reader.class.isAssignableFrom(cls);
    }
    // other methods
}

...

At CXF level, the users which depend on CXF MultipartProvider to have InputStream or String references to multipart attachments will be affected unless they use @Multipart annotation. For example, if we have a multipart payload with a single part/attachment only then the following code:

Code Block
java
java

@POST
@Consumes("multipart/form-data")
public void upload(InputStream is) {
}

which in CXF 2.7.x or earlier will return a pointer to first/single individual part, will actually return a stream representing the complete unprocessed multipart payload. Adding a @Multipart marker will keep the existing code working as expected:

Code Block
java
java

@POST
@Consumes("multipart/form-data")
public void upload(@Multipart InputStream is) {
}

...

The cxf-rt-frontend-jaxrs dependency is required:

Code Block
xml
xml

   <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-frontend-jaxrs</artifactId>
      <version>3.0.0-milestone1</version>
   </dependency>

...

Optional providers (including the default JSONProvider) are located in this module:

Code Block
xml
xml

   <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-rs-extension-providers</artifactId>
      <version>2.6.0</version>
   </dependency>

The Search extension is now located in

Code Block
xml
xml

   <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-rs-extension-search</artifactId>
      <version>2.6.0</version>
   </dependency>

...

Please note that this bundle has a transitive Maven dependency on the Jetty server modules. If you are using Maven and working with other servlet containers such as Tomcat then please add the following exclusion:

Code Block
xml
xml

   <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-bundle-jaxrs</artifactId>
      <version>${cxf.version}</version>
      <exclusions>
          <exclusion>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-server</artifactId>
          </exclusion>
      </exclusions> 

   </dependency>

...

The runtime will replace '.xml' or '.en' with an appropriate header value. For it to know the type or language value associated with a given URI suffix, some configuration needs to be done. Here's an example of how it can be done with Spring:

Code Block
xml
xml


  <jaxrs:server id="customerService" address="/">
    <jaxrs:serviceBeans>
      <bean class="org.apache.cxf.jaxrs.systests.CustomerService" />
    </jaxrs:serviceBeans>
    <jaxrs:extensionMappings>
      <entry key="json" value="application/json"/>
      <entry key="xml" value="application/xml"/>
    </jaxrs:extensionMappings>
    <jaxrs:languageMappings>
       <entry key="en" value="en-gb"/>  
    </jaxrs:languageMappings>
  </jaxrs:server>

...

Many of the existing CXF features can be applied either to jaxrs:server or jaxrs:client. For example, to enable logging of requests and responses, simply do:

Code Block
xml
xml

<beans xmlns:cxf="http://cxf.apache.org/core" 
   xsi:schemaLocation="http://cxf.apache.org/core 
      http://cxf.apache.org/schemas/core.xsd">
<jaxrs:server>
<jaxrs:features>
     <cxf:logging/>
</jaxrs:features>
<jaxrs:server>
</beans>

...