Web Service入门学习2--1个示例

本文档介绍了如何使用Java创建和调用Web Services的步骤。首先,通过Eclipse创建一个Java项目并编写服务端代码,展示了服务启动后的控制台输出和WSDL地址。接着,说明了如何在另一个Java项目中作为客户端调用该Web Service,并提供了详细的配置和测试用例代码。最后,展示了执行测试用例的结果。
摘要由CSDN通过智能技术生成

本节创建一个Web Services示例:


本文JDK为1.8

(1)服务段代码

在Eclipse中新建一个Java项目,项目名称为webService,最终结构如下图:



创建一个Function类,其代码如下

package com.ws.services;

import javax.jws.WebService;
import javax.xml.ws.Endpoint;

@WebService
public class Function {
	
	// defined the method 'transWords'
	public String transWords(String words) {
		String res = "";
		for (char ch : words.toCharArray()) {
			res += ch + "/";
		}
		return res;
	}

	public static void main(String[] args) {
		// Publish webservice
		Endpoint.publish("http://localhost:9990/ws/function", new Function());
		System.out.println("Start Publishing... ");
	}
}

按照JAVA程序启动之后,控制台打印输出结果:

Start Publishing... 

此时,在浏览器上输入:http://localhost:9990/ws/function?wsdl

就可以看到返回内容:

<!--
 Published by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. 
-->
<!--
 Generated by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. 
-->
<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://services.ws.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://services.ws.com/" name="FunctionService">
<types>
<xsd:schema>
<xsd:import namespace="http://services.ws.com/" schemaLocation="http://localhost:9990/ws/function?xsd=1"/>
</xsd:schema>
</types>
<message name="transWords">
<part name="parameters" element="tns:transWords"/>
</message>
<message name="transWordsResponse">
<part name="parameters" element="tns:transWordsResponse"/>
</message>
<portType name="Function">
<operation name="transWords">
<input wsam:Action="http://services.ws.com/Function/transWordsRequest" message="tns:transWords"/>
<output wsam:Action="http://services.ws.com/Function/transWordsResponse" message="tns:transWordsResponse"/>
</operation>
</portType>
<binding name="FunctionPortBinding" type="tns:Function">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="transWords">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="FunctionService">
<port name="FunctionPort" binding="tns:FunctionPortBinding">
<soap:address location="http://localhost:9990/ws/function"/>
</port>
</service>
</definitions>

(2) 客户段代码

在Eclipse中新建一个Java项目,项目名为wsClient。其他操作如下,



选择Other,找到Web Service Client,下一步


填入(1)服务器中发步的地址:http://localhost:9990/ws/function?wsdl,Finish



最终的结构如下:



(3)测试用例:

在wsClient下写测试用例,TestFunction类,结构如下



TestFunction类的代码如下:

package com.ws.services;

import java.rmi.RemoteException;

import javax.xml.rpc.ServiceException;

public class TestFunction {
	public static void main(String[] args) {
		Function fu;
		try{
			fu=new FunctionServiceLocator().getFunctionPort();
			String str=fu.transWords("This is test ws Function.");
			System.out.println(str);
		}catch (ServiceException e) {
			// TODO: handle exception
			e.printStackTrace();
		}catch (RemoteException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
}

执行TestFunction,结果显示如下(图or文字):

执行结果:

六月 27, 2018 10:00:21 下午 org.apache.axis.utils.JavaUtils isAttachmentSupported
警告: Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled.
T/h/i/s/ /i/s/ /t/e/s/t/ /w/s/ /F/u/n/c/t/i/o/n/./




感谢参考:https://www.cnblogs.com/AlanLee/archive/2017/06/02/6933073.html


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值