设为首页 收藏本站
查看: 874|回复: 0

[经验分享] Apache CXF实现Web Service(1)——不借助重量级Web容器和Spring实现一个纯的JAX-WS web service

[复制链接]

尚未签到

发表于 2017-1-15 07:03:31 | 显示全部楼层 |阅读模式
废话少说,先在Eclipse中新建一个Java Project
  (可以不是WTP的Dynamic Web Project)
  
DSC0000.png
 选择Java Project
  
DSC0001.png
 

再看pom.xml
  我们使用cxf 3.1.4版本,

<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/maven-v4_0_0.xsd">  
<modelVersion>4.0.0</modelVersion>  
<groupId>com.cnblog.richaaaard.cxfstudy</groupId>  
<artifactId>cxf-test-standalone-ws-helloworld</artifactId>  
<packaging>war</packaging>  
<version>1.0-SNAPSHOT</version>  
<name>cxf-test-standalone-ws-helloworld Maven Webapp</name>  
<url>http://maven.apache.org</url>  
<properties>  
<!-- <cxf.version>2.7.18</cxf.version> -->
<!-- 注意使用2.x.x版本的同学,下面例子中引入cxf-bundle-jaxrs是不必要的,因为jaxws启动一个服务需要类JaxWsServerFactoryBean在cxf-rt-frontend-jaxws.jar中-->

<cxf.version>3.1.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</artifactId>  
<version>${cxf.version}</version>  
</dependency>  
<dependency>  
<groupId>org.apache.cxf</groupId>  
<artifactId>cxf-rt-transports-http-jetty</artifactId>  
<version>${cxf.version}</version>  
</dependency>  
<dependency>  
<groupId>org.apache.cxf</groupId>  
<artifactId>cxf-rt-ws-security</artifactId>  
<version>${cxf.version}</version>  
</dependency>  
<dependency>  
<groupId>org.apache.cxf</groupId>  
<artifactId>cxf-rt-ws-policy</artifactId>  
<version>${cxf.version}</version>  
</dependency>  
<!--         <dependency>   -->
<!--             <groupId>org.apache.cxf</groupId>   -->
<!--             <artifactId>cxf-bundle-jaxrs</artifactId>   -->
<!--             <version>${cxf.version}</version>   -->
<!--         </dependency>   -->
<dependency>  
<groupId>javax.ws.rs</groupId>  
<artifactId>jsr311-api</artifactId>  
<version>1.1.1</version>  
</dependency>  
<dependency>  
<groupId>org.slf4j</groupId>  
<artifactId>slf4j-api</artifactId>  
<version>1.5.8</version>  
</dependency>  
<dependency>  
<groupId>org.slf4j</groupId>  
<artifactId>slf4j-simple</artifactId>  
<version>1.5.8</version>  
</dependency>  
<dependency>  
<groupId>commons-httpclient</groupId>  
<artifactId>commons-httpclient</artifactId>  
<version>3.0</version>  
</dependency>  
<dependency>  
<groupId>commons-io</groupId>  
<artifactId>commons-io</artifactId>  
<version>2.3</version>  
</dependency>  
<dependency>  
<groupId>junit</groupId>  
<artifactId>junit</artifactId>  
<version>4.8.1</version>  
<scope>test</scope>  
</dependency>  
</dependencies>  
<build>  
<finalName>cxfstudy</finalName>  
<resources>  
<resource>  
<directory>src/main/resources</directory>  
</resource>  
<resource>  
<directory>src/main/java</directory>  
<includes>  
<include>**</include>  
</includes>  
<excludes>  
<exclude>**/*.java</exclude>  
</excludes>  
</resource>  
</resources>  
<plugins>  
<plugin>  
<groupId>org.mortbay.jetty</groupId>  
<artifactId>maven-jetty-plugin</artifactId>  
<configuration>  
<contextPath>/</contextPath>  
<connectors>  
<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">  
<port>9000</port>  
</connector>  
</connectors>  
</configuration>  
</plugin>  
<plugin>  
<groupId>org.apache.maven.plugins</groupId>  
<artifactId>maven-compiler-plugin</artifactId>  
<configuration>  
<source>1.7</source>  
<target>1.7</target>  
</configuration>  
</plugin>  
</plugins>  
</build>  
</project>
  另外有例子给出的slf4j是slf4j-jdk14 (http://mvnrepository.com/artifact/org.slf4j/slf4j-jdk14)的版本,如果用jdk5以上的同学可以将其替换成slf4j-simple实现。

CXF Web Service (Annotation)实现,上代码
  CXF支持两种方式发布一个web service,一种是Java Annotation(Bottom up),另外一种是先定义好wsdl和xsd schema然后通过工具生成(Top down),我们这里的例子先介绍Java Annotation。
  首先我们需要定义一个web service接口类HelloWorld,并加上Annotation

package com.cnblog.richaaaard.cxftest.standalone.ws.helloworld.services;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
@WebService  
public interface HelloWorld {  
@WebMethod  
@WebResult String sayHi(@WebParam String text);  
}
  在此我们先不介绍 @WebService、 @WebMethod、@WebResult还有@WebParam如何工作的。
  然后我们需要用HelloWorldImpl来实现这个接口类,

package com.cnblog.richaaaard.cxftest.standalone.ws.helloworld.services;
public class HelloWorldImpl implements HelloWorld {  
public String sayHi(String name) {  
String msg = "Hello " + name + "!";  
return msg;  
}  
}

发布一个Web Service
  我们先用一个最简单的方式,使用javax.xml.ws.Endpoint来发布

package com.cnblog.richaaaard.cxftest.standalone.ws.helloworld.server;
import javax.xml.ws.Endpoint;
import com.cnblog.richaaaard.cxftest.standalone.ws.helloworld.services.HelloWorldImpl;
public class SimpleServer {  
public static void main(String[] args) throws Exception {
System.out.println("Starting Server");
HelloWorldImpl implementor = new HelloWorldImpl();
String address = "http://localhost:9000/ws/HelloWorld";
Endpoint.publish(address, implementor);
}  
}
  注意:这里的address路径是大小写敏感的,如果发布成"http://localhost:9000/ws/ helloWorld ",而客户端使用/HelloWorld会找不到服务。
  直接Run As..-> Java Application
  
DSC0002.png
 我们可以在Eclipse中看到一个jetty服务正常运行

Starting Server
128 [main] INFO org.apache.cxf.service.factory.ReflectionServiceFactoryBean - Creating Service {http://services.helloworld.ws.standalone.cxftest.richaaaard.cnblog.com/}HelloWorldImplService from class com.cnblog.richaaaard.cxftest.standalone.ws.helloworld.services.HelloWorld
487 [main] INFO org.apache.cxf.endpoint.ServerImpl - Setting the server's publish address to be http://localhost:9000/ws/HelloWorld
506 [main] INFO org.eclipse.jetty.server.Server - jetty-8.1.15.v20140411
554 [main] INFO org.eclipse.jetty.server.AbstractConnector - Started SelectChannelConnector@localhost:9000
  此时,我们通过浏览器访问http://localhost:9000/ws/HelloWorld?WSDL
  
DSC0003.png
 

如何访问我们发布的这个服务呢?
  我们利用jaxws中的JaxWsProxyFactoryBean来消费这个服务

package com.cnblog.richaaaard.cxftest.standalone.ws.helloworld.client;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.cnblog.richaaaard.cxftest.standalone.ws.helloworld.services.HelloWorld;
public class Client {  
public static void main(String[] args) {  
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();  
factory.getInInterceptors().add(new LoggingInInterceptor());
factory.getOutInterceptors().add(new LoggingOutInterceptor());
factory.setServiceClass(HelloWorld.class);  
factory.setAddress("http://localhost:9000/ws/HelloWorld");  
HelloWorld helloworld = (HelloWorld) factory.create();  
String reply = helloworld.sayHi("HI");
System.out.println("Server said: " + reply);
System.exit(0);
}  
}
  同样右键选择Client.java,Run As.. ->Java Application,我们可以看到运行后台执行的日志最后成功返回“Server said: Hello HI!”

114 [main] INFO org.apache.cxf.service.factory.ReflectionServiceFactoryBean - Creating Service {http://services.helloworld.ws.standalone.cxftest.richaaaard.cnblog.com/}HelloWorldService from class com.cnblog.richaaaard.cxftest.standalone.ws.helloworld.services.HelloWorld
531 [main] INFO org.apache.cxf.services.HelloWorldService.HelloWorldPort.HelloWorld - Outbound Message
---------------------------
ID: 1
Address: http://localhost:9000/ws/HelloWorld
Encoding: UTF-8
Http-Method: POST
Content-Type: text/xml
Headers: {Accept=[*/*], SOAPAction=[""]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHi xmlns:ns2="http://services.helloworld.ws.standalone.cxftest.richaaaard.cnblog.com/"><arg0>HI</arg0></ns2:sayHi></soap:Body></soap:Envelope>
--------------------------------------
748 [main] INFO org.apache.cxf.services.HelloWorldService.HelloWorldPort.HelloWorld - Inbound Message
----------------------------
ID: 1
Response-Code: 200
Encoding: UTF-8
Content-Type: text/xml;charset=UTF-8
Headers: {Content-Length=[258], content-type=[text/xml;charset=UTF-8], Server=[Jetty(8.1.15.v20140411)]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHiResponse xmlns:ns2="http://services.helloworld.ws.standalone.cxftest.richaaaard.cnblog.com/"><return>Hello HI!</return></ns2:sayHiResponse></soap:Body></soap:Envelope>
--------------------------------------
Server said: Hello HI!
  上面Outbound Message和Inbound Message是我们加的两个Logging Interceptor输出的日志,关于Interceptor的原理和更多使用方式,会专题介绍。

这样就完成了一个Web Service的发布和消费

*服务端的扩展
  除了用一个最简单的javax.xml.ws.Endpoint来发布CXF web service,我们还可以使用cxf-rt-frontend-jaxws.jar中的JaxWsServerFactoryBean来发布一个web service,它可以让我们更多的控制web service的行为,比如说加Logging Interceptor之类

package com.cnblog.richaaaard.cxftest.standalone.ws.helloworld.server;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import com.cnblog.richaaaard.cxftest.standalone.ws.helloworld.services.HelloWorld;
import com.cnblog.richaaaard.cxftest.standalone.ws.helloworld.services.HelloWorldImpl;
public class Server {  
public static void main(String[] args) throws Exception {  
JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();  
factory.setServiceClass(HelloWorld.class);  
HelloWorldImpl implementor = new HelloWorldImpl();
factory.setServiceBean(implementor);
factory.getInInterceptors().add(new LoggingInInterceptor());
factory.getOutInterceptors().add(new LoggingOutInterceptor());
factory.setAddress("http://localhost:9000/ws/HelloWorld");  
factory.create();  
System.out.println("Server start...");  
Thread.sleep(60 * 1000);  
System.out.println("Server exit...");  
System.exit(0);
}  
}
  参考:
  http://blog.csdn.net/kongxx/article/details/7525476
  https://cwiki.apache.org/confluence/display/CXF20DOC/A+simple+JAX-WS+service

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-328474-1-1.html 上篇帖子: Apache下的开设虚拟主机设置--添加站点2008-01-07 20:27Apache下的开设虚拟主机设置--添加站点 下篇帖子: 深入解析Apache Mina源码(4)——Mina编解码以及对粘包和断包的处理
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表