`

webservice cxf简单案例

 
阅读更多
cxf简单java项目案例

IHelloWorld.java
package com.chen.ws;

import java.util.List;
import java.util.Map;

import javax.jws.WebService;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

import com.chen.adapter.AuthXmlAdapter;
import com.chen.pojo.Pet;
import com.chen.pojo.User;

@WebService
public interface IHelloWorld {
	public String hello();
	
	public List<Pet> getPetsByUser(User user);
	
	/*
	 * cxf不直接Map<String,Pet>类型,需要自己实现转换器
	 */
	@XmlJavaTypeAdapter(AuthXmlAdapter.class)public Map<String,Pet> getAllPets();
}



实现类HelloWorldImpl.java
package com.chen.ws.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.jws.WebService;

import com.chen.pojo.Pet;
import com.chen.pojo.User;
import com.chen.service.UserService;
import com.chen.ws.IHelloWorld;
@WebService(endpointInterface="com.chen.ws.IHelloWorld",
			serviceName="HelloWorldImpl")
public class HelloWorldImpl implements IHelloWorld {

	@Override
	public String hello() {
		return "hello world! date:"+new Date();
	}

	@Override
	public List<Pet> getPetsByUser(User user) {
		UserService userService = new UserService();
		return userService.getPetsByUser(user);
	}

	@Override
	public Map<String, Pet> getAllPets() {
		UserService userService = new UserService();
		return userService.getAllPets();
	}

}



发布服务demo.java
package com.chen.demo;

import java.io.IOException;
import java.io.PrintWriter;

import javax.xml.ws.Endpoint;

import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.EndpointImpl;

import com.chen.interceptor.AuthInterceptor;
import com.chen.ws.IHelloWorld;
import com.chen.ws.impl.HelloWorldImpl;

public class ServiceMain {
	public static void main(String[] args) throws IOException{
		IHelloWorld hw = new HelloWorldImpl();
		EndpointImpl ep = (EndpointImpl) Endpoint.publish("http://132.126.1.98/helloDemo", hw);
		
		//PrintWriter pi = new PrintWriter("D:/chen/work/framework/cxf_demo01/src/in.txt");
		//PrintWriter po = new PrintWriter("D:/chen/work/framework/cxf_demo01/src/out.txt");
		//添加拦截器
		//ep.getInInterceptors().add(new LoggingInInterceptor());
		//ep.getOutInterceptors().add(new LoggingOutInterceptor());
		ep.getInInterceptors().add(new AuthInterceptor());
		
		System.out.println("发布成功!");
	}
}



客户端
通过cmd,到指定目录执行 wsdl2java http://132.126.1.98/helloDemo?wsdl
得到客户端相关代码

客户端调用服务Demo.java
package com.chen.demo;

import java.util.List;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.interceptor.LoggingOutInterceptor;

import com.chen.interceptor.AuthInterceptor;
import com.chen.ws.Entry;
import com.chen.ws.IHelloWorld;
import com.chen.ws.Pet;
import com.chen.ws.StringPet;
import com.chen.ws.User;
import com.chen.ws.impl.HelloWorldImpl;

public class ClientMain {
	public static void main(String[] args) {
		HelloWorldImpl factory = new HelloWorldImpl();
		IHelloWorld hw = factory.getHelloWorldImplPort();
		
		//添加拦截器
		Client client = ClientProxy.getClient(hw);
		//client.getInInterceptors().add(new LoggingInInterceptor());
		client.getOutInterceptors().add(new LoggingOutInterceptor());
		client.getOutInterceptors().add(new AuthInterceptor("chen","chen"));
		
		System.out.println(hw.hello());

		User u1 = new User();
		u1.setId(1L);
		u1.setName("chen1");
		List<Pet> pets = hw.getPetsByUser(u1);
		for(Pet obj : pets){
			System.out.println(obj.getName());
		}
		
		StringPet res  = hw.getAllPets();
		for(Entry entry : res.getEntries()){
			System.out.println(entry.getKey()+": "+entry.getValue().getId()+" "+entry.getValue().getName());
		}
		
	}
}





cxf+spring
服务器端

applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:p="http://www.springframework.org/schema/context"   
    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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd  
        http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
    	">
    <!-- (4)、导入cxf提供的Schema,XML配置文件 jaxws-->
    <!-- web应用的类加载路径classpath有两类:
    	A、WEB-INF/classes目录
    	B、WEB-INF/lib目录下
     -->
    <!-- (5)、导入cxf配置文件 -->
    <import resource="classpath:META-INF/cxf/cxf.xml"/>
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
    
    <!-- (6)、配置endpoint 
    	implementor 服务提供者
    		两种形式
    		A:直接给定服务提供者的类名(不能使spring自动注入) com.chen.ws.impl.HelloWorldImpl
    		B:设置为一个bean引用,要在bean的id前加上#
    	address 发布地址
    -->
    <bean id="helloWorld" class="com.chen.ws.impl.HelloWorldImpl">
    	<property name="userService" ref="userService" />
    </bean>
    <jaxws:endpoint 
    	implementor="#helloWorld" 
    	address="/helloDemo">
    	<!-- (8)、添加拦截器 -->
    	<jaxws:inInterceptors>
    		<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
    	<bean class="com.chen.interceptor.AuthInterceptor"></bean> 
    	</jaxws:inInterceptors>
    </jaxws:endpoint>
    
    <!-- (7)、配置业务层service bean -->
    <bean id="userService" class="com.chen.service.UserService"/>
    
    
    	
</beans>



客户端
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:p="http://www.springframework.org/schema/context"   
    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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd  
        http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
    	">
    <!-- (4)、导入cxf提供的Schema,XML配置文件 jaxws-->
    <!-- web应用的类加载路径classpath有两类:
    	A、WEB-INF/classes目录
    	B、WEB-INF/lib目录下
     -->
    <!-- (5)、导入cxf配置文件 -->
    <import resource="classpath:META-INF/cxf/cxf.xml"/>
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
    
    <!-- (6)、配置client 
    	 
    -->
    <jaxws:client id="hw" 
     		serviceClass="com.chen.ws.IHelloWorld"
     		address="http://132.126.1.98/helloDemo">
    	<jaxws:outInterceptors>
    		<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
    		<bean class="com.chen.interceptor.AuthInterceptor">
    			<constructor-arg value="chen"/>
    			<constructor-arg value="chen"/>
    		</bean>
    	</jaxws:outInterceptors>
    </jaxws:client> 
    
     
    
    
    	
</beans>


测试demo.java
package com.chen.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.chen.ws.IHelloWorld;

@Controller
@RequestMapping("/demo")
public class DemoController {
	@Autowired
	private IHelloWorld hw ;
	 
	@RequestMapping("hello")
	public String hello(){
		
		hw.hello();
		
		return "hello";
	}
	
}



详情看源码
分享到:
评论

相关推荐

    webservice_cxf Demo

    这是一个webservice(Apache CXF) 简单案例

    cxf 小案例 java webservice

    cxf 小案例 java webservice

    Webservice笔记含使用cxf和jaxws两种方式开发webservice【源代码+笔记】

    第一天: 什么是webservice? 从案例(便民查询网站)分析如何实现? 使用socket实现。 使用jaxws开发webservice。 Webservice三要素 ... CXF发布rest的webservice。(重点) 综合案例: 实现便民查询网站

    CXF搭建webservice案例

    CXF搭建webservice案例,适合初学者

    JDK+CXF实现webservice简单案例

    通过JDK+CXF结合开发一个简单的webservice学习案例,下载下来后需要自己修改发布的IP地址为自己的端口,然后发布,以及通过新的WSDL文件从新生成客户端代码才能实现客户端、服务端的交互

    cxf实现webservice的项目实例

    结合网上资源用cxf实现的webservice实例。 参考 http://blog.csdn.net/hu_shengyang/article/details/38384597

    ssh整合cxf(webservice)

    ssh框架整合cxf(webservice),ssh案例(增、删、改、查),发布webservice,客户端调用,该工程自带jar包,mysql连接池,自动建库、建表。 ①直接解压,导入ssh2cxf项目 ②用tomcat启动ssh2cxf项目 ③在浏览器输入...

    CXF 使用实例集成

    CXF webservice 使用实例,CXF创建webservice以及调用示例;WEBSERVICE输入、输出拦截器设置;用户验证;文件上传等。附带所有使用到的JAR包。

    WebSevice(CXF)入门案例java工程代码

    WebService入门案例,使用CXF框架,与Spring进行整合,包括客户端工程和服务端工程。

    cxf webservice能运行起来的demo

    cxf webservice能运行起来的简单案例,包括jar包都在里面。

    WebService入门案例java工程代码

    该工程代码包含基于使用CXF框架开发的入门案例 和进阶案例(与Spring进行整合)

    Cxf+Spring集成案例代码

    Spring是目前最流行的JavaEE Framework,但是使用Spring的Spring-WS开发WebService却...CXF是一个简化WebService开发的开源项目,通过Spring和CXF的结合可以大大简化基于Spring Framework的应用中的WebService开发。

    apache-cxf web服务实例

    基于maven的webservice例子,服务端发布和客户端调用代码,实现对mysql数据库表的curd操作

    基于Apache CXF构建SOA应用 随书源代码

    本书主要介绍Apache CXF在构建SOA架构各个方面的应用说明和编程案例。覆盖以下内容:基于JAX-WS规范和CXF自身的前端模式实现,CXF支持的数据绑定(DataBindings),CXF支持的WSDL绑定,CXF支持的传输协议绑定。CXF的...

    apache-cxf-3.0.15.zip

    完整的cxf-3.0.15webservice服务端jar包。包含使用案例。

    基于xml的webservice接口demo (Maven)管理

    这是一个cxf的webservice接口。 简单,可以直接用。 当时自己搭框架的时候,出现了各种问题,最后自己处理了一个。感觉还可以。 这是一个xml的处理项目。 可伸展性强

Global site tag (gtag.js) - Google Analytics