cxf简单java项目案例
IHelloWorld.java
实现类HelloWorldImpl.java
发布服务demo.java
客户端
通过cmd,到指定目录执行 wsdl2java http://132.126.1.98/helloDemo?wsdl
得到客户端相关代码
客户端调用服务Demo.java
cxf+spring
服务器端
applicationContext.xml
客户端
applicationContext.xml
测试demo.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";
}
}
详情看源码