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";
}

}



详情看源码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值