spring+mybatis+WebService发布webservice接口

第一次写博客,有什么错误的地方希望大佬们给我指出来,这是一个在web项目中发布发布webservice接口的demo,使用maven创建项目:

目录结构如下:spring+mybatis+WebService发布webservice接口

第一步pom里面引入相关jar,spring和mybatis的就不写了,大家经常用到,webservice需要用到的jar如下

<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-frontend-jaxws</artifactId>
    <version>3.2.1</version>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-http</artifactId>
    <version>3.2.1</version>
</dependency>

2.web.xml配置

<!-- 初始化spring容器 -->
<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:spring/applicationContext-server.xml</param-value>
</context-param>
<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
  <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>

<!-- WebServices设置 -->
<servlet>
  <servlet-name>CXFServices</servlet-name>
  <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  <load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>CXFServices</servlet-name>
  <url-pattern>/services/*</url-pattern>
</servlet-mapping>

3.编写接口,就是最上方目录结构的service包里面写(这里随意)第一个方法从数据取值,第二个一般方法,写一个类实现这个接口impl包

@WebService
public interface TbContentService {

    List<TbContent> getAll();
    String say(String str);
}
@WebService(endpointInterface = "com.skzhang.service.TbContentService")
public class TbContentServiceImpl implements TbContentService {

    @Autowired
    private TbContentMapper tbContentMapper;

    @Override
    public List<TbContent> getAll() {
        return tbContentMapper.getAll();
    }

    @Override
    public String say(String str) {
        return "hello " + str;
    }
}

4.在resources下面新建文件夹spring,引入spring的配置文件applicationContext-server.xml,具体配置如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       xsi:schemaLocation="
            http://cxf.apache.org/jaxws
            http://cxf.apache.org/schemas/jaxws.xsd
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.2.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
            http://www.springframework.org/schema/util
            http://www.springframework.org/schema/util/spring-util-4.2.xsd">

        <!-- 开启注解 -->
        <context:annotation-config/>
        <!-- 配置数据库连接池 -->
        <!-- 加载配置文件 -->
        <context:property-placeholder location="classpath:properties/db.properties" />
        <!-- 数据库连接池 -->
        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
              destroy-method="close">
            <property name="url" value="${jdbc.url}" />
            <property name="username" value="${jdbc.username}" />
            <property name="password" value="${jdbc.password}" />
            <property name="driverClassName" value="${jdbc.driver}" />
            <property name="maxActive" value="10" />
            <property name="minIdle" value="5" />
        </bean>
        <!-- SqlSessionFactory -->
        <!-- spring管理sqlsessionfactory 使用mybatisspring整合包中的 -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <!-- 数据库连接池 -->
            <property name="dataSource" ref="dataSource" />
            <!-- 加载mybatis的全局配置文件 -->
            <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
            <property name="mapperLocations" >
                <list>
                    <!-- 加载映射文件 -->
                    <value>classpath:com/skzhang/dao/mapping/*.xml</value>
                </list>
            </property>
        </bean>
        <!-- Mapper映射文件的包扫描器 -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.skzhang.dao" />
        </bean>

        <!-- Import apache CXF bean definition 固定-->
        <import resource="classpath:META-INF/cxf/cxf.xml" />
        <!--<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />-->
        <!--<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />-->

        <!-- services接口配置 -->
        <bean id="tbContentServiceBean" class="com.skzhang.impl.TbContentServiceImpl" />
        <!-- CXF 配置WebServices的服务名及访问地址  address访问地址 -->
        <jaxws:server id="tbContentService" address="/merchandise" serviceClass="com.skzhang.service.TbContentService">
            <jaxws:serviceBean>
                <ref bean="tbContentServiceBean"/>
            </jaxws:serviceBean>
        </jaxws:server>
    <!-- -->
</beans>
5.使用tomcat发布项目,我这里创建的是maven项目,可以直接使用mavne的tomcat插件运行,pom文件如下

<build>
  <finalName>spring-mybatis-webservice</finalName>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
        <resource>
            <directory>src/main/java</directory>
            <!--<includes>-->
                <!--<include>**/*.properties</include>-->
                <!--<include>**/*.xml</include>-->
            <!--</includes>-->
            <!--<filtering>false</filtering>-->
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <path>/</path>
            </configuration>
        </plugin>
    </plugins>
</build>
5.访问http://localhost:8080/services/ 有结果发布成功

6.写个测试类测试下结果,我这里实在test文件下写的(个人习惯,在那随意)

public class TestWebService {

    @Test
    public void test(){
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setServiceClass(TbContentService.class);
        factory.setAddress("http://localhost:8080/services/merchandise");
        TbContentService ts = (TbContentService) factory.create();
        List<TbContent> list = ts.getAll();
        System.err.println(list.size());
        System.out.println("=====================");
        System.out.println(ts.say("webservice"));
    }

}
测试结果如下:4          ===================             hello webservice

spring+mybatis+WebService发布webservice接口

参考资料:http://blog.csdn.net/crave_shy/article/details/20375609