JAX-RS and JAX-WS
JAX-RS and JAX-WS
Here's a beans.xml showing how to have a single service class supporting both SOAP and REST-based invocations at the same time with the help of JAX-WS and JAX-RS :
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<!-- JAX-RS -->
<jaxrs:server id="customerService" address="/">
<jaxrs:serviceBeans>
<ref bean="customerService" />
</jaxrs:serviceBeans>
</jaxrs:server>
<!-- JAX-WS -->
<jaxws:endpoint implementor="#customerService"
address="/CustomerWorld" wsdlLocation="..."/>
<bean id="customerService" class="demo.jaxrs.server.CustomerService" />
</beans>
Either contract-first or Java-first approach can be used for JAX-WS. JAX-RS annotations can be added to the existing service class. Some custom providers may need to be created, depending on the complexity of the method signatures.
When a WSDL-first approach is used then a document-literal-wrapped style may or may not be a good fit as the code generator unwraps all the types into a signature, for example :
public class CustomerService {
public void doIt(String a, String b) {...};
}
By default JAX-RS may not be able to handle such methods as it requires that only a single parameter can be available in a signature that is not annotated by one of the JAX-RS annotations like @PathParam. So if
a 'String a' parameter can be mapped to a @Path template variable or one of the query segments then this signature won't need to be changed :
@Path("/customers/{a}")
public class CustomerService {
public void doIt(@PathParam("a") String a, String b) {...};
}
Note that CXF Continuations API is supported for both JAXWS and JAXRS services.
Dealing with contexts
When combining JAXWS and JAXRS, one may need to access some context information as part of processing a given request. At the moment, CXF JAXRS does not offer a context implementation which can be used to access a request-specific information common for both JAXWS and JAXRS requests, in cases when the same methods are used to handle both JAXWS and JAXRS requests. Please use a JAXWS WebServiceContext and JAXRS contexts or CXF JAXRS composite MessageContext :
@Path("/customers")
@WebService
public class CustomerService {
@Context WebServiceContext jaxwsContext;
@Context MessageContext jaxrsContext;
@WebMethod
@POST
public void doIt(String b) {
isUserInRole();
};
private void isUserInRole() throws WebApplicationException {
if (jaxwsContext.getSecurityContext() != null) {
// soap invocation
jaxwsContext.getSecurityContext().isUserInRole(theRole);
} else {
// http-only jaxrs one
jaxrsContext.getSecurityContext().isUserInRole(theRole);
}
}
}
Note that injected context instances (jaxwsContext and jaxrsContext) are in fact thread-local proxies hence they will not be equal to null even if they do not represent a given request. For example, jaxrsContext will not be equal to null even if it's not a JAXWS invocation which is being processed at the moment.
However, if say a (JAXWS or JAXRS) SecurityContext needs to be accessed then it will be set in, say, jaxwsContext only if it's a JAXWS/SOAP invocation. For this reason it can be handy using a composite CXF JAXRS MessageContext when accessing a JAXRS-specific context information when combining JAXWS and JAXRS as one can easily check if it's actually a JAXRS request by simply checking an individual context like SecurityContext or UriInfo for null.
Using individual contexts like JAXRS SecurityContext might be less attractive :
@WebService
public class CustomerService {
@Context WebServiceContext jaxwsContext;
// @Resource can be applied too
@Context SecurityContext jaxrsSecurityContext;
}
as some methods of SecurityContext return boolean values so only throwing a runtime exception can reliably indicate that this context is actually not in scope.
Note that if you do not share the same service methods between JAXRS and JAXWS invocations then you can directly access corresponding contexts :
@Path("/customers")
@WebService
public class CustomerService
@Context WebServiceContext jaxwsContext;
@Context MessageContext jaxrsContext;
@WebMethod
public void doItSoap(String b) {
isUserInRole(jaxwsContext.getSecurityContext().getPrincipal());
};
@POST
public void doItSoap(String b) {
isUserInRole(jaxwsContext.getSecurityContext().getPrincipal());
}
private void isUserInRole(Principal p) throws WebApplicationException {
...
}
}
Another option is to avoid the use of contexts in the service code and deal with them in CXF interceptors or JAXRS filters. Sometimes it's possible to avoid the use of contexts altogether. For example, Spring Security can be used to secure a given service at an individual method level.
Sharing CXF DataBindings
JAX-WS and JAX-RS endpoints can be configured to share a single CXF DataBinding instance for reading/writing the data.
Please see the CXF DataBindings section for more information.
Sharing JAX-RS Providers
JAX-WS and JAX-RS endpoints can be configured to share a single JAX-RS provider instance for reading/writing the data.
Please see the JAX-RS DataBinding section for more information.
Applying external user models
When using a WSDL-first approach toward developing the SOAP services you may not want or be able to add JAX-RS annotations to the generated service interface class. Indirectly applying an external user model to this service class via the jaxrs:server endpoint makes it possible to REST-ify the service without making the code changes.