中间件之实习四 Web Services 开发

时间: 2023-07-11 admin 互联网

中间件之实习四 Web Services 开发

中间件之实习四 Web Services 开发

一 实习目的

利用Axis 2 进行Web service开发,掌握Web service, WSDL, SOAP等基本概念以及Web service 开发的主要模块。

二 实习要求

  1. 利用Java Swing 或SWT 开发一桌面应用程序
  2. 应用程序中集成以下网站提供的3种或3种以上的Web服务

.aspx

.aspx 

可集成的Web服务有:

  1. 中英文双向翻译
  2. 手机号归属地查询
  3. IP地址归属地
  4. 城市天气预报
  5. 股票行情
  6. QQ号在线状态查询
  7. 国内飞机航班时刻表查询
  8. 火车时刻表
  9. 外汇-人民币兑换报价
  10. 中国电视节目预告
  11. 中文简体字--繁体字转换
  12. 中国邮政编码--地址信息双向查询
  13. 验证码--验证码图片
  14. 快递查询
  15. 身份证查询
  16. 公交线路查询
  17. ISBN查询
  18. ICP备案查询
  1. 界面控件布置合理、美观;将所集成的Web服务作为应用程序的主要业务功能,供用户使用。
  2. 将应用程序以及所需要用到的库文件打包生成一个可执行jar文件,双击所生成jar文件可以打开应用程序界面;程序提供正确的业务功能。

三 实习过程

  1. 环境配置

安装Web Service插件

 安装Eclipse-jee;下载Axis2的最新版本Axis2: 

Apache Downloads ,选择Standard Binary Distribution的.zip包即"axis2-1.4.1-bin.zip"这个文件,解压缩得到的目录名axis2-1.4.1,目录内的文件结构如下

在Eclipse的菜单栏中,Window --> Preferences --> Web Service --> Axis2-->Perferences,在Axis2 runtime location中选择Axis2解压缩包的位置,设置好后,点"OK"即行,如下图。

2)集成Web服务,开发客户端(以集成邮政编码--地址信息为例)

(1)下载WSDL,将所需要集成的Web服务的WSDL文件下载到本机

(2)创建一个空的Java 工程,如MyApplication。

(3)右键点击“MyApplication”, 选择New--Other, 进入以下向导界面

选择Web Services client,点击next进入以下向导界面

在向导界面中,Service definition栏输入WSDL所在位置的完整的网络路径。Client type下的标尺拖动到Develop client。点击Finish, 向导就可以产生客户端代理代码。如下图:

(4)反复执行(1)和(3)步骤,可以将多个Web服务的客户端代理代码生成到你的应用程序中。

(5)使用类名中带着Proxy的类,如使用上图中类ChinaZipSearchWebServiceSoapProxy,生成对象,通过对象就可调用对应Web服务所提供的业务功能。

(6)使用Swing或SWT设计界面,界面中有文本输入框,查询按钮等,供用户输入和查询。查询过程中,调用Web服务的客户端代理类生成对象,访问Web Service。将结果以合理方式返回给用户。

(7)复杂(对象,自定义类型,复杂类型)返回结果处理,本次实习提供的WebService,服务端定义WSDL和部署信息时,并没有对返回的复杂类型数据给出足够的描述信息,因此客户端生成代理代码时,未能生成合理的详细的类对应返回复杂类型数据。服务端返回复杂类型数据时,只是用xml文件存放这些数据,并将xml文件返回给用户。我们需要解析xml文档才能获得详细信息。解析xml,大家可以参考Javascript中DOM树解析。XML和HTML一样,可以构造一棵DOM树进行解析。

邮政编码--地址信息为例,基本过程如下:

  1. 调用ChinaZipSearchWebServiceSoapProxy类的getAddressByZipCode()方法,根据邮编获取地址信息。本例中获取邮编“712100”对应的地址信息。
  2. 返回结果类型为GetAddressByZipCodeResponseGetAddressByZipCodeResult,调用该类的get_any() 方法,可得到一个数组,存放着服务端返回的XML文件内容。其中详细信息见附件result.xml。
  3. 从数组中得到xml文件内容,使用DOM  API进行解析。详细过程见附件Test.java.

四 实验代码及结果展示

1.中英文双向翻译服务

public class Test{private JFrame frame;private JTextField textField;public static void main(String[] args) {EventQueue.invokeLater(new Runnable() {public void run() {try {Test window = new Test();window.frame.setVisible(true);} catch (Exception e) {e.printStackTrace();}}});}public Test() {initialize();}public void initialize() {frame = new JFrame("中英双向翻译");frame.setBounds(100, 100, 632, 320);frame.setResizable(false);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);JPanel panel = new JPanel();frame.getContentPane().add(panel, BorderLayout.CENTER);panel.setLayout(null);JLabel lblNewLabel = new JLabel("请输入单词");lblNewLabel.setFont(new Font("幼圆", Font.PLAIN, 17));lblNewLabel.setBounds(41, 11, 198, 31);lblNewLabel.setHorizontalAlignment(SwingConstants.LEFT);panel.add(lblNewLabel);textField = new JTextField();textField.setFont(new Font("宋体", Font.PLAIN, 17));textField.setBounds(268, 14, 146, 24);panel.add(textField);textField.setColumns(10);final JTextArea textArea = new JTextArea();textArea.setFont(new Font("Monospaced", Font.BOLD, 18));textArea.setBounds(0, 42, 626, 245);textArea.setEditable(false);panel.add(textArea);JButton btnNewButton = new JButton("翻译");btnNewButton.setFont(new Font("幼圆", Font.PLAIN, 17));btnNewButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {}});btnNewButton.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {EnglishChineseSoapProxy proxy=new EnglishChineseSoapProxy();String[] results;StringBuffer sb = new StringBuffer();String str=null;try {results =proxy.translatorString(textField.getText());for(int i = 0; i < 2*results.length/5; i++){sb. append(results[i]+";");}for(int i = 2*results.length/5; i < results.length; i++){sb. append(results[i]+"\n");}sb.deleteCharAt(sb.length() - 1); str =sb.toString();} catch (RemoteException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}textArea.setText(str);}});btnNewButton.setBounds(453, 13, 122, 27);panel.add(btnNewButton);}
}

运行结果如图所示:

2.中国邮政编码--地址信息双向查询

public class Test {private JFrame frame;private JTextField textField;public static void main(String[] args) {EventQueue.invokeLater(new Runnable() {public void run() {try {Test window = new Test();window.frame.setVisible(true);} catch (Exception e) {e.printStackTrace();}}});}public Test() {initialize();}public void initialize() {frame = new JFrame("中国邮政编码地址信息双向查询");frame.setBounds(100, 100, 1098, 594);frame.setResizable(false);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);JPanel panel = new JPanel();frame.getContentPane().add(panel, BorderLayout.CENTER);panel.setLayout(null);JLabel lblNewLabel = new JLabel("请输入邮政编码");lblNewLabel.setFont(new Font("幼圆", Font.PLAIN, 17));lblNewLabel.setBounds(260, 11, 198, 31);lblNewLabel.setHorizontalAlignment(SwingConstants.LEFT);panel.add(lblNewLabel);textField = new JTextField();textField.setFont(new Font("宋体", Font.PLAIN, 17));textField.setBounds(472, 14, 121, 24);panel.add(textField);textField.setColumns(10);final JTextArea textArea = new JTextArea();textArea.setFont(new Font("新宋体", Font.PLAIN, 16));textArea.setBounds(0, 42, 1092, 517);textArea.setEditable(false);panel.add(textArea);JButton btnNewButton = new JButton("地址信息");btnNewButton.setFont(new Font("幼圆", Font.PLAIN, 17));btnNewButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {}});btnNewButton.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {
ChinaZipSearchWebServiceSoapProxy locator=new ChinaZipSearchWebServiceSoapProxy();String str = null;StringBuffer sb = new StringBuffer();try {GetAddressByZipCodeResponseGetAddressByZipCodeResult result = locator.getAddressByZipCode(textField.getText(),"");MessageElement elements[]=result.get_any();NodeList list=elements[1].getElementsByTagName("ZipInfo");for(int i=0;i<list.getLength();i++){Node node =(Node) list.item(i);NodeList children =node.getChildNodes();for(int j=0;j<children.getLength();j++){sb. append(children.item(j).getFirstChild()+"\n"); str =sb.toString()+"\n";}}} catch (RemoteException e1) {e1.printStackTrace();}textArea.setText(str);}});btnNewButton.setBounds(630, 12, 113, 27);panel.add(btnNewButton);}
}

运行结果如图所示:

3.城市天气预报服务

public class Test1{private JFrame frame;private JTextField textField;public static void main(String[] args) {EventQueue.invokeLater(new Runnable() {public void run() {try {Test1 window = new Test1();window.frame.setVisible(true);} catch (Exception e) {e.printStackTrace();}}});}public Test1() {initialize();}public void initialize() {frame = new JFrame("城市天气预报");frame.setBounds(100, 100, 632, 320);frame.setResizable(false);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);JPanel panel = new JPanel();frame.getContentPane().add(panel, BorderLayout.CENTER);panel.setLayout(null);JLabel lblNewLabel = new JLabel("请输入所查城市");lblNewLabel.setFont(new Font("幼圆", Font.PLAIN, 17));lblNewLabel.setBounds(41, 11, 198, 31);lblNewLabel.setHorizontalAlignment(SwingConstants.LEFT);panel.add(lblNewLabel);textField = new JTextField();textField.setFont(new Font("宋体", Font.PLAIN, 17));textField.setBounds(268, 14, 146, 24);panel.add(textField);textField.setColumns(10);final JTextArea textArea = new JTextArea();textArea.setFont(new Font("Monospaced", Font.BOLD, 18));textArea.setBounds(0, 42, 626, 245);textArea.setEditable(false);panel.add(textArea);JButton btnNewButton = new JButton("查询");btnNewButton.setFont(new Font("幼圆", Font.PLAIN, 17));btnNewButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {}});btnNewButton.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {
//核心代码如下:
String[] result;StringBuffer sb = new StringBuffer();String str=null;WeatherWSLocator weather =new WeatherWSLocator();WeatherWSSoapStub stub;try {stub = (WeatherWSSoapStub) weather.getPort(WeatherWSSoapStub.class);result =stub.getWeather("西安",null);for(int i = 0; i < 2*result.length/5; i++){sb. append(result[i]+";");}for(int i = 2*result.length/5; i < result.length; i++){sb. append(result[i]+"\n");}sb.deleteCharAt(sb.length() - 1); str =sb.toString();textArea.setText(str);} catch (ServiceException e1) {e1.printStackTrace();} catch (RemoteException e1) {e1.printStackTrace();}     }});btnNewButton.setBounds(453, 13, 122, 27);panel.add(btnNewButton);}
}

运行结果如图所示:

 

4.QQ号在线状态查询

public class CheckQQOnlineClient{private JFrame frame;private JTextField textField;public static void main(String[] args) {EventQueue.invokeLater(new Runnable() {public void run() {try {CheckQQOnlineClient window = new CheckQQOnlineClient();window.frame.setVisible(true);} catch (Exception e) {e.printStackTrace();}}});}public CheckQQOnlineClient() {initialize();}public void initialize() {frame = new JFrame("QQ在线状态查询");frame.setBounds(100, 100, 632, 320);frame.setResizable(false);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);JPanel panel = new JPanel();frame.getContentPane().add(panel, BorderLayout.CENTER);panel.setLayout(null);JLabel lblNewLabel = new JLabel("请输入QQ号");lblNewLabel.setFont(new Font("幼圆", Font.PLAIN, 17));lblNewLabel.setBounds(41, 11, 198, 31);lblNewLabel.setHorizontalAlignment(SwingConstants.LEFT);panel.add(lblNewLabel);textField = new JTextField();textField.setFont(new Font("宋体", Font.PLAIN, 17));textField.setBounds(268, 14, 146, 24);panel.add(textField);textField.setColumns(10);final JTextArea textArea = new JTextArea();textArea.setFont(new Font("Monospaced", Font.BOLD, 18));textArea.setBounds(0, 42, 626, 245);textArea.setEditable(false);panel.add(textArea);JButton btnNewButton = new JButton("查询");btnNewButton.setFont(new Font("幼圆", Font.PLAIN, 17));btnNewButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {}});btnNewButton.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {
//此处为核心代码:
QqOnlineWebServiceLocator qqOnlineWS=new QqOnlineWebServiceLocator();QqOnlineWebServiceSoap qqOnlineWSSoap;String results;try {qqOnlineWSSoap = qqOnlineWS.getqqOnlineWebServiceSoap();results =qqOnlineWSSoap.qqCheckOnline(textField.getText());// String,Y = 在线;N = 离线;E = QQ号码错误;A = 商业用户验证失败;V = 免费用户超过数量if ("Y".equalsIgnoreCase(results)) {results = "在线";} else if ("N".equalsIgnoreCase(results)) {results = "离线";} else if ("E".equalsIgnoreCase(results)) {results = "QQ号码错误";} else if ("A".equalsIgnoreCase(results)) {results = "商业用户验证失败";} else if ("V".equalsIgnoreCase(results)) {results = "免费用户超过数量";}textArea.setText(results);} catch (ServiceException e2) {// TODO Auto-generated catch blocke2.printStackTrace();} catch (RemoteException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}			}});btnNewButton.setBounds(453, 13, 122, 27);panel.add(btnNewButton);}
}

运行结果如图所示:

 5.手机号归属地查询

package com.devins.ws.phone;
import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;
import cn.com.WebXml.MobileCodeWSLocator;public class PhoneBelongQuery {public static void main(String[] args) throws ServiceException, RemoteException {MobileCodeWSLocator factory = new MobileCodeWSLocator();
cn.com.WebXml.MobileCodeWSSoap mobileCodeWSSoap = factory.getMobileCodeWSSoap();String result = mobileCodeWSSoap.getMobileCodeInfo("18202969482",null);System.out.println(result);}
}

运行结果如图所示: