Java Web Service (Spring 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 ?


dcom

soaprequest

dulich

xml1

  • 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

simpleclass

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

cyclicgraph

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:

cyclic_solve

  • 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

workflow_contract_first

messagedispatcher_process

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

definition_xml_request_response

  • Data contract

data_contract

Generate java structure code from xml schema by using xjc command with syntax:
Xjc –d destdir  sourcefile

xjctoolcommand

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

exception

Service interface

interface

Service implementation impl

Define MessageDispatcherServlet (WEB-INF/web.xml)

define_mess_dispatcher

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

automatically_wsdl

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

define_endpoint

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

endpoint_mapping

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

exception_mapping

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

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • E-mail this story to a friend!
  • MySpace
  • StumbleUpon

Comments

Leave a Reply




  • About

    HD Techblog is a collection of knowledge about Information Technology from HD Expertise. We, HD engineers, contribute many different topics from small technical things like programming tips to broad view such as technology trends, business review. Our aim is to help you to find useful knowledge for your work from our expertise.

Canadian pharmacies Canada online pharmacy online pharmacies pharmacy review canada pharmacy online Canada pharmacy canadian online pharmacy Canada pharmacy online