WebService -- Java 实现之 CXF ( 使用:Spring+CXF+Tomcat发布webService)

Posted TonyZhang24

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WebService -- Java 实现之 CXF ( 使用:Spring+CXF+Tomcat发布webService)相关的知识,希望对你有一定的参考价值。

1. 新建一个Maven项目,选择webapp模板,命名为WS_Spring_CXF_Tomcat

2. 在POM.xml中添加Spring和CXF的依赖

    <!-- 添加 Spring dependency -->
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>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</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-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>
	    <version>4.1.7.RELEASE</version>
	</dependency> 

    <!-- 添加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. 修改 project->src->main->web app->WEB-INFO->web.xml

指定spring配置和servlet配置:

<?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 servlet 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. 添加Spring配置文件到classpath路径下

<?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="http://www.springframework.org/schema/aop"   
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:jee="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://www.springframework.org/schema/context/spring-context-4.0.xsd  
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.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"/>

注意添加:

1.Spring和CXF名空间;

2.CXF配置文件 

5.定义WebService接口及实现类

package com.example.tuo.webservice.impl;

import javax.jws.WebService;

import org.springframework.stereotype.Component;

import com.example.tuo.webservice.HelloWorld;

@Component("helloWorld")
@WebService
public class HelloWorldImpl implements HelloWorld{

	public String sayHello(String sb) {
		// TODO Auto-generated method stub
		return "Hello world," +sb;
	}

}

在实现类中添加 "@Component("helloWorld")" 注解,并且修改Spring配置文件(applicationContext.xml),利用Spring管理实现类

<!-- auto scan class as bean -->
<context:component-scan base-package="com.example.tuo.webservice" />

6.修改Spring配置文件,暴露微博service的endpoint

<!-- define web service provider  -->
	<jaxws:endpoint
	    implementor="#helloWorld"
	    address="/HelloWorld">
	</jaxws:endpoint>

7. 启动Tomcat,在浏览器中输入webservice的address

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

 

OK, 这样我们就成功的讲webservice发布到了Tomcat上。 :)

以上是关于WebService -- Java 实现之 CXF ( 使用:Spring+CXF+Tomcat发布webService)的主要内容,如果未能解决你的问题,请参考以下文章

Java调用WebService之Axis实现

WebService -- Java 实现之 CXF (初体验)

WebService -- Java 实现之 CXF ( 添加系统预定义的拦截器)

WebService之CXF注解之三(Service接口实现类)

WebService -- Java 实现之 CXF ( 使用CXF工具生成client 程序)

(原创)用JAX-WS+Spring实现简单soap规范的webservice