In my endless search for the best way to develop applications, I’ve recently been interested in web services in general and contract-first in particular. Web services are coined contract-first when the WSDL is designed in the first place and classes are generated from it. They are acknowledged to be the most interoperable of web services, since the WSDL is agnostic from the underlying technology. In the past, I’ve been using Axis2 and then CXF but now, JavaEE provides us with the power of JAX-WS (which is aimed at SOAP, JAX-RS being aimed at REST). There’s also a Spring Web Services sub-project. My first goal was to check how easy it was to inject through Spring in both technologies but during the course of my development, I came across other comparison areas. OverviewWith Spring Web Services, publishing a new service is a 3-step process:
For JAX-WS (Metro implementation), the process is very similar:
Creating a web service in Spring or JAX-WS requires the same number of steps of equal complexity. Code generationIn both cases, we need to generate Java classes from the Web Service Definitions File (WSDL). This is completely independent from the chosen technology. The web service itselfCreating the web service in JAX-RS is just a matter of implementing the port type interface, which contains the service method. URL configurationIn JAX-RS, the sun-jaxws.xml file syntax let us configure very finely how each URL is mapped to a particular web service. Spring dependency injectionInjecting a bean in a Spring WS is very easy since the service is already a Spring bean thanks to the Exposing the WSDLBoth JAX-WS and Spring WS are able to expose a WSDL. In order to do so, JAX-WS uses the generated classes and as such, the exposed WSDL is the same as the one we designed in the first place.
Integration testingSpring WS has one feature that JAX-WS lacks: integration testing. A test class that will be configured with Spring Beans definitions file(s) can be created to assert sent output messages agains known inputs. Here’s an example of such a test, based on both Spring test and TestNG: @ContextConfiguration(locations = { "classpath:/spring-ws-servlet.xml", "classpath:/applicationContext.xml" }) public class FindPersonServiceEndpointIT extends AbstractTestNGSpringContextTests { @Autowired private ApplicationContext applicationContext; private MockWebServiceClient client; @BeforeMethod protected void setUp() { client = MockWebServiceClient.createClient(applicationContext); } @Test public void findRequestPayloadShouldBeSameAsExpected(String request, String expectedResponse) throws DatatypeConfigurationException { int id = 5; Source requestPayload = new StringSource(request); Source expectedResponsePayload = new StringSource(expectedResponse); String request = "<a:findPersonRequestType xmlns:a='http://blog./ws-inject'><id>" + id + "</id></a:findPersonRequestType>"; GregorianCalendar calendar = new GregorianCalendar(); XMLGregorianCalendar xmlCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar); xmlCalendar.setHour(FIELD_UNDEFINED); xmlCalendar.setMinute(FIELD_UNDEFINED); xmlCalendar.setSecond(FIELD_UNDEFINED); xmlCalendar.setMillisecond(FIELD_UNDEFINED); String expectedResponse = "<ns3:findPersonResponseTypexmlns:ns3='http://blog./ws-inject'><person><id>" + id + "</id><firstName>John</firstName><lastName>Doe</lastName><birthdate>" + xmlCalendar.toXMLFormat() + "</birthdate></person></ns3:findPersonResponseType>"; client.sendRequest(withPayload(requestPayload)).andExpect(payload(expectedResponsePayload)); } } Note that I encountered problems regarding XML suffixes since not only is the namespace checked, but also the prefix name itself (which is a terrible idea). Included and imported schema resolutionOn one side, JAX-WS inherently resolves included/imported schemas without a glitch. <!-- Let us reference XML schemas --> <bean class="org.springframework.ws.transport.http.XsdSchemaHandlerAdapter" /> <!-- Let us resolve person.xsd reference in the WSDL --> <bean id="person" class="org.springframework.xml.xsd.SimpleXsdSchema"> <property name="xsd" value="classpath:/wsdl/person.xsd" /> </bean> MiscellaneousJAX-WS provides an overview of all published services at the root of the JAX-WS servlet mapping:
ConclusionConsidering contract-first web services, my little experience has driven me to choose JAX-WS in favor of Spring WS: testing benefits do not balance the ease-of-use of the standard. I must admit I was a little surprised by these results since most Spring components are easier to use and configure than their standard counterparts, but results are here. You can find the sources for this article here . Note: the JAX-WS version used is 2.2 so the Maven POM is rather convoluted to override Java 6 native JAX-WS 2.1 classes. |
|