java WebService简单使用案例
首先,建立一个WebService:
package garfield; import javax.jws.WebService;
import javax.xml.ws.Endpoint; @WebService
public class MyJ6WebService {
public String SayHello(String strName) {
return "Hello ,"+strName+"!";
} public static void main(String[] args) {
//发布WebService,注意如果提示:Exception in thread "main" com.sun.xml.internal.ws.server.ServerRtException: Server Runtime Error: java.net.BindException: Address already in use: bind
//则需要修改一下发布端口
Endpoint.publish("http://localhost:8030/garfield.MyJ6WebService", new MyJ6WebService());
System.out.println("WebService was published success !");
}
}
启动Tomcat,然后运行,系统会输出:WebService was published success !,表示WebService已启动。
可以在浏览器中输入地址:http://localhost:8030/garfield.MyJ6WebService?wsdl
显示WebService信息:
<?xml version="1.0" encoding="UTF-8" ?>
- <!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01.
-->
- <!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01.
-->
- <definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://garfield/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://garfield/" name="MyJ6WebServiceService">
- <types>
- <xsd:schema>
<xsd:import namespace="http://garfield/" schemaLocation="http://localhost:8030/garfield.MyJ6WebService?xsd=1" />
</xsd:schema>
</types>
- <message name="SayHello">
<part name="parameters" element="tns:SayHello" />
</message>
+ <message name="SayHelloResponse">
<part name="parameters" element="tns:SayHelloResponse" />
</message>
- <portType name="MyJ6WebService">
- <operation name="SayHello">
<input wsam:Action="http://garfield/MyJ6WebService/SayHelloRequest" message="tns:SayHello" />
<output wsam:Action="http://garfield/MyJ6WebService/SayHelloResponse" message="tns:SayHelloResponse" />
</operation>
</portType>
- <binding name="MyJ6WebServicePortBinding" type="tns:MyJ6WebService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />
- <operation name="SayHello">
<soap:operation soapAction="" />
- <input>
<soap:body use="literal" />
</input>
- <output>
<soap:body use="literal" />
</output>
</operation>
</binding>
- <service name="MyJ6WebServiceService">
- <port name="MyJ6WebServicePort" binding="tns:MyJ6WebServicePortBinding">
<soap:address location="http://localhost:8030/garfield.MyJ6WebService" />
</port>
</service>
</definitions>
在WebService启动的状态下,在命令行中输入:
E:\Temp>wsimport -p garfield.garfieldj6wsclient -keep http://localhost:8030/garf
ield.MyJ6WebService?wsdl
parsing WSDL... generating code... compiling code... E:\Temp>
注意,一定要对应你发布的WebService信息!
然后系统会自动生成相关的接口文件,如下:

新建Java工程,将输出文件引入,然后建立测试类:
package xxh;
import garfield.garfieldj6wsclient.*;
public class WebClient {
/**
* @param args
*/
public static void main(String[] args) {
//创建一个客户端服务对象
MyJ6WebService myJ6WS = new MyJ6WebServiceService().getMyJ6WebServicePort();
//调用服务方法,并得到方法返回值
String strTest = myJ6WS.sayHello("Garfield");
//打印服务的返回值
System.out.println(strTest);
}
}
在WebService运行情况下,运行客户端测试程序,输出:
Hello ,Garfield!
java WebService简单使用案例的更多相关文章
- 主题:Java WebService 简单实例
链接地址:主题:Java WebService 简单实例 http://www.iteye.com/topic/1135747 前言:朋友们开始以下教程前,请先看第五大点的注意事项,以避免不必要 ...
- Java WebService简单使用
一直在写java但从来没有使用webservice,在网上查了下资料写个简单的使用放这里做备份 具体步骤: 1.新建一个java工程在里面写一个类(服务端)如下: package com.webser ...
- java webservice简单的例子
开发环境:eclipse .jdk 创建服务端 demo-webservice 创建类 HelloService.java package com.hundsun.ws.service; import ...
- Java WebService 简单实例使用JDK
原文地址:http://www.cnblogs.com/jasoncc/archive/2011/12/22/2296052.html 什么是WebServices? 它是一种构建应用程序的普 ...
- Java WebService 简单实例
前言:朋友们开始以下教程前,请先看第五大点的注意事项,以避免不必要的重复操作. 一.准备工作(以下为本实例使用工具) 1.MyEclipse10.7.1 2.JDK 1.6.0_22 二.创建服务端 ...
- Java WebService 简单实例[转]
http://www.cnblogs.com/yisheng163/p/4524808.html?utm_source=tuicool 前言:朋友们开始以下教程前,请先看第五大点的注意事项,以避免不必 ...
- Java WebService简单实例
一.准备工作(以下为本实例使用工具) 1.MyEclipse10.7.1 2.JDK 1.6.0_22 二.创建服务端 1.创建[Web Service Project],命名为[TheService ...
- Java WebService 简单实例[转载]
[注意,本文转载自 http://hyan.iteye.com/ ] 一.准备工作(以下为本实例使用工具) 1.MyEclipse10.7.1 2.JDK 1.6.0_22 二.创建服务端 1 ...
- Java WebService 简单实例(转
一.准备工作(以下为本实例使用工具) 1.MyEclipse10.7.1 2.JDK 1.6.0_22 二.创建服务端 1.创建[Web Service Project],命名为[TheService ...
随机推荐
- 史上最全的JavaScript工作笔记
/* * JavaScript查看对象函数 */ function resultTest( obj ){ var resultTest = ''; $.each(obj,function(key,va ...
- webform 转 MVC 飞一般的感觉
前言: 浅谈webform与mvc,让开发变得更加简单,这里主要通过比较webform与mvc的开发方式,以下全属个人看法,不完善的地方可以留言补充. 正文: 废话不多说,直接说工作中经常用到的地方 ...
- HTML 学习整理
一.名词解释 一. HTML : Hypertext Markup Language 超文本标记语言 二. CSS : Cascading Style Sheet 层叠样式表 三. 浏览器: ...
- TCP调试助手
网络开发经常要用到一些TCP&UDP的调试工具,搜集一些备用. 目前总结工具有(不分先后): chrome等自带调试器调试HTTP Fiddler(.NET)和Charles debugger ...
- Visual C++ 打印编程技术-编程基础
背景: windows产生前,操作系统(如DOS等)都不提供支持图像处理的打印机驱动程序,使得程序员为打印出图像,不得不针对使用的打印机 自己编写设备驱动程序,导致了大量的.不必要的重复开发. 随着w ...
- iOS 多张图片保存到相册问题(add multiple images to photo album)
不知道朋友们有木有做过多图保存到系统的相册这个需求,我在用`UIImageWriteToSavedPhotosAlbum`保存图片时候,在代理回调方`didFinishSavingWithError` ...
- ARC和MRC实现单例模式
代码如下,可直接拷贝到头文件中 #define singleton_h(name) +(instancetype)shared##name # if __has_feature(objc_arc) / ...
- redis基本数据类型【2】-Hash类型
一.概述 1.散列是一种典型的字典结构,filed和value的映射,但value只能存储字符串,不支持其他类型 2.一个散列类型最多包含 2^32 -1个字段 3.散列适合存储对象:使用对象和ID构 ...
- 九度OJ 1084 整数拆分
题目地址:http://ac.jobdu.com/problem.php?pid=1084 题目描述: 一个整数总可以拆分为2的幂的和,例如: 7=1+2+4 7=1+2+2+2 7=1+1+1+4 ...
- ubuntu 下安装软件,卸载,查看已经安装的软件
参考网址:http://wiki.ubuntu.org.cn/UbuntuSkills 一般的安装程序用三种: .deb 和.rpm 这两种安装文件 .bundle 这是二进制的安装文件 而 tar. ...