如何针对wsdl文件创建SOAP Web Client

时间:2013-11-12 01:55:53

标签: java eclipse web-services soap distributed-computing

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

Ctrl-n - >网络服务 - >服务器实施 - >浏览源代码 - >光洁度。

我创建了一个tomcat服务器及其运行的所有。

即使面临死亡威胁,我也无法创建客户。

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

Ctrl-n - >网络服务 - >传递给wsdl文件。

Eclipse实际上试图覆盖PoliceDepartment.java类。

在任何情况下,它都不会创建Web客户端。

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

我想必须有一些工具可以为PoliceDepartment服务器创建一个代理,我可以从我的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();
        }

    }


}

1 个答案:

答案 0 :(得分:1)