java jdk webservice_JDK实现WebService(Web)

使用JDK实现WebService(Web)

一、环境介绍

IDE:IntelliJ IDEA 14.1

JDK:1.7

Tomcat:8.0.35

(-)使用@WebService注解方式

(-)使用Endpoint方式发布

(-)使用wsimport生成客户端

二、服务端

1.准备一个接口

package com.tgb.ws;

import javax.jws.WebService;

@WebService

public interface HelloWorld {

String sayHi(String name);

}

2.完成接口的实现

package com.tgb.ws.impl;

import java.util.Date;

import javax.jws.WebService;

import com.tgb.ws.HelloWorld;

@WebService(endpointInterface="com.tgb.ws.HelloWorld",

serviceName="HelloWorldWs")

public class HelloWorldWs implements HelloWorld {

@Override

public String sayHi(String name) {

return name + ". Welcome!" + " Now time is "

+ new Date();

}

}

3.开始发布

package servlet;

import com.tgb.ws.HelloWorld;

import com.tgb.ws.impl.HelloWorldWs;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.xml.ws.Endpoint;

@WebServlet(value="",loadOnStartup=0)

public class ServerMainServlet extends HttpServlet {

public void init() throws ServletException {

HelloWorld hw = new HelloWorldWs();

// EndPoint publish WebService

Endpoint.publish("http://localhost:8989/crazyit", hw);

System.out.println("WebService is published!");

}

}

4.验证是否发布成功

配置tomcat,启动项目

打开浏览器,输入地址:http://localhost:8989/crazyit

三、客户端

1.使用wsimport命令生成

- 创建一个Java工程

- 在src目录下进入cmd窗口(在地址栏打入cmd回车即可)

- 输入命令:wsimport -keep http://localhost:8989/crazyit?wsdl

2.开始调用服务

package client;

import com.tgb.ws.impl.HelloWorld;

import com.tgb.ws.impl.HelloWorldWs;

public class ClientMain {

public static void main(String[] args) {

HelloWorldWs hw = new HelloWorldWs();

HelloWorld helloWorld = hw.getHelloWorldWsPort();

System.out.println(helloWorld.sayHi("HelloWorld"));

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值