Pages

Sunday, 5 May 2013

Web services in Spring 3.0 with JAX-WS

There is some information on the net about how to integrate Spring with JAX-WS but most of the stuff I've found has been using Spring 2.0. This will show you how to achieve web service integration with full wsdl generation in Spring 3.0 which is rather easy actually.
 
Below is the web.xml:

The applicationContext.xml is as follows:


The the essential part of this is really line 14 that does all the magic and the component scanning context xml declaration above that part. As the simpleJaxWsServiceExporter will essentially look thru any beans that have been declared that are decorated with the WebService attribute are create an endpoint for them like the example below:


1:  package com.dmcliver.springhibernatejaxws.endpoints;  
2:    
3:  import java.util.List;  
4:    
5:  import javax.jws.WebMethod;  
6:  import javax.jws.WebService;  
7:    
8:  import org.springframework.beans.factory.annotation.Autowired;  
9:  import org.springframework.stereotype.Service;  
10:  import com.dmcliver.springhibernatejaxws.domain.Person;  
11:  import com.dmcliver.springhibernatejaxws.domain.Profession;  
12:  import com.dmcliver.springhibernatejaxws.services.PersonService;  
13:    
14:  // This will be the port name appended with the word port at the end of the name  
15:  @Service("PersonServiceEndpoint")  
16:  // This will be the service name  
17:  @WebService(serviceName="PersonService")  
18:  public class PersonServiceEndpoint{  
19:    
20:       @Autowired  
21:        private PersonService personService;  
22:    
23:       @WebMethod  
24:       public List<Person> findByProfession(Profession profession){  
25:            return personService.findByProfession(profession);  
26:       }  
27:  }  
28:    

Since we've declared our spring context to enable component scanning we can now use this to our advantage to allow bean creation of a hibernate persistence layer as seen with the rest of xml in the application context file.

Note that this example is only valid for using in a non jee web container such as Tomcat or Jetty. If you are using a jee spec server then no applicationContext file is needed at all, you can simply declare the endpoint class in the web.xml file itself and there is a tutorial by Byron Kiourtzoglou over at java code geeks on this.

Full source is available at github

No comments:

Post a Comment