您的位置:首页 > 运维架构 > 网站架构

[笔记-架构探险]web服务框架-cxf-4.1.多种方式的发布与客户端调用

2015-11-26 20:46 781 查看
赤裸裸的全是代码,发布配置,客户端配置使用


cxf是两个开源框架的整合,Celtix 与XFire。而且Axis在2012年貌似就没有更新了。可见cxf的强大

Jdk接口Jax-ws的具体实现是Oracle JAX-WS Ri。RI的发布就直接跳过了。不记录了,也不实验了直接从cxf开始

使用cxf内置的Jetty来发布ws

pom依赖

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<cxf.version>3.0.4</cxf.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>${cxf.version}</version>
</dependency>
</dependencies>


//接口
//@WebService
public interface HelloService {
String say(String name);
}
//实现,
//@WebService
public class HelloServiceImpl implements HelloService {
public String say(String name) {
return "hello " + name;
}
}
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

/**
* @author zhuqiang
* @version V1.0
* @date 2015/11/25 21:55
*/
public class Client {
public static void main(String[] args) {
//jax-ws 的发布方式 ,其他的配置项都是默认的,这种方式产生的 wsdl文件内容要比它的父类少很多
JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean(); //继承自ServerFactoryBean。
factory.setAddress("http://localhost:8080/ws/soap/hello");
factory.setServiceClass(HelloServiceImpl.class);
factory.setServiceBean(new HelloServiceImpl());
factory.create();
System.out.println("soap ws is published");

//simple 发布
//        ServerFactoryBean factory = new ServerFactoryBean();
//        factory.setAddress("http://localhost:8080/ws/soap/hello");
//        factory.setServiceClass(HelloServiceImpl.class);
//        factory.setServiceBean(new HelloServiceImpl());
//        factory.create();
//        System.out.println("soap ws is published");
}
}


使用Tmcat和Spring发布服务ws

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>

<groupId>cn.mrcode.study</groupId>
<artifactId>cxf001</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<cxf.version>3.0.4</cxf.version>
<spring.version>4.1.4.RELEASE</spring.version>
</properties>
<dependencies>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf.version}</version>
</dependency>
</dependencies>

</project>


spring.xml

<?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:context="http://www.springframework.org/schema/context"
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"> <context:component-scan base-package="cxf.ws"/>
<import resource="spring-cxf.xml"/>
</beans>


spring-cxf

<?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"
xmlns:simple="http://cxf.apache.org/simple"
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 http://cxf.apache.org/simple http://cxf.apache.org/schemas/simple.xsd"> <bean id="helloServiceImpl" class="cxf.ws.HelloServiceImpl"/>
<!--方式1:jaxws:server -->
<jaxws:server id="helloService" address="/soap/hello">
<jaxws:serviceBean>
<ref bean="helloServiceImpl"/>
</jaxws:serviceBean>
</jaxws:server>
<!-- 方式2:jaxws:endpoint, cxf 可以使用#简写的形式 -->
<jaxws:endpoint implementor="#helloServiceImpl" address="/soap/helloEnt"/>
<!-- 方式3:simple  发布方式 -->
<simple:server address="/soap/helloSimp">
<simple:serviceBean>
<ref bean="helloServiceImpl"/>
</simple:serviceBean>
</simple:server>
</beans>


wxb.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name>cxf001</display-name>
<!-- spring -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- cxf -->
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
</web-app>


ws接口和实现还是用的之间的helloService;

cxf提供的客户端

静态代理客户端

需要客户端调用者通过wsdl生成jar包,或则拥有目标接口的类文件.下面三种方式发布的 都适用

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

/**
* @author zhuqiang
* @version V1.0
* @date 2015/11/26 19:47
*/
public class Client {
public static void main(String[] args) {
String url1 = "http://localhost:8080/ws/soap/helloSimp?wsdl"; //simple:server
String url2 = "http://localhost:8080/ws/soap/helloEnt?wsdl"; //jaxws:endpoint
String url3 = "http://localhost:8080/ws/soap/hello"; //jaxws:server
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setAddress(url3);
//        factory.setServiceClass(HelloService.class);
HelloService helloService = factory.create(HelloService.class); //看到啦,需要指定接口的class
String word = helloService.say("word");
System.out.println(word);
}
}


动态代理客户端1

底层使用jdk的动态代理特性完成的。指需要你知道该wsdl地址和发布的接口方法签名就能使用了

JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
String url1 = "http://localhost:8080/ws/soap/helloSimp?wsdl"; //simple:server
String url2 = "http://localhost:8080/ws/soap/helloEnt?wsdl"; //jaxws:endpoint
String url3 = "http://localhost:8080/ws/soap/hello?wsdl"; //jaxws:server
org.apache.cxf.endpoint.Client client = factory.createClient(url1); //该动态代理能用于:simple:server 和 jaxws:endpoint 发布的服务
try {
Object[] result = client.invoke("say", "word"); //方法名称;对应的参数
System.out.println(Arrays.toString(result));
} catch (Exception e) {
e.printStackTrace();
}


动态代理客户端2

书上说:这种方式比上面的方式更智能,经过测试。两个能适用的服务都一样的。没看出什么来喔。

String url1 = "http://localhost:8080/ws/soap/helloSimp?wsdl"; //simple:server
String url2 = "http://localhost:8080/ws/soap/helloEnt?wsdl"; //jaxws:endpoint
String url3 = "http://localhost:8080/ws/soap/hello?wsdl"; //jaxws:server
DynamicClientFactory factory = DynamicClientFactory.newInstance();
org.apache.cxf.endpoint.Client client = factory.createClient(url2);
try {
Object[] result = client.invoke("say", "word"); //方法名称;对应的参数
System.out.println(Arrays.toString(result));
} catch (Exception e) {
e.printStackTrace();
}


基于CXF simple方式的客户端

只适用 simple发布的ws

String url1 = "http://localhost:8080/ws/soap/helloSimp?wsdl"; //simple:server
String url2 = "http://localhost:8080/ws/soap/helloEnt?wsdl"; //jaxws:endpoint
String url3 = "http://localhost:8080/ws/soap/hello?wsdl"; //jaxws:server

ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
factory.setAddress(url1);
HelloService helloService = factory.create(HelloService.class);
String word = helloService.say("word");
System.out.println(word);


基于spring的客户端

JaxWsProxyFactoryBean

jaxws:client

这个配置,只不过是把上面的代码,变成了用spring来创建了。

<?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"
xmlns:simple="http://cxf.apache.org/simple"
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 http://cxf.apache.org/simple http://cxf.apache.org/schemas/simple.xsd"> 
<bean id="factory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="cxf.ws.HelloService"/>   <!-- 使用xml配置的话,这个serviceClass一定需要不然报错哦 -->
<property name="address" value="http://localhost:8080/ws/soap/hello?wsdl"/>
</bean>
<bean id="helloServiceJaxWs" factory-bean="factory" factory-method="create"/>

<jaxws:client id="helloServiceJaxws"
serviceClass="cxf.ws.HelloService" address="http://localhost:8080/ws/soap/helloEnt?wsdl">
</jaxws:client>
</beans>

public class Client {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-cxf-client.xml");
HelloService helloServiceJaxWs = context.getBean("helloServiceJaxWs", HelloService.class);
HelloService helloServiceJaxws = context.getBean("helloServiceJaxws", HelloService.class);
System.out.println(helloServiceJaxWs.say("word helloServiceJaxWs"));
System.out.println(helloServiceJaxws.say("word helloServiceJaxws"));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: