Showing posts with label Spring Web Services. Show all posts
Showing posts with label Spring Web Services. Show all posts

Saturday, January 14, 2012

Adding Spring MVC to Spring Web Services Part 2

In this example we will create a Person request handled by Spring MVC framework. Towards the end, I will summarize how both Spring MVC and Spring Web Services can be in the same application but used differently.
  1. Create an entry point into your Web Application. We will create a simple HTML file

  2. Create welcome-file in your web.xml

  3. Now lets add Spring to our application. In web.xml add a front controller mapping for Spring. This controller will control where all requests are routed based on configuration we will set up later.

    • Line 2 - Servlet name will be used for our Spring configuration files
    • Line 3 - DispatcherServlet used as a front controller
    • Line 8 - Any URL that ends with .htm will be served by DispatcherServlet
  4. Create configuration file used by DispatcherServlet containing definitions for all beans. The name of the file is derived from using servlet-name defined in web.xml and appending "-servlet".

    Here we have defined a bean (/person.htm) with a mapping to a Page Controller, which is responsible for handling a request for a particular page of the website. HandlerMapping is an interface that maps between a request URL and the object that is going to handle that request (the handler). The default handler for DispatcherServlet is BeanNameUrlHandlerMapping which uses the bean name defined in this config file to map to the URL in the request so that the DispatcherServlet knows which controller must be invoked for handling different URLs.
  5. Create PersonController

    Since we have not explicitly defined a ViewResolver, we are going to be given a default one by Spring that simply forwards to a URL matching the name of the view specified.
  6. Create Person view, this is the view that is returned by the PersonController. Because we are using a default ViewResolver, place this file in your WebContent folder (outside of WEB-INF)

  7. List of JARs
    • commons-logging
    • spring.web.servlet
    • spring.web-3.1.0
  8. Test Controller

  9. Deploy your application on a Server. Hit the URL: http://localhost:8080/MyWebServices/person.htm

Saturday, December 3, 2011

Web Services using Spring-WS Part 1

Sample Web Service example using Spring-WS


  1. Since Spring-WS uses contract-first style of developing web services, starting with WSDL contract and using Java to implement it (for more detail visit Spring-WS site), we will start with defining a simple XML schema.
    Location: WEB-INF

    Based on above Schema, the XML document can have 2 possible root elements:
    1. PersonRequest element which contains a Person element with FirstName and LastName
    2. PersonResponse element with FullName and SSN
    Here are sample XMLs



  2. Create an Endpoint class to handle XML requests. getPerson() takes JDOM Element as a parameter. Using XPath we can execute queries on the element, selecting specific parts of the XML JDOM tree. In later sections we will explore different parameters.


    1. Line 1 - @Endpoint annotates that this is a web service endpoint
    2. Line 4 - Namespace defined in your XML Schema
    3. Line 11 - @Autowired annotates that this constructor is to be autowired. Spring will scan beans defined in configuration files and match them by data types. Once found, an instance of that class is created and injected.
    4. Line 22 - @PayloadRoot marks an endpoint method as the handler for an incoming request. Any request of 'PersonRequest' root element will be handled by getPerson method
    5. Line 23 - @ResponsePayload annotates that the method return value should be bound to response payload. For void methods, remove the annotation.
    6. Line 24 - @RequestPayload annotates that method parameter should be bound to request payload.
    The rest of the logic calls a service and then builds a JDOM element that gets returned.
  3. Now we need Spring-WS configuration file. You can name the file anything, as long as it matches servlet-name defined in web.xml and ends with "-servlet.xml". More on that in Step 4.
    Location : WEB-INF


    1. Line 5 - Defines a package to scan for components and beans annotated with @Repository, @Component, @Service, or @Controller and would automatically get registered in the application context. PersonService class is annotated with @Component (or can be @Service).
    2. Line 7 - Instruct Spring-WS to use annotation-driven endpoints
  4. Configurations are need in web.xml file to add Spring-WS Servlet. This example uses MessageDispatcherServlet and can be configured to map any type of request (i.e. /ws/* requests). Because this servlet is a standard DispatcherServlet, it will look for a file name [servlet-name]-servlet.xml (spring-ws-servlet.xml created in Step 3) in your WEB-INF and create beans defined in configuration file inside the Spring container.


  5. When using MessageDispatcherServlet, you get the benefit of automatic WSDL exposure. WSDL is used for describing the functionality of your Web Service. It's machine-readable description of how the service can be called, what parameters it expects and what data structures it returns. The MessageDispatcherServlet will automatically detect any WsdlDefinition beans defined in its Spring container and expose them. Add the following configuration to your spring-ws-servlet.xml and web.xml files:

    1. Line 1 - id determines the URL where the WSDL can be retrieved (person.wsdl)
    2. Line 2 - locationuri defines location where the service can be reached (/ws/PersonService/). Using relative URI, we instruct the framework to transform it dynamically to an absolute URI, this way if the service is deployed to different contexts we don't have to change the URI manually
    3. Line 3 - porttypename defines WSDL port type
    4. Line 4 - targetnamespace is not required and if not set will have the same namespace as the XSD schema.
    5. Line 5 - location of XML schema we define in Step 1
    For location transformation to work (mentioned under locationuri), we need to add the following init-param to our servlet in web.xml

  6. List of JARs


  7. Deploy Application to a Server
  8. Using a SOAP client, such as soapUI, you can test out your Web Service. To view your WSDL use the following URL : http://localhost:[port]/[project name]/[servlet-mapping]/[xml-schema].wsdl (i.e. http://localhost:9080/PersonServices/ws/person.wsdl). You can paste the URL in the browser to view generated file.
  9. JUnit Testing is easy with Spring. Create your Test class using SpringJUnit4ClassRunner. Since WEB-INF is never in your classpath, create a copy of your Spring configuration file and save it in the same package as your Test class.
    Here is a sample Test case :

  10. This concludes Part 1 of Spring-WS example.