spring创建webservice项目

1,新建maven项目,一路next,到finish(注意模板选择)

使用mavn-archetype-webapp方式生成的模板,会提示project facets错误,可右键项目打开properties,修改project facets属性(默认修改此选项时,apply不可点击,可先取消勾选,点击apply,再选中3.0,此时apply可点击)

 

2,打开pom.xml文件,引入依赖关系(spring包需要引入完整,否则无法启动)

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.java</groupId>
  <artifactId>simpleSpring</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>simpleSpring Maven Webapp</name>
  <url>http://maven.apache.org</url>
  
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>4.0.5.RELEASE</spring.version>
        <cxf.version>2.7.15</cxf.version>
    </properties>
  
  <dependencies>
          <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
            <!-- 关闭(排除依赖引用)默认的commons-logging -->
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- spring aop -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
      <!-- spring上下文 -->
          <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>
  <!--若项目中包含jsp文件,需要添加servlet依赖-->
      <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>
        <!-- 以下为webservice依赖 -->
      <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>${cxf.version}</version>
    </dependency>
    <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>${cxf.version}</version>
            <type>jar</type>
    </dependency>
    <!-- webservice end -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  
  <build>
    <finalName>simpleSpring</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
        <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.1</version>
                <configuration>
                    <uriEncoding>utf-8</uriEncoding>
                    <path>/simple</path>
                    <port>80</port>
                </configuration>
            </plugin>
    </plugins>
  </build>
</project>

 

 

 3,配置web.xml文件

<?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_3_0.xsd" version="3.0">
  <display-name>simple</display-name>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring.xml</param-value>
  </context-param>
  <!-- 这个必须有,为程序启动入口,作用是启动Web容器时,自动装配ApplicationContext的配置信息-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>cxfServlet</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    <load-on-startup>0</load-on-startup>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>cxfServlet</servlet-name>
    <url-pattern>/webservice/*</url-pattern>
  </servlet-mapping>
</web-app>

 PS:若缺少listener,访问时后台报错:WARNING: Can't find the the request for http://localhost/simple/webservice/webserviceHello's Observer 

 

4,配置spring文件

<?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:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:cxf="http://cxf.apache.org/core" 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.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
        http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
    
    <!-- 配置bean自动扫描包位置 -->
    <context:component-scan base-package="com.java" />
    
    <import resource="classpath:META-INF/cxf/cxf.xml"></import>
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
    
    <!-- id=hello对应webservice的servicename,implementor对应bean名称,address为发布地址 -->
    <jaxws:endpoint id="hello" implementor="#helloClass" address="/webserviceHello"></jaxws:endpoint>
    
</beans>

 

 

5,编辑代码

package com.java;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public interface HelloWord {
    @WebMethod
    public String sayHi(@WebParam(name="name")String name);
}
package com.java;

import javax.jws.WebService;

import org.springframework.stereotype.Component;

@WebService(endpointInterface="com.java.HelloWord",serviceName="hello")
@Component("helloClass")
public class HelloWordImpl implements HelloWord {

    @Override
    public String sayHi(String name) {
        return "hello word "+name;
    }

}

 

 

6,启动项目,访问路径http://localhost/simple/webservice/webserviceHello?wsdl

其中simple为项目名称,webservice为web.xml中配置,webserviceHello为spring.xml中配置的地址

 ps:完整项目目录https://files.cnblogs.com/files/javadongx/simpleSpringWebservice.rar

posted @ 2017-06-07 16:13  javadongx  阅读(4514)  评论(0编辑  收藏  举报

javadong@qq.com