一、调用jdk自带的Endpoint类,通过端点发布
1、新建项目
- a.File -> New Project…菜单打开新建项目窗口,依次选择Java,WebServices,Version项选择Apache Axis,Libraries项选择 Download。然后点击Next按钮进入下一页
- 输入项目名称,然后点击Finish按钮开始下载依赖包。(下载依赖包可能会失败,建议多次尝试)依赖包下载完成后进入新建的项目
2.开发服务器端
- 在项目下新建一个包webservice_student_server
- 在包中新建一个接口StudentInterface,在接口中声明一个查询的方法studentQuery(int number)
package com.webservice.server;
public interface StudentInterface {
public abstract String studentQuery(int number);
}
- 新建StudentInterface接口的实现类StudentInterfaceImp,在类中实现StudentInterface接口中的查询方法studentQuery(int number),调用jdk自带的Endpoint类,通过端点发布
package com.webservice.server;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
@WebService
public class StudentInterfaceImp implements StudentInterface {
String name[]={
" 姜倩云 "," 张丽梅 "," 吴美庆 "," 张忠敏 "," 王雅蓉 "," 葛红敏 "," 赵斌灿 "," 王严鑫 ",
" 莫可宏 "," 王猛 ", " 侯勇军 "," 马铸辉 "," 易鹏 "," 罗路辉 "," 闫治鹏 "," 孙磊 "," 李佳玲 ",
" 王勃博 "," 康尧 "," 张波 ", " 邓雷 "," 陈佳鑫 "," 唐炜宁 "," 葛龙 "," 倪赛杰 "," 陈成 ",
" 刘浩浩 "," 周子轩 "," 宫蕊 "," 文云丽 "," 沈雪 "," 刘冬媛 "," 褚蓉 "," 钱苑 "," 董樟裕 ",
" 马凌杰 "," 王京源 "," 陈辉云 "," 罗建兴 "," 陈晨 "," 赵文宇 "," 宗玮雯 "," 赵贝贝 ",
" 黄豪琛 "," 娄宽 "," 任上海 "," 王康 "," 沈楠 "," 蒋宇 "," 吕阳 "," 王刚 "," 王扬 "," 吕中剑 ",
" 张军 "," 卢惺鑫 "," 姚礼宗 "
};
@Override
public String studentQuery(int number) {
System.out.println("from client..." +" 学号 :" +number);
String result = name[number];
System.out.println("to client..." + " 姓名 :"+result);
return result;
}
public static void main(String[] args) {
//java jdk提供一个自带的类可以将java应用程序发布成webservice
/**
* Endpoint.publish(String address, Object implementor):
* 参数1:提供服务对外的访问地址
* 参数2:提供服务的类的对象
*
* */
Endpoint.publish("http://localhost:8080/StudentInterfaceImp", new StudentInterfaceImp());
System.out.println(" 发布成功 ...");
}
}
- 运行该程序,看到以下提示信息,说明该服务已经启动
- 在浏览器地址栏中输入http://localhost:8080/StudentInterfaceImp?wsdl 回车,看到以下界面,说明服务发布成功
3、开发客户端
- 新建Java Project,项目名称为:webserviceDemo_client,在该项下新建包com.webservice.client
- 找到该项目的src的绝对目录
- 打开CMD窗口,执行生成命令 wsimport -s “src目录” -p “生成类所在包名” -keep wsdl发布地址,如下图
- 新建Client窗体类,调用服务
package com.webservice.client;
import com.webservice.service.StudentInterfaceImp;
import com.webservice.service.StudentInterfaceImpService;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Client extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JButton search;
private JButton cancel;
private JTextArea textArea;
private StudentInterfaceImpService impService;
private StudentInterfaceImp imp;
String result;
private static int number=0;
public Client(String string) {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setForeground(Color.WHITE);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
// System.out.println("请输入学号进行查询");
JLabel label = new JLabel(" 请输入学号进行查询 ");
label.setForeground(Color.BLUE);
label.setBounds(155, 10, 129, 50);
contentPane.add(label);
textField = new JTextField("20");
textField.setBounds(155, 58, 110, 22);
contentPane.add(textField);
textField.setColumns(10);
// 通过 webservice 的服务视图
impService = new StudentInterfaceImpService();
// 获得服务端点
imp = impService.getStudentInterfaceImpPort();
search = new JButton(" 查询 ");
search.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == search) {
number = Integer.parseInt(textField.getText().toString());
if (number > 56 || number < 1) {
result = " 非法输入! ";
textArea.setText(result);
} else {
textArea.setText(" 你输入的学号是: " + number + "\n" + " 查询结果为: " + imp.studentQuery(number));
System.out.println( imp.studentQuery(number));
}
}
}
});
search.setBounds(139, 104, 73, 23);
contentPane.add(search);
cancel = new JButton(" 取消 ");
cancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.setText("");
textArea.setText("");
}
});
cancel.setBounds(222, 104, 73, 23);
contentPane.add(cancel);
textArea = new JTextArea();
textArea.setBounds(105, 153, 223, 70);
contentPane.add(textArea);
Component glue = Box.createGlue();
glue.setBounds(61, 196, 1, 1);
contentPane.add(glue);
}
public static void main(String[] args) {
Client frame = new Client(" 自定义 web service 调用 ");
frame.setVisible(true);
}
}
- 运行结果
二、IDEA直接生成wsdl文件,并通过服务器发布
1、在服务端新建包calculate_server,并新建类Circle
package calculate_server;
import javax.jws.WebService;
@WebService
public class Circle {
public double area(double radius){
return Math.PI * radius * radius;
}
}
2、服务器配置
- 打开Edit Configurations…菜单
- 在Run/Debug Configurations窗口里点击+按钮,在出现的菜单里选择Tomcat Server --> Local
- 然后在添加的Tomcat Server配置页面中配置服务器的Name、Application server和HTTP Port,配置页面底部可能会显示有Warning,点击警告信息右侧的Fix…按钮切换到Deployment标签页。
- 添加 Artifact 后的界面如下,已经没有警告了,点击OK
- 选择File–> Project Structure…菜单
- 在出现的窗口中选择Project Settings --> Artifacts,会到在新的警告信息。点击警告信息右侧的Fix…按钮,选择Add ‘JAX-WS-Apache Axis’ to the artifact,然后点击OK
3、通过IDEA直接生成WSDL文件
- 选中Circle.java文件,然后点击Tools–>WebServices --> Generate Wsdl From Java Code
- 确认要添加的函数后点击OK
- 可以看到在项目结构目录里多了一个Circle.wsdl文件
4、启动Tomcat发布服务
- Tomcat启动成功后,在浏览器中输入http://localhost:8080/services,页面如下,没有看到发布的Circle.wsdl
- 但是输入http://localhost:8080/services/Circle?wsdl 后显示服务已经发布成功
要在浏览器中输入http://localhost:8080/services,看到发布的Circle.wsdl,方法如下:
- 打开项目目录结构下的web–>WEB-INF–>server-config.wsdd,复制一份圈起来的部分
- 对类名,包名,命名空间等进行修改,这里添加了,Circlel类和StudentInterfaceImp类
- 重新启动Tomcat,在浏览器输入http://localhost:8080/services后,结果如下: