首页 > 代码库 > maven构建spring+cxf的webservice工程

maven构建spring+cxf的webservice工程

一.环境准备  

  1. 安装eclipse  

  2. 安装jdk1.8

  3.配置maven

  4.安装tomcat

二、构建webservice工程

1.创建maven webapp工程

技术分享

技术分享

2.build path 为jdk1.8

技术分享

3.配置maven 工程依赖pom.xml文件

<dependencies>
        <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
   <!-- spring start  -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>4.3.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.3.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <version>4.2.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>4.2.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>4.2.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-ldap</artifactId>
            <version>4.2.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-acl</artifactId>
            <version>4.2.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-cas</artifactId>
            <version>4.2.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-taglibs</artifactId>
            <version>4.2.1.RELEASE</version>
        </dependency>
        <dependency>
         <groupId>org.springframework</groupId>
       <artifactId>spring-aop</artifactId>
       <version>4.3.0.RELEASE</version>
       </dependency>
        
        <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>4.3.0.RELEASE</version>
       </dependency>
       
       <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>4.3.0.RELEASE</version>
    </dependency>
       
        
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.3.0.RELEASE</version>
        </dependency>
        <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>4.3.0.RELEASE</version>
      </dependency>
      
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-instrument</artifactId>
    <version>4.3.0.RELEASE</version>
    </dependency>
    
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>4.3.0.RELEASE</version>
 </dependency>
      <!-- spring end  -->
    
       <dependency>
   <groupId>commons-logging</groupId>
   <artifactId>commons-logging</artifactId>
   <version>1.2</version>
  </dependency>
  <dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>slf4j-log4j12</artifactId>
   <version>1.6.6</version>
  </dependency>

 <!-- webservice  start --> 
  <dependency>  
            <groupId>org.apache.cxf</groupId>  
            <artifactId>cxf-rt-frontend-jaxws</artifactId>  
            <version>3.0.10</version>  
        </dependency>  
        
    <!-- cxf3.0 以后,配置文件里面三个xml import需要去掉,不要导入了-->    
    <!-- webservice  end--> 
    
 
    <dependency>
       <groupId>org.aspectj</groupId>
       <artifactId>aspectjweaver</artifactId>
       <version>1.8.9</version>
    </dependency>
        
  </dependencies>

4.创建webservice发布的接口和实现类

接口类

package com.test;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
@WebService
@SOAPBinding(style = Style.RPC)
public interface IServ {
@WebMethod
public String sayHi(@WebParam(name = "param") String param);
}

实现类

package com.test.impl;
import com.test.IServ;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
@WebService(endpointInterface = "com.test.IServ")
@SOAPBinding(style = Style.RPC)
public class IServImpl implements IServ {
 public String sayHi(@WebParam(name = "param") String param) {
  // TODO Auto-generated method stub
  return "hello" + param;
 }
}

5.配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 
 <!-- 加载spring配置文件 -->
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:application-server.xml</param-value>
 </context-param>
 <!-- 字符编码 -->
 <filter>
  <filter-name>encoding</filter-name>
  <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  <init-param>
   <param-name>encoding</param-name>
   <param-value>UTF-8</param-value>
  </init-param>
 </filter>
 <filter-mapping>
  <filter-name>encoding</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 
 <!-- spring context 加载监听 -->
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
   <!-- webservice servlet 映射配置 -->
 <servlet>
  <servlet-name>CXFServlet</servlet-name>
  <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>CXFServlet</servlet-name>
  <url-pattern>/*</url-pattern>
 </servlet-mapping>
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
</web-app>


6.配置spring + websevice 发布配置文件 application-server.xml

<?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"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
 xsi:schemaLocation="
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
  http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<!-- 发布地址是 http://ip:port/工程名/address?wsdl -->
 <jaxws:endpoint implementorClass="com.test.IServ" id="test" address="/test">
  <jaxws:implementor>
   <bean id="testImp" class="com.test.impl.IServImpl">
   </bean>
  </jaxws:implementor>
 </jaxws:endpoint>
</beans>

7.部署到tomcat,publish webservice

技术分享

看到下图就说明该webservice发布成功

技术分享

技术分享

本文出自 “改变世界” 博客,请务必保留此出处http://wongsong.blog.51cto.com/6040075/1895941

maven构建spring+cxf的webservice工程