JAX-WS开发webservice示例详解

目录:

  • 概述
  • 实验环境
  • 服务端的实现
  • 客户端的实现

[一]、概述
Java API for XML Web Services (JAX-WS)是Java程序设计语言一个用来创建Web服务的API。

在服务器端,用户只需要通过Java语言定义远程调用所需要实现的接口SEI(service endpoint interface),并提供相关的实现,通过调用JAX-WS的服务发布接口就可以将其发布为WebService接口。

在客户端,用户可以通过JAX-WS的API创建一个代理(用本地对象来替代远程的服务)来实现对于远程服务器端的调用。

当然 JAX-WS 也提供了一组针对底层消息进行操作的API调用,你可以通过Dispatch 直接使用SOAP消息或XML消息发送请求或者使用Provider处理SOAP或XML消息。

JAX-WS2.0 (JSR 224 )是Sun新的web services协议栈,是一个完全基于标准的实现。在binding层,使用的是the Java Architecture for XML Binding (JAXB, JSR 222 ),在parsing层,使用的是the Streaming API for XML (StAX, JSR 173 ),同时它还完全支持schema规范。

JAX-WS与JAX-RPC的区别 可参见:http://java.sun.com/xml/faq.html#JAX-WS-and-JAX-RPC-difference

JAX-WS一些参考资料:

  1. JAX-RPC 2.0 renamed to JAX-WS 2.0
  2. The Java web service Tutorial
  3. javax.jws.WebService

[二]、实验环境

  • java version “1.6.0_18″、Eclipse3.7
  • maven构建项目:mvn archetype:create -DgroupId=com.micmiu.jaxws.demo -DartifactId=jaxws-demo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

[三]、服务端的实现

1.最基本的实例

编写接口代码:ReportService.java

Reportservice代码   收藏代码
  1. package com.jshx.http.ws;  
  2.   
  3. public interface ReportService {  
  4.     String queryDate(String jsonStr);   
  5. }  

 实现接口并添加webservice注释:ReportServiceImpl.java

Java代码   收藏代码
  1. package com.jshx.http.ws.impl;  
  2.   
  3. import javax.jws.WebMethod;  
  4. import javax.jws.WebParam;  
  5. import javax.jws.WebService;  
  6. import javax.jws.soap.SOAPBinding;  
  7.   
  8. import com.jshx.http.ws.ReportService;  
  9.   
  10. @WebService  
  11. @SOAPBinding(style = SOAPBinding.Style.RPC)  
  12. public class ReportServiceImpl implements ReportService {  
  13.   
  14.     @WebMethod  
  15.     public String queryDate(@WebParam(name="jsonStr") String jsonStr) {  
  16.         return "hi," + jsonStr + " welcom to www.micmiu.com";  
  17.     }  
  18.   
  19. }  

 编写服务端发布代码:WsServerStart .java

Java代码   收藏代码
  1. package com.jshx.http.ws;  
  2.   
  3. import javax.xml.ws.Endpoint;  
  4.   
  5. import com.jshx.http.ws.impl.ReportServiceImpl;  
  6.   
  7. public class WsServerStart {  
  8.     public static void main(String[] args) {  
  9.         ReportService ws = new ReportServiceImpl();  
  10.         Endpoint.publish("http://localhost:8080/ReportServer", ws);  
  11.     }  
  12. }  

 浏览器打开:http://localhost:8080/ReportServer?wsdl 回车显示如下:

 

可见服务端已经发布成功。

如果出现问题:

 

原因:cxf需要jaxws-api-2.1.jar及jaxb-api-2.1.jar的支持

 

解决方案

1、将cxf所需的2.1的jar复制一份到jdk目录下的jre\lib\endorsed文件夹中。如果endorsed文件不存在,可新建。如果不行,可能还需要在public class XXX上方加入@SOAPBinding(style = SOAPBinding.Style.RPC)
2、jdk升级到1.6.0_22版本以上。

 

二、MyEclipse利用网上公开发布WSDL文件,创建WebService Client,进行调用WebService

1. 打开MyEclipse,新建一个Web Project;然后新建 一个package,取名为com.test;

2. 然后再New一个Web Service Client;

点next ,然后录入 WSDL URL: http://localhost:8080/ReportServer?wsdl

点next,点finish;ok了,系统会自动帮忙生成很多代码。

 

2.编写客户端测试程序ReportClient.java

Java代码   收藏代码
  1. package com.jshx.http.ws.client;  
  2.   
  3. public class ReportClient {  
  4.     public static void main(String[] args) {  
  5.         ReportServiceImplService service = new ReportServiceImplService();  
  6.         ReportServiceImpl report = service.getReportServiceImplPort();  
  7.         System.out.println("start webservice client ...");  
  8.         System.out.println("send Michael to server ");  
  9.         System.out.println(report.queryDate("Michael"));  
  10.         System.out.println("test client end.");  
  11.   
  12.     }  
  13. }  

 运行测试程序,日志如下:

 

start webservice client ...
send Michael to server
hi,Michael welcom to www.micmiu.com
test client end.

 可见客户端调用成功。

 

 

另一种方法:

1 MyFirstJAX-WS
2.建一个包<包名根据自己需要来我这里是com.wx.jaxws.example>
  在里面建立一个java类
 
Java代码   收藏代码
  1. package com.wx.jaxws.example;  
  2. //import javax.jws.WebMethod;  
  3. //import javax.jws.WebService;  
  4. //import javax.jws.soap.SOAPBinding;  
  5. //@WebService(serviceName = "HelloService", portName = "HelloServicePort", targetNamespace = "http://example.jaxws.wx/jaxws/MyFirstOne")  
  6. //@SOAPBinding(style = SOAPBinding.Style.DOCUMENT,  
  7.  use = SOAPBinding.Use.LITERAL, parameterStyle = SOAPBinding.ParameterStyle.WRAPPED)  
  8. public class MyFirstOne {   
  9. //@WebMethod public String sayHello(String s) {    
  10. System.out.println("hello," + s);   
  11.  return "hello," + s;   
  12. }  
  13. }  
 
有很多被注释的地方不管.这个类很简单 就是接收一个字符串 打印并且返回
3.建立服务
右键点击项目File->New->others->Myeclipse->Web Service->Web Service 
点击之后出现的屏幕,在 Strategy 中选择--> <Create web service from Java Bean>Bottom-up scenario
点击next 在接下来的对话框中 输入类的名字 并且找到它  点击FINISH
因为我们已经建立好了类而且想根据它建立JAX-WS服务。
会生成一个 类名+Delegate.java的文件  我这里生成的是 MyFirstOneDelegate.java
 
4.到上一步已经做好了WebService 接下来我们做发布
在项目名称上右击->properties->点击左边的 Java Build Path->选择选项卡 Libraries->点击右边的按钮 Add Library->
选择 Myeclipse Libraries->勾选 JAX-WS 2.1 Runtime Labraries(Project Metro 1.1) 和 JAX-WS 2.1 API Labraries
<其实就是最后面的两个>
 
5.导入几个Jar包<我已经放在目录里了> 其实这几个Jar包在 
MyEclipse 6.5\myeclipse\eclipse\plugins\com.genuitec.eclipse.ws.xfire_6.5.1.zmyeclipse650200806\lib
的目录下也已经存在 不过我们要导入项目中
其实要的是这个包webservices-tools.jar
 
6.然后可以运行了 URL和xfire不一样
http://127.0.0.1:8080/MyFirstJAX-WS/MyFirstOnePort?wsdl
是项目名/类名Port?wsdl
正确出现xml文档则表示ok!
 
---------------------使用-----------------------
7.新建一个工程 java工程也好 web工程也好
我这里方便测试建的java工程 MyTestJAX-WS_New
右键点击项目File->New->others->Myeclipse->Web Service->Web Service Client 
点击next 选择JAX-WS ->选择 WSDL URL 在这个里面输入刚才测试通过的URL 然后点击next等处理一下点击FINISH就好了
会生成很多类  这里我让自动生成的全在test包里
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值