WebService-Java Implementation CXF (using: Spring+cxf+tomcat release WebService)

Source: Internet
Author: User

1. Create a new MAVEN project, select the WebApp template, and name it Ws_spring_cxf_tomcat

2. Adding spring and cxf dependencies in Pom.xml

    <!--add Spring dependency--><dependency> <groupId>org.springframework</groupId> <art Ifactid>spring-core</artifactid> <version>4.1.7.RELEASE</version></dependency>< Dependency> <groupId>org.springframework</groupId> <artifactid>spring-beans</artifactid > <version>4.1.7.RELEASE</version></dependency><dependency> <groupId> Org.springframework</groupid> <artifactId>spring-tx</artifactId> <version>4.1.7.release& lt;/version> </dependency><dependency> <groupId>org.springframework</groupId> <arti Factid>spring-context</artifactid> <version>4.1.7.RELEASE</version></dependency>< Dependency> <groupId>org.springframework</groupId> <artifactid>spring-context-support</ 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-webmvc</artifactId> <version >4.1.7.RELEASE</version></dependency><dependency> <groupid>org.springframework</ Groupid> <artifactId>spring-aop</artifactId> <version>4.1.7.release</version></ dependency> <dependency> <groupId>org.springframework</groupId> <artifactId> Spring-aspects</artifactid> <version>4.1.7.release</version></dependency><dependency > <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <vers ion>4.1.7.release</version></dependency> <!--add CXF DependenCy--><dependency> <groupId>org.apache.cxf</groupId> <artifactid>cxf-core</ Artifactid> <version>3.1.5</version></dependency><dependency> <groupId> Org.apache.cxf</groupid> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.1.5 </version></dependency><dependency> <groupId>org.apache.cxf</groupId> <  Artifactid>cxf-rt-transports-http</artifactid> <version>3.1.5</version></dependency>
3. Modify Project->src->main->web App->web-info->web.xml

Specify the spring configuration and servlet configuration:

<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee/http Java.sun.com/xml/ns/javaee/web-app_2_5.xsd "id=" webapp_id "version=" 2.5 "> <display-name>ws_spring_cxf_ Tomcat</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> < Welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> < Welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> < Welcome-file>default.jsp</welcome-file> </welcome-file-list> <!--Spring config-->< Context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath: applicationcontext.xml</param-value></context-param> <!--Spring Listener--><listener>   <listener-class>org.springframework.web.context.contextloaderlistener</listener-class></ Listener>
<!--Add on a servlets to handle Web service request--><servlet> <servlet-name>cxfservlet</servlet- Name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/webservice/*< /url-pattern> </servlet-mapping></web-app>
4. Add the spring configuration file to the Classpath path
<?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:p= "http://www.springframework.org/schema/p" xmlns:aop= "H Ttp://www.springframework.org/schema/aop "xmlns:context=" Http://www.springframework.org/schema/context "xmlns:j Ee= "Http://www.springframework.org/schema/jee" xmlns:tx= "Http://www.springframework.org/schema/tx"xmlns:jaxws= "Http://cxf.apache.org/jaxws" xsi:schemalocation= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP Http://www.springframework.org/schema/aop/spring-aop-4.0.xsd Http://www.springframework.org/schema/beans/HTTP Www.springframework.org/schema/beans/spring-beans-4.0.xsd Http://www.springframework.org/schema/context http://w Ww.springframework.org/schema/context/spring-context-4.0.xsd Http://www.springframework.org/schema/jee HTTP://WW W.springframework.org/schema/jee/spring-jee-4.0.xsd Http://www.springframework.org/schema/tx HTTP://WWW.SPRINGFR Amework.org/schema/tx/spring-tx-4.0.xsd Http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd " > <!--import CXF configuration Files----<import resource= "Classpath:meta-inf/cxf/cxf.xml"/> <import resource= "classpath:meta-inf/cxf/ Cxf-servlet.xml "/>

Note add:

1.Spring and cxf name space;

2.CXF configuration file

5. Define WebService interface and implementation class
Package Com.example.tuo.webservice.impl;import Javax.jws.webservice;import Org.springframework.stereotype.component;import Com.example.tuo.webservice.HelloWorld; @Component ("HelloWorld")@WebServicepublic class Helloworldimpl implements Helloworld{public String SayHello (String SB) {//TODO auto-generated method Stubreturn "Hello world," +SB;}}

Add "@Component (" HelloWorld ") annotation to the implementation class, and modify the spring configuration file (Applicationcontext.xml) to implement the class with spring management

<!--auto Scan class as Bean--><context:component-scan base-package= "Com.example.tuo.webservice"/>
6. Modify the spring configuration file to expose the endpoint of the Weibo service
<!--define Web service provider  --><jaxws:endpoint    implementor= "#helloWorld"    address= "/ HelloWorld "></jaxws:endpoint>
7. Launch Tomcat and enter WebService address in the browser

http://localhost:8080/WS_Spring_CXF_Tomcat/webservice/HelloWorld?wsdl

OK, so we can successfully say that WebService was posted on tomcat. :)

WebService-Java Implementation CXF (using: Spring+cxf+tomcat release WebService)

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.