Spring整合CXF发布及调用WebService

这几天终于把webService搞定,下面给大家分享一下发布webService和调用webService的方法

  1. 添加jar包 (官方下载地址:http://cxf.apache.org/download.html 为了方便扩展我把所有的jar包都加了)
  2. 在web.xml中配置spirng以及CXF
    <?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">
    <display-name></display-name>
    <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <!-- spring -->
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.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> <!-- CXFService -->
    <servlet>
    <servlet-name>CXFService</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    </servlet> <servlet-mapping>
    <servlet-name>CXFService</servlet-name>
    <url-pattern>/*</url-pattern>
    </servlet-mapping>
    </web-app>

    3.写接口实现类   要发布webService的类及要被调用的接口

    package cn.com.lrl.service.impl;
    
    import javax.jws.WebParam;
    import javax.jws.WebResult;
    import javax.jws.WebService; import cn.com.lrl.service.JAXService;
    @WebService
    public class JAXServiceImpl implements JAXService { @Override
    public @WebResult(name="news")String sayHello(@WebParam(name="name") String name) {
    return name +" hello!";
    }

    4.配置spirng配置文件,发布webService  jaxws:server 标签中的address 中的路径 就是访问webService的路径  实际路径是http://localhost:8080/ws/Users?wsdl(ws是项目名)

    <?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"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"
    > <!-- 发布的webService类 -->
    <bean id="userServiceBean" class="cn.com.lrl.service.impl.JAXServiceImpl"/> <!-- 注意下面的address,这里的address的名称就是访问的WebService的name -->
    <jaxws:server id="userService" serviceClass="cn.com.lrl.service.JAXService" address="/Users">
    <jaxws:serviceBean>
    <!-- 要暴露的 bean 的引用 -->
    <ref bean="userServiceBean"/>
    </jaxws:serviceBean>
    <!-- 添加请求日志 -->
    <jaxws:inInterceptors>
    <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
    </jaxws:inInterceptors>
    <!-- 添加响应 日志 -->
    <jaxws:outInterceptors>
    <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>
    </jaxws:outInterceptors>
    </jaxws:server>
    </beans>

    5.启动tomcat发布webService 访问你的webService  如果是这样就代表成功了Spring整合CXF发布及调用WebService6.通过wsimport解析wsdl文件

  Spring整合CXF发布及调用WebService

  7.将生成的代码复制到要调用webService的项目

  8.测试接口是否可以使用

public class Test {
public static void main(String[] args) {
JAXServiceService jaxServiceService =new JAXServiceService();
JAXService jaxService=jaxServiceService.getJAXServicePort();
//调用方法输出
System.out.println(jaxService.sayHello("lrl"));
}
}

  9.调用成功 控制台输出

  Spring整合CXF发布及调用WebService

上一篇:java并发编程(十六)happen-before规则


下一篇:迷你DVD管理器(Java版)