Java Web Service (Spring web service)
Posted on December 17, 2010 by toando
Filed Under How To | Tags: spring ws, web service |
CONTENT
What and why web service ?
What contract first and contract last ?
Why contract first ?
Steps to create a simple web service (server) with spring-ws
Call Java web service from .NET(C#)
References
Questions & Answers
What and why web service ?




- Web service is a standard for building and developing distributed applications
- Ability to work on all operating systems
- Expanding interoperability between applications
- Can be reused
- Enhancing communication between client and server through the web environment
What contract first and contract last
- Contract-last approach: write code (Java, .Net, etc) code first, and let the Web service contract (WSDL, Schema) be generated from that
- Contract-first: write service contract(WSDL, Schema) first, and use language (Java, .Net, etc) to implement that contract
- Ex: Labour contracts
Why contract first ?
- Cyclic graphs

Converting it to XML, we will end up with something like:

which will take a long time to finish, because there is no stop condition for this loop
Solution: references to objects that were already marshalled, like so:

- Performance
When Java is automatically transformed into XML, an object might reference another object, which refers to another, etc. In the end, half of the objects on the heap in your virtual machine might be converted into XML, which will result in slow response times.
When using contract-first, we do not care about converting time
Spring web service

An appropriate endpoint is searched for using the configured EndpointMapping(s)
An appropriate adapter is searched for the endpoint. The MessageDispatcher delegates to this adapter to invoke the endpoint
Return a response
Steps to create a simple web service with spring-ws
- Define operations

- Data contract

Generate java structure code from xml schema by using xjc command with syntax:
Xjc –d destdir sourcefile
![]()
An easier way to do it is with eclipse XJC plugin from this site:
https://jaxb-workshop.dev.java.net/plugins/eclipse/xjc-plugin.html
Place person.xsd into WEB-INF folder
Add the following lib into WEB-INF/lib folder
aopalliance.jar
bcprov-jdk14-1.43.jar
commons-logging-1.1.1.jar
log4j-1.2.15.jar
opensaml-1.1.jar
saaj-impl-1.3.2.jar
spring.jar
spring-webmvc.jar
spring-ws-1.5.9-all.jar
wsdl4j-1.6.1.jar
wss4j-1.5.8.jar
xalan-2.7.0.jar
xercesImpl-2.8.1.jar
xml-apis-1.3.04.jar
xmlsec-1.4.3.jar
xmlsec-2.0.jar
xws-security-2.0-FCS.jar
- Create service classes
Handle exception

Service interface

Service implementation 
Define MessageDispatcherServlet (WEB-INF/web.xml)

Automatic WSDL exposure (WEB-INF/spring-ws-servlet.xml)

Define endpoint (WEB-INF/spring-ws-servlet.xml)

Endpoint mapping (WEB-INF/spring-ws-servlet.xml)

Exception mapping (WEB-INF/spring-ws-servlet.xml)

Conclusion
- Define operations (in xml)
- Data contract (in xml schema – xsd file)
- Generate xsd file to java source (in jaxb)
- Create service classes
- Define MessageDispatcherServlet (in web.xml)
- Automatic WSDL Exposure (in spring-ws-servlet.xml)
- Define endpoints (in spring-ws-servlet.xml)
- Endpoint mappings (in spring-ws-servlet.xml)
- Exception mappings (in spring-ws-servlet.xml)
References
- Spring-ws
http://static.springsource.org/spring-ws/sites/1.5/
- Demo source code
http://www.mediafire.com/?vnc7p29lzr3prbf












Leave a Reply