`
ruijin5566
  • 浏览: 4543 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

CXF发布webservice

阅读更多
[size=large][size=medium]服务端配置
<?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.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-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

<bean id="address" class="cn.com.pop.service.AddressManager"/>

<bean id="inInter" class="org.apache.cxf.interceptor.LoggingInInterceptor"/>

<bean id="outInter" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>

<bean id="myPasswordCallback" class="cn.com.pop.interceptor.ServerPasswordCallback"/>

<bean id="checkSignInter" class="org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor">
         <constructor-arg>
            <map>
               <entry key="action" value="UsernameToken"/>
               <entry key="passwordType" value="PasswordDigest"/>
               <entry key="signaturePropFile" value="..."/>
               <entry key="passwordCallbackRef">
                  <ref bean="myPasswordCallback"/>
               </entry>
            </map>
         </constructor-arg>
    </bean>
     
<jaxws:server id="addressServer" serviceClass="cn.com.pop.service.AddressManager" address="/Address">
<jaxws:serviceBean>
<ref bean="address"/>
</jaxws:serviceBean>
<jaxws:inInterceptors>
<ref bean="inInter"/>
<!-- <ref bean="checkSignInter"/> -->
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<ref bean="outInter"/>
</jaxws:outInterceptors>
</jaxws:server>

<jaxws:endpoint id="helloService" implementor="cn.com.pop.service.impl.HelloServiceImpl" address="/HelloService">
  <jaxws:inInterceptors>
  <ref bean="checkSignInter"/>
  </jaxws:inInterceptors>
</jaxws:endpoint>
 
</beans>

服务类
package cn.com.pop.service.impl;

import javax.jws.WebService;

import cn.com.pop.service.HelloService;
import cn.com.pop.vo.Student;

@WebService(endpointInterface = "cn.com.pop.service.HelloService", serviceName = "helloService")
public class HelloServiceImpl implements HelloService {

public String sayHello(String str) {
return "Hello ".concat(str);
}

public Student getStudentBean() {
Student student = new Student();
student.setName("Jim Green");
student.setSex(1);
return student;
}

}

发布服务:
/**
* 利用CXF中的Jetty来发布自己的WebService
*
* 利用CXF来生成客户端需要的一些文件和WebService接口,命令如下:
* D:\Program Files\Java\apache-cxf-2.2.4\bin>wsdl2java -p cn.com.pop.client.jetty.clientbean -d D:\ http://localhost:9000/helloService?wsdl
*
* wsdl2java是CXF官方的帮助工具,可以下载一个官方的CXF包 本案例用的版本是apache-cxf-2.2.4 ,解压之后可以拷贝到自己的盘符,本案例拷贝到D:\Program Files\Java\目录
*  apache-cxf-2.2.4目录下有个bin目录,该目录下有很多官方提供的工具,其中有个wsdl2java,可以用命令wsdl2java -h来查看命令参数,如果感兴趣可以自己找找帮助文件
*  命令:wsdl2java -p com.jd.sop1 -all -d D:\ http://localhost:9000/helloService?wsdl
*  如果WebService换了发布URL之后需要重新生成客户端文件,或者手动修改客户端文件中对应的URL

* 还可以用别的方法来生成客户端需要的文件
*
* @author Administrator
*
*/
public class JettyTestCase {

public static void main(String[] arg){
System.out.println("Starting Server");

HelloServiceImpl impl = new HelloServiceImpl();
        String addr = "http://localhost:9000/helloService";
       
        //通过CXF提供的Jetty方式发布WebService
        Endpoint.publish(addr,impl);
       
        System.out.println("WebServer Register Success");
}

}


客户端:
<?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:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

<jaxws:client id="helloService" serviceClass="cn.com.pop.client.web.clientbean.HelloService"
address="http://localhost:9000/helloService">
<jaxws:inInterceptors>
<ref bean="logIn"/>
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<ref bean="logOut"/>
<!-- <ref bean="saajOut"/> -->
<ref bean="wss4jOut"/>
</jaxws:outInterceptors>
</jaxws:client>

<bean id="logIn" class="org.apache.cxf.interceptor.LoggingInInterceptor" />
<bean id="logOut" class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
<bean id="saajOut" class="org.apache.cxf.binding.soap.saaj.SAAJOutInterceptor" />
<bean id="wss4jOut" class="org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor">
<constructor-arg>
<map>
<entry key="action" value="UsernameToken" />
<entry key="user" value="joe" />
<entry key="passwordType" value="PasswordText" />
<entry key="passwordCallbackClass" value="cn.com.pop.client.web.util.ClientPasswordCallback" />
</map>
</constructor-arg>
</bean>

</beans>

package cn.com.pop.client.web;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.com.pop.client.web.clientbean.HelloService;

/**
* 利用spring配置来获取WebService
*
* @author
*
*/
public class SpringCase {

public static void main(String[] args) {
// 利用spring的上下文来加载我们需要的服务
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(
"classpath:bean-client.xml");
ctx.registerShutdownHook();
HelloService client = (HelloService) ctx.getBean("helloService");
String str = client.sayHello("中国,测试成功");
System.out.println("str=" + str);
// System.exit(0);
}
}


package cn.com.pop.client.jetty;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import cn.com.pop.client.jetty.clientbean.HelloService;
import cn.com.pop.client.jetty.clientbean.Student;

/**
*
* 利用JaxWsProxyFactoryBean来获取WebService
*
*/
public class TestCase {
public static void main(String[] args) {
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();

factory.setAddress("http://localhost:9000/helloService");

factory.setServiceClass(HelloService.class);
HelloService obj = (HelloService) factory.create();
System.out.println(obj.sayHello("中国,测试成功"));

Student student = obj.getStudentBean();
System.out.println(student.getName());
}

}

[/size][/size]
分享到:
评论

相关推荐

    cxf发布webservice示例

    webservice示例 springmvc+maven+cxf发布webservice 博客地址 http://blog.csdn.net/joe_storm/article/details/78839150

    Spring+CXF 发布WebService服务

    本文将深入探讨如何使用Spring和CXF来发布WebService服务。 首先,Spring是一个开源的Java平台,它提供了全面的编程和配置模型,用于简化企业级应用的开发。Spring框架的核心特性包括依赖注入、面向切面编程(AOP)...

    使用CXF发布WebService

    当我们谈论“使用CXF发布WebService”时,我们实际上是在讨论如何利用Apache CXF框架创建和部署Web服务。Apache CXF是一个开源的Java框架,专门用于构建和消费Web服务,支持多种协议,如SOAP和RESTful。 首先,我们...

    用cxf发布webservice所需的基础jar包

    在标题中提到的"用cxf发布webservice所需的基础jar包",这是指运行CXF Web服务所需的最小依赖集合。这些Jar包主要包括以下几个部分: 1. **CXF核心库**:这是CXF框架的核心组件,包含了处理Web服务请求和响应的基本...

    CXF发布WebService加入拦截器

    当我们谈论"CXF发布WebService加入拦截器"时,这涉及到在CXF服务端和客户端增加拦截器来增强服务功能和控制流程。拦截器是CXF框架中的一个重要组件,它们提供了在消息发送和接收过程中插入自定义逻辑的能力。 一、...

    CXF发布WebService,jboss和tomcat都能发布

    ### 一、CXF WebService发布 1. **CXF环境搭建** 在开始之前,确保你已经安装了Java Development Kit (JDK) 和 Maven 或 Gradle(根据项目管理工具选择)。然后,通过Maven或Gradle将CXF库添加到项目的依赖中。 2...

    SpringBoot框架及CXF发布WebService

    在给定的压缩包文件中,"WebService_Server"可能包含了SpringBoot与CXF集成的服务器端代码示例,而"Webservice_Client"则可能包含CXF客户端调用服务的示例代码。这两个部分可以作为学习和实践SpringBoot发布和消费...

    springboot整合CXF发布webservice和客户端调用

    通过这个项目,开发者不仅可以了解SpringBoot和CXF的基本概念,还能掌握两者如何协同工作,发布和调用Web服务。同时,对于SpringBoot应用的打包、部署和测试也有了一定的认识。这个例子是一个理想的实践项目,对于...

    CXF 发布WebService DEMO

    CXF方式实现的WebService 接口的发布

    springboot+CXF发布webservice接口

    本教程将详细介绍如何在Spring Boot项目中集成CXF来发布Web服务接口。 首先,我们需要确保项目中包含了Spring Boot和CXF的相关依赖。在`pom.xml`文件中,我们需要添加以下依赖: ```xml &lt;groupId&gt;org.spring...

    通过cxf发布webservice需要的一些jar

    本篇文章将详细介绍通过Apache CXF发布Web服务时所需的jar文件以及相关的知识点。 首先,我们需要理解Web服务的本质。Web服务是一种基于互联网的软件应用程序,它能够通过标准协议(如SOAP或REST)与其他应用进行...

    CXF发布WebService的多种方法实例

    该websevice项目发布方式 1、直接运行com.demo.desk包下的各种发布方式,共3种方法(注意way3需要cxf-servlet.xml和wsdl) ...ps:发布到服务器的WebService地址为:http://localhost:8080/CXFDemo/HelloCXF?wsdl

    springboot整合cxf发布webservice以及调用的方法

    SpringBoot整合CXF发布WebService以及调用的方法 SpringBoot是一款流行的Java框架,而CXF(Apache CXF)是一款开源的WebService框架。今天,我们将介绍如何使用SpringBoot整合CXF发布WebService,以及如何调用它。 ...

    Spring+CXF发布webservice

    【Spring+CXF发布Web服务】是将Spring框架与Apache CXF结合,用于构建和部署Web服务的一个常见实践。在本文中,我们将深入探讨这个主题,了解如何利用这两个强大的工具来创建、配置和运行Web服务。 Spring框架是...

    cxf发布webservice

    CXF发布Web服务是Java开发中一种常见的方法,它允许开发者将Java接口转换为符合SOAP标准的Web服务。Apache CXF是一个开源的Web服务框架,它提供了丰富的功能,包括SOAP、RESTful API、WS-*协议支持等。在这个过程中...

    spring整合cxf发布webservice实例

    将下载的demo(包括serviceserverdemo及serviceclientdemo,bat文件在serviceclientdemo的src下)导入eclipse即可运行使用,编译时可能需要修改jdk版本,如果导入有错,可新建web项目,按所下载demo的结构搭建即可,...

    使用CXF和camel-cxf调用webservice

    Apache CXF和camel-cxf是两个流行的开源框架,它们可以帮助开发者轻松地创建、消费和集成Web服务。本篇文章将详细介绍如何使用CXF和camel-cxf调用Web服务,以及这两个工具的核心功能和使用场景。 Apache CXF是一个...

    Java cxf开发webservice,分别有客户端和服务端

    2.用cxf开发webservice 3.这个服务端和客户端的小demo 在服务端 对外开放接口服务,然后在客户端 调用服务端的方法, 实现客户端(一个javaweb项目)对服务端(javaweb项目)方法的调用, 实际上就是发送和接收消息...

    基于spring3.0的cxf发布webservice+client的

    本篇文章将围绕“基于Spring 3.0的CXF发布Web Service及客户端”的主题展开,详细介绍如何利用这两个强大的工具来实现服务的发布和调用。 首先,我们需要理解Spring 3.0的关键特性。Spring 3.0引入了更多的模块和...

Global site tag (gtag.js) - Google Analytics