`
duanfei
  • 浏览: 719971 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

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

package com.jshx.http.ws;

public interface ReportService {
	String queryDate(String jsonStr); 
}

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

package com.jshx.http.ws.impl;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;

import com.jshx.http.ws.ReportService;

@WebService
@SOAPBinding(style = SOAPBinding.Style.RPC)
public class ReportServiceImpl implements ReportService {

	@WebMethod
	public String queryDate(@WebParam(name="jsonStr") String jsonStr) {
		return "hi," + jsonStr + " welcom to www.micmiu.com";
	}

}

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

package com.jshx.http.ws;

import javax.xml.ws.Endpoint;

import com.jshx.http.ws.impl.ReportServiceImpl;

public class WsServerStart {
	public static void main(String[] args) {
		ReportService ws = new ReportServiceImpl();
		Endpoint.publish("http://localhost:8080/ReportServer", ws);
	}
}

 浏览器打开: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

package com.jshx.http.ws.client;

public class ReportClient {
	public static void main(String[] args) {
		ReportServiceImplService service = new ReportServiceImplService();
		ReportServiceImpl report = service.getReportServiceImplPort();
		System.out.println("start webservice client ...");
		System.out.println("send Michael to server ");
        System.out.println(report.queryDate("Michael"));
        System.out.println("test client end.");

	}
}

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

 

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类
 
package com.wx.jaxws.example;
//import javax.jws.WebMethod;
//import javax.jws.WebService;
//import javax.jws.soap.SOAPBinding;
//@WebService(serviceName = "HelloService", portName = "HelloServicePort", targetNamespace = "http://example.jaxws.wx/jaxws/MyFirstOne")
//@SOAPBinding(style = SOAPBinding.Style.DOCUMENT,
 use = SOAPBinding.Use.LITERAL, parameterStyle = SOAPBinding.ParameterStyle.WRAPPED)
public class MyFirstOne { 
//@WebMethod public String sayHello(String s) {  
System.out.println("hello," + s); 
 return "hello," + s; 
}
}
 
有很多被注释的地方不管.这个类很简单 就是接收一个字符串 打印并且返回
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包里
  • 大小: 110.8 KB
  • 大小: 86.5 KB
分享到:
评论
1 楼 gengjunshi 2016-02-02  
非常感谢哈,刚好在学webservice编程,很有用呢。

相关推荐

Global site tag (gtag.js) - Google Analytics