基于spring+hibernate+cxf写的webservice服务,详细配置

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010652906/article/details/72779166

1.首先下载Apache-cxf的jar包

下载地址:http://www.apache.org/dyn/closer.lua/cxf/3.0.13/apache-cxf-3.0.13.zip

2.搭建好spring+hibernate环境

搭建好spring+hibernate环境,确保可以正常运行!这里具体省略!

3.引入cxf的jar包到项目

这里我们选择将所有jar包导入

4.完成WebService服务端代码

IService接口代码

@WebService  
public interface IService {  

    public void hello(@WebParam(name="username") String username);
}  

ServiceImpl实现接口代码:


@Repository
@Transactional
@WebService
public class ServiceImp implements IService{  
    @Override  
    public void hello(@WebParam(name = "username") String username) {  
        System.out.println("hello " + username + " now is " + new Date());  
    }
}  

5.完善cxf和spring结合部分配置文件

除过spring-hibernate.xml,spring-servlet.xml配置外,增加
spirng-cxf.xml

spring-cxf.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:aop="http://www.springframework.org/schema/aop"
    xmlns:c="http://www.springframework.org/schema/c"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:task="http://www.springframework.org/schema/task"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.3.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.3.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.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">

        <import resource="classpath:META-INF/cxf/cxf.xml"/>
        <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>


        <bean id="service1" class="com.sgcc.mcsas.fbsservice.webservice.HelloWorldImpl"></bean>
        <jaxws:endpoint id="hello" implementor="#service1" address="/HelloWorldService"/>

    <bean id="service2" class="com.sgcc.mcsas.fbsservice.openWebService.AbnormalSignImpl"></bean>
        <jaxws:endpoint id="hello2" implementor="#service2" address="/AbnormalSignImpl"/>


这里可以写多个service...



</beans>

6.设置cxf的servlet,配置web.xml

除过spring-servlet外增加cxf-servlet

    <!-- cxf-servlet -->
    <servlet>
        <servlet-name>cxf</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>cxf</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>

7.生成客户端代码测试

打开apache-cxf的bin包,下面可以发现很多可执行的bat文件,

这里我们首先启动tomcat确保项目不会报错,
浏览器进入这个地址:
localhost:8080/项目名/services

就会看到有链接,点击,会发现一条末尾是wsdl的地址,我们复制浏览器的地址,

电脑输入执行cmd命令

将路径转到apache-cxf bin目录

执行 :

wsdl2java -client localhsot:8080/项目名/services/...?wsdl

自动生成客户端。

会生成很多文件,我们只关注结尾是client的java中的main方法即可!

阅读更多
换一批

没有更多推荐了,返回首页