WebService's Helloword (web)

Source: Internet
Author: User

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&gt            ; </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"/&GT;&LT;/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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.