So easy Webservice 8.spring整合CXF 发布WS

1.添加jar包(cxf的jar包中包含了spring的jar包),添加spring配置文件

2.web.xml中配置CXFServlet,过滤WS服务的地址

<!-- 配置CXFServlet,实现地址过滤的功能,项目启动时实例化 -->
<servlet>
<servlet-name>cxfServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>csfServlet</servlet-name>
<!-- 访问 http://localhost:8080/工程名/ws/ 下的所有请求,都会交给当前servlet过滤 -->
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>

<!-- 由于上面的url-pattern中配置了过滤路径,所有ws服务地址应配置为:http://localhost:8080/工程名/ws/xxxService -->

3.配置applicationContext.xml

3.1 web.xml中配置spring监听器,在项目启动时加载spring配置文件

<!-- 配置监听器:加载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>

3.2 添加ws服务实现类(必须添加注解@WebService),在spring中配置服务实现类的bean

import java.util.ArrayList;
import java.util.List;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

@WebService(
        serviceName="UserService",
        name="user",
        portName="UserServicePort",
        targetNamespace="com.mlxs.ws.cxfws.spring"
)
public class UserServiceImpl {

    private static List<User> ulist = new ArrayList<User>();

    @WebMethod(operationName="add")
    public void add(@WebParam(name="u") User u) {
        ulist.add(u);
    }

    @WebMethod(operationName="query")
    //@WebResult 如果是集合,指定的是集合中存放的对象名称
    public @WebResult(name="user") List<User> query() {
        return ulist;
    }

}
<bean id="userService" class="com.mlxs.ws.cxfws.spring.UserServiceImpl"/>

3.3 添加jaxws标签
在cxf.jar中,找到/schemas/jaxws.xsd,再查找命名空间:targetNamespace="http://cxf.apache.org/jaxws",将内容复制到spring文件中:

xmlns:jaxws="http://cxf.apache.org/jaxws"

再在xsi:schemaLocation中添加(别名 实际地址):

http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd

3.4 配置CXF, 把服务实现类bean配置发布成WS服务 ;
address="/user"中不用配置前缀,ws地址在web.xml中servlet的配置的:http://localhost:8080/工程名/ws/user;
配置日志;

<jaxws:server address="/user">
<jaxws:serviceBean>
<ref bean="userService"/>
</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>

4.部署到tomcat,启动服务,使用浏览器访问http://localhost:8080/webservice_cxf_server/ws/user?WSDL

<wsdl:definitions xmlns:ns1="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="com.mlxs.ws.cxfws.spring" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="UserService" targetNamespace="com.mlxs.ws.cxfws.spring">
<wsdl:types>
<xs:schema xmlns:tns="com.mlxs.ws.cxfws.spring" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified" targetNamespace="com.mlxs.ws.cxfws.spring" version="1.0">
<xs:element name="add" type="tns:add"/>
<xs:element name="addResponse" type="tns:addResponse"/>
<xs:element name="query" type="tns:query"/>
<xs:element name="queryResponse" type="tns:queryResponse"/>
<xs:complexType name="query">
<xs:sequence/>
</xs:complexType>
<xs:complexType name="queryResponse">
<xs:sequence>
<xs:element maxOccurs="unbounded" minOccurs="0" name="user" type="tns:user"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="user">
<xs:sequence>
<xs:element name="id" type="xs:int"/>
<xs:element minOccurs="0" name="name" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="add">
<xs:sequence>
<xs:element minOccurs="0" name="u" type="tns:user"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="addResponse">
<xs:sequence/>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="queryResponse">
<wsdl:part element="tns:queryResponse" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="add">
<wsdl:part element="tns:add" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="addResponse">
<wsdl:part element="tns:addResponse" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="query">
<wsdl:part element="tns:query" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:portType name="user">
<wsdl:operation name="query">
<wsdl:input message="tns:query" name="query"></wsdl:input>
<wsdl:output message="tns:queryResponse" name="queryResponse"></wsdl:output>
</wsdl:operation>
<wsdl:operation name="add">
<wsdl:input message="tns:add" name="add"></wsdl:input>
<wsdl:output message="tns:addResponse" name="addResponse"></wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="UserServiceSoapBinding" type="tns:user">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="query">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="query">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="queryResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="add">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="add">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="addResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="UserService">
<wsdl:port binding="tns:UserServiceSoapBinding" name="UserServicePort">
<soap:address location="http://localhost:8080/webservice_cxf_server/ws/user"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

5.使用myeclipse SOAP浏览器浏览:

调用add服务接口添加数据:

查看控制台:

Address: http://localhost:8080/webservice_cxf_server/ws/user
Encoding: UTF-8
Http-Method: POST
Content-Type: text/xml; charset=utf-8
Headers: {Accept=[application/soap+xml, application/dime, multipart/related, text/*], cache-control=[no-cache], connection=[close], Content-Length=[371], content-type=[text/xml; charset=utf-8], host=[localhost:8080], pragma=[no-cache], SOAPAction=[""], user-agent=[IBM Web Services Explorer]}
Payload: <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="com.mlxs.ws.cxfws.spring" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soapenv:Body>
    <q0:add>
      <u>
        <id>1</id>
        <name>admin</name>
      </u>
    </q0:add>
  </soapenv:Body>
</soapenv:Envelope>

调用查询服务接口query:

控制台:

----------------------------
ID: 12
Address: http://localhost:8080/webservice_cxf_server/ws/user
Encoding: UTF-8
Http-Method: POST
Content-Type: text/xml; charset=utf-8
Headers: {Accept=[application/soap+xml, application/dime, multipart/related, text/*], cache-control=[no-cache], connection=[close], Content-Length=[288], content-type=[text/xml; charset=utf-8], host=[localhost:8080], pragma=[no-cache], SOAPAction=[""], user-agent=[IBM Web Services Explorer]}
Payload: <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="com.mlxs.ws.cxfws.spring" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soapenv:Body>
    <q0:query/>
  </soapenv:Body>
</soapenv:Envelope>

--------------------------------------
2016-1-28 23:38:35 org.apache.cxf.interceptor.AbstractLoggingInterceptor log
信息: Outbound Message
---------------------------
ID: 12
Encoding: UTF-8
Content-Type: text/xml
Headers: {}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:queryResponse xmlns:ns2="com.mlxs.ws.cxfws.spring"><user><id>1</id><name>admin</name></user><user><id>2</id><name>魅力小生</name></user></ns2:queryResponse></soap:Body></soap:Envelope>
--------------------------------------

也可以自己写jsp或者java测试类进行ws服务接口的访问。。。

时间: 2024-10-15 09:02:39

So easy Webservice 8.spring整合CXF 发布WS的相关文章

webservice开发spring整合cxf入门案例

一.创建服务端发布服务 1. 添加依赖 <dependencies> <!-- cxf 进行rs开发 必须导入 --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxrs</artifactId> <version>3.0.1</version> </dependency> &l

Spring 整合CXF 实现WebService(JAX-WS)

服务端创建项目 添加依赖 web.xml 配置CXFServlet <?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="htt

Spring整合CXF,发布RSETful 风格WebService(转)

Spring整合CXF,发布RSETful 风格WebService 这篇文章是承接之前CXF整合Spring的这个项目示例的延伸,所以有很大一部分都是一样的.关于发布CXF WebServer和Spring整合CXF这里就不再多加赘述了.如果你对Spring整合CXF WebService不了解,具体你可以参看这两篇文章: http://www.cnblogs.com/hoojo/archive/2011/03/30/1999563.html http://www.cnblogs.com/ho

Spring整合CXF之发布WebService服务

今天我们来讲下如何用Spring来整合CXF,来发布WebService服务: 给下官方文档地址:http://cxf.apache.org/docs/writing-a-service-with-spring.html 根据官方文档.我们把前面的实例用Spring整合CXF来处理下.会简化很多: 首先我们来建一个Maven项目 WebService_CXF 建好项目第一步,我们打开pom.xml 我们来添加下Spring支持: <!-- 添加Spring支持 --> <dependen

spring boot整合cxf发布和调用webservice

一.前言 说起web service最近几年restful大行其道,大有取代传统soap web service的趋势,但是一些特有或相对老旧的系统依然使用了传统的soap web service,例如银行.航空公司的机票查询接口等.本博客主要讲解得是spring boot整合cxf发布webservice服务和spring boot整合cxf客户端调用webservice服务本案例使用maven方式二.编码核心文件清单1.pom.xml <?xml version="1.0"

WebService与Spring整合部署

服务端(CXF发布webservice): 1.  http://cxf.apache.org/download.html下载 cxf文件包(3.1.18必须使用Java 7或Java 8). 2.  将下载包的lib文件夹下的jar全部拷贝到spring项目中的lib目录下,注意删除相同的jar包(版本号不同拷贝不会替换). 3.  删除以下4个jar包: cxf-services-ws-discovery-api-3.1.5.jar cxf-services-ws-discovery-ser

spring整合CXF

一. 概述 可以在传统的Java EE应用的基础上添加一层Web Service层, 我们的Java EE应用就可以对外暴漏Web Service, 这样就允许任何平台.任何语言编写的程序来调用这个Java EE应用 二. 步骤 1. 新建web工程springCXF, 并复制需要的Jar包:见上图 2. 在web.xml中配置CXF的核心控制器: CXFServlet <?xml version="1.0" encoding="UTF-8"?> <

Spring整合CXf WebService总结

Web service是一个平台独立的,低耦合的,自包含的.基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述.发布.发现.协调和配置这些应用程序,用于开发分布式的互操作的应用程序.关于Java webService框架,主要有AXIS.XFire.CXF,还有Java自带的JAX-WS(必须JDK6以上环境). SOAP RPC的工作原理:类似于web的请求/响应方式,不同之处在于web客户和web服务器之间传输的是HTML数据.在SOAP RPC模式中

Spring 集合cxf发布webservice

通过spring发布webservice接口 spring jar包+cxf jar Java包 以下文件缺少jar包需要自行去下载 项目结构 1.实体类 package com.test.entity; public class User { public User(String name, String pwd, String sex) { super(); this.name = name; this.pwd = pwd; this.sex = sex; } private String