开发一个简单的Java webService以及相关知识

新建一个web或java工程(maven工程也可以)

在这里插入图片描述

创建一个类,使用Endpoint方式发布

package com.jsp.web;

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

@WebService
public class HelloWorld {
	@WebMethod
	public String sayHello(String greet) {
		System.out.println("get Message...");
		String result = "Hello World, " + greet;
		return result;
	}

	public static void main(String[] args) {
		System.out.println("server is running");
		String address = "http://localhost:8089/service/HelloWorld";
		HelloWorld implementor = new HelloWorld();
		Endpoint.publish(address, implementor);
		System.out.println("published !");
	}
}

启动main方法,完成发布

在这里插入图片描述

浏览器访问http://localhost:8089/service/HelloWorld

在这里插入图片描述

坑!

使用jdk1.8不能访问,百度好久才知道是jdk问题,使用jdk1.7就可以了,具体原因还不知道,暂且几下

webService客户端开发

新建
在这里插入图片描述
在这里插入图片描述
输入WSDL服务描述文件的访问地址
http://localhost:8089/service/HelloWorld?wsdl
在这里插入图片描述
生成代码
在这里插入图片描述

编写测试类

**package com.jsp;
import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;
import com.jsp.web.HelloWorld;
import com.jsp.web.HelloWorldServiceLocator;
public class TestHelloWorld {
	public static void main(String[] args) throws ServiceException, RemoteException {
		// 1.创建Locator对象,相当于传统服务的类或者是库
		HelloWorldServiceLocator hsLocator = new HelloWorldServiceLocator();
		// 2.获取port,相当于传统类或库的方法接口
		HelloWorld hPort = hsLocator.getHelloWorldPort(); // ServiceException
		// 3.通过port调用服务
		String sayHello = hPort.sayHello("I love the whole world!"); // RemoteException
		System.out.println(sayHello);
	}
}**

运行

在这里插入图片描述

学习知识总结

基本的WebService平台是Http+Xml
WebService三大基本元素是WSDL,SOAP,UDDI
WSDL核心是XML Schema(与DTD相类似)
SOAP = HTTP + XML
UDDI是构建唯一web服务的框架

WSDL文档结构

元素定义
<portType>web service 执行的操作
<message>web service 使用的消息
<types>web service 使用的数据类型
<binding>web service 使用的通信协议

一个 WSDL 文档的主要结构是类似这样的

<definitions>

<types>
   definition of types........
</types>

<message>
   definition of a message....
</message>

<portType>
   definition of a port.......
</portType>

<binding>
   definition of a binding....
</binding>

</definitions>

SOAP 构建模块

在这里插入图片描述
基本结构

<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">

<soap:Header>
  ...
  ...
</soap:Header>

<soap:Body>
  ...
  ...
  <soap:Fault>
    ...
    ...
  </soap:Fault>
</soap:Body>

</soap:Envelope>

关系wsdl,soap,uddi相关介绍可以参考
http://www.w3school.com.cn/ws.asp

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

岁月玲珑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值