java - 如何根据 wsdl 文件创建 SOAP Web 客户端

标签 java eclipse web-services soap distributed-computing

我已经从名为 PoliceDepartment.java 的 java 类创建了一个 Web 服务。 我这样做是这样做的:

Ctrl-n -> Web 服务 -> 服务器实现 -> 浏览源代码 -> 完成。

我已经创建了一个 tomcat 服务器及其运行状态等。

即使在死亡威胁下我也无法创建客户端。

我已准备好客户端代码(见下文)。

Ctrl-n -> Web 服务 -> 向其传递 wsdl 文档。

Eclipse 实际上尝试重写 PoliceDepartment.java 类。

无论如何,它都不会创建 Web 客户端。

我需要的是简单地发送带有参数的信号:createClient("blabla")。

我想一定有一些工具可以创建警察部门服务器的代理,我可以简单地从我的 java 客户端调用它。

如果您知道如何做,请帮助我实现这一目标。

感谢您提前提供的帮助。

这是来自命令行的“客户端”解析参数(它需要代理将解析后的参数发送到 Web 服务):

package client;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;
import java.util.regex.Pattern;

import interfaces.Compute;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class Console {

    public void loop(){

        Scanner sc = new Scanner(System.in);
        while(true){
            System.err.println("Enter you badge id in digits");
            String id = sc.nextLine();
            System.err.println("Choose police station: SPVM, Brossard or Longueil");
            String station = sc.nextLine();
            if(station.equals("SPVM")||station.equals("Brossard")||station.equals("Longueil")){
                PoliceOfficer popo = new PoliceOfficer(station, id);
                Compute stub = (Compute) this.connect(popo.station);

                while (true){
                    System.err.println("Choose which record you want: Criminal, Missing, Edit, Get Count; type Show to see database, Transfer to transfer a record or Exit to exit:");
                    try{
                        String choice=sc.nextLine();
                        if(choice.equals("Criminal")){
                            System.out.println("Please enter: firstname, lastname, description, status");
                            String firstname=sc.nextLine();
                            String lastname=sc.nextLine();
                            String description=sc.nextLine();
                            String status=sc.nextLine();
                            this.write(stub.createCRecord(firstname, lastname, description, status, popo.badgeID), popo.badgeID);



                        }else if(choice.equals("Missing")){
                            System.err.println("Please enter: firstname, lastname,address,last location, last date seen, status");
                            String firstname=sc.nextLine();
                            String lastname=sc.nextLine();
                            String address=sc.nextLine();
                            String lastlocation=sc.nextLine();
                            String lastdate = sc.nextLine();
                            String date[] = lastdate.split("-");
                            Date cal = new Date(Integer.parseInt(date[0])-1901, 
                                    Integer.parseInt(date[1]), Integer.parseInt(date[2]));

                            String status=sc.nextLine();
                            this.write(stub.createMRecord(firstname, lastname, address, cal, lastlocation, status, popo.badgeID), popo.badgeID);

                        } else if(choice.equals("Edit")){
                            System.err.println("Please enter: lastname, recordid number (only the digits), status");
                            String lastname=sc.nextLine();
                            int recordid=0;
                            try{
                                recordid=Integer.parseInt(sc.nextLine());
                                String newstatus=sc.nextLine();
                                this.write(stub.editCRecord(lastname, recordid, newstatus, popo.badgeID), popo.badgeID);
                            }catch (NumberFormatException e){
                                System.err.println("NOT INTEGER. ABORTING. RECORDID MUST BE INTEGER e.g CR0 you must enter 0");
                            }


                        } else if(choice.equals("Get Count")){

                                  DatagramSocket clientSocket = new DatagramSocket();
                                  InetAddress IPAddress = InetAddress.getByName("localhost");
                                  byte[] sendData = new byte[1024];
                                  byte[] receiveData = new byte[1024];
                                  sendData="COUNT".getBytes();

                                  DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
                                  clientSocket.send(sendPacket);
                                  DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
                                  clientSocket.receive(receivePacket);
                                  String modifiedSentence = new String(receivePacket.getData());
                                  this.write("Get Count:" + modifiedSentence + "\n", popo.badgeID);

                        } else if(choice.equals("Show")){

                            this.write(stub.show(), popo.badgeID);


                        }else if(choice.equals("Transfer")){

                            System.err.println("Please enter the department and the id of the record");
                            String department=sc.nextLine();
                            String record_id=sc.nextLine();
                            System.err.println("Following record transfered to the "+department+" department:");

                            this.write(stub.transferRecord(department, record_id, popo.badgeID), popo.badgeID);

                        }
                        else if(choice.equals("Exit")){
                            break;
                        }
                        System.err.println("\n\n\n");

                    }catch(Exception e){
                        e.printStackTrace();
                    }
                }//end inner while
            }// end if the choice is SPVM, Longueil or Brossard
        }//end outer while (choose police station)
    }

    public Compute connect(String station){
        Compute stub=null;
        try{

            Registry registry = LocateRegistry.getRegistry();



                stub = (Compute) registry.lookup(station);





        }catch(Exception e){
            System.err.println("an error in the client has occured");
            e.printStackTrace();
        }
        return stub;

    }

    private void write(String s, String filename){
        PrintWriter file=null;
        try{
            file = new PrintWriter(new FileOutputStream(
                    new File(filename), 
                    true /* append = true */)); 
            file.println(s);
            System.err.println(s);
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            file.close();
        }

    }


}

最佳答案

关于java - 如何根据 wsdl 文件创建 SOAP Web 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19919511/

相关文章:

java - 如何确定原始变量的原始类型?

java - 有没有办法在 Windows 上使用 Java 中的 DatagramSocket 忽略广播的 UDP 消息?

java - 从另一个类访问私有(private)最终静态 double

c# - 从网络服务向服务器发送 POST

web-services - 使用 SoapUI 时出现问题 : "A required header representing a Message Addressing Property is not present

java - 什么是NullPointerException,我该如何解决?

java - Spring 和 Hibernate 依赖关系尚未解决

java - 在maven jetty插件中使用@WebServlet注释

java - 打开 zip 文件时出错或缺少 : C:\Program 的 JAR list

java - 在 Java Web 服务方法中添加项目后刷新应用程序