Spring Integrated WebService
Pom.xml file
<dependencies><!--CXF WS Development--><dependency><groupid>org.apache.cxf</groupid>< Artifactid>cxf-rt-frontend-jaxws</artifactid><version>3.0.1</version></dependency> <dependency><groupid>org.springframework</groupid><artifactid>spring-context</ Artifactid><version>4.1.7.release</version></dependency><dependency><groupid> Org.springframework</groupid><artifactid>spring-web</artifactid><version>4.1.7.release </version></dependency><dependency><groupId>org.springframework</groupId>< Artifactid>spring-test</artifactid><version>4.1.7.release</version></dependency> <dependency><groupid>junit</groupid><artifactid>junit</artifactid><version >4.12</version></dependency></dependencies><build><plugins><plugin>< Groupid>org.codehaus.mOjo</groupid><artifactid>tomcat-maven-plugin</artifactid><version>1.1</version> <configuration><port>9998</port></configuration></plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactid>maven-compiler-plugin</ Artifactid> <version>2.3.2</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> ; </plugin> </plugins></build>
Web. xml file
<context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:applicationcontext.xml</param-value> </context-param> <listener> < Listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </ listener> <servlet> <servlet-name>CXFService</servlet-name> <servlet-class >org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>1</ load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFService< /servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping>
Applicationcontext.xml file
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws= "Http://cxf.apache.org/jaxws" xsi:schemalocation= " Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp:// Cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd "><!--ServiceClass Service Interface address service access addresses -- ><jaxws:server id= "UserService" address= "/userservice" serviceclass= "Com.baidu.service.UserService" >< Jaxws:servicebean><bean class= "COM.BAIDU.SERVICE.IMP.USERSERVICEIMP"/></jaxws:servicebean></ Jaxws:server></beans>
Package structure
UserService interface
Package Com.baidu.service;import Javax.jws.webmethod;import Javax.jws.webservice;import com.baidu.domain.User;@ Webservicepublic interface UserService {@WebMethodpublic User get (Integer id), @WebMethodpublic void Eat ();}
Userserviceimp Implementation Class
Package Com.baidu.service.imp;import Javax.jws.webmethod;import Javax.jws.webservice;import com.baidu.domain.User; Import Com.baidu.service.UserService; @WebServicepublic class Userserviceimp implements UserService {public User get ( Integer ID) {if (id==1) {User u=new User (); U.setid (2); U.setname ("Zhang San"); return u;} return null;} @Overridepublic void Eat () {System.out.println ("123");}}
Create a new MAVEN Web project
Pom.xml file does not change
Applicationcontext.xml file
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws= "Http://cxf.apache.org/jaxws" xsi:schemalocation= " Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp:// Cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd "><!--address Client Access Service path ServiceClass Configure interface Servicebean Configuration implementation class--><jaxws:client id= "Userserviceclient" serviceclass= "Com.baidu.service.UserService" address= "Http://localhost:9998/werservicespring/services/userService" ><!--source message blocker <jaxws: Ininterceptors><bean class= "Org.apache.cxf.interceptor.LoggingInInterceptor"/></JAXWS: ininterceptors>--><!--output message blocker <jaxws:outinterceptors><bean class= " Org.apache.cxf.interceptor.LoggingOutInterceptor "/></jaxws:outinterceptors>--></jaxws:client ></beans>
It is also necessary to create an interface package structure before testing
Package Com.baidu.service;import Javax.jws.webmethod;import Javax.jws.webservice;import com.baidu.domain.User;@ Webservicepublic interface UserService {@WebMethodpublic User get (Integer id), @WebMethodpublic void Eat ();}
Test class
Package Com.baidu.test;import Org.junit.test;import Org.junit.runner.runwith;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.test.context.contextconfiguration;import Org.springframework.test.context.junit4.springjunit4classrunner;import Com.baidu.domain.user;import Com.baidu.service.UserService, @RunWith (springjunit4classrunner.class) @ContextConfiguration (locations= " Classpath:applicationContext.xml ") public class Test01 {@Autowiredprivate userservice userservice; @Testpublic void Test01 () {//userservice.eat (); User user = Userservice.get (1); SYSTEM.OUT.PRINTLN (user);}}
WebService's Helloword (web)