简体   繁体   中英

java web service SOAP

I have that kind of problem: Create your own SOAP web service which will hold information about people (eg in a map). Each person has the following attributes: First name, Surname, Birth date. Your web service will respond to a query which will filter the stored entries based on: Surname, Birth date. Based on the provided samples create a JUnit test for verifying whether your SOAP web service works as expected.

And I created Dynamic Web Project in Eclipse and wrote classes like this:

public class Person {
    String firstName, surname, birthDate;
    public Person(String firstName, String surname, String birthDate) {
        this.firstName = firstName;
        this.surname = surname;
        this.birthDate = birthDate;
    }
}

and Search:

public class Search {
    ArrayList<Person> people = new ArrayList<Person>();
    public Search() {
        Person jim = new Person("Jim", "Abacki","01/01/1990");
        Person scott = new Person("Scott","Babacki", "01/01/1990");
        Person anna = new Person("Anna","Cabacki", "01/01/1991");
        Person dan = new Person("Dan","Dabacki", "01/01/1992");
        Person ola = new Person("Ola","Fabacki", "01/01/1993");
        Person eva = new Person("Eva","Fabacki", "01/01/1991");

        people.add(jim);
        people.add(scott);
        people.add(anna);
        people.add(dan);
        people.add(ola);
        people.add(eva);
    }

    public String[] searchBySurname(String surname){

        int i =0;
        for(Person x : people){
            if(x.surname==surname){
                i++;
            }
        }
        String[] result = new String[i];
        int a=0;
        for(Person x : people){

            if(x.surname==surname){
                result[a]=x.firstName+ " "+ x.surname + " "+ x.birthDate;
                a++;
            }
        }
        return result;
    }


    public String[] searchByBirthDate(String birthDate){
        int i =0;
        for(Person x : people){
            if(x.surname==birthDate){
                i++;
            }
        }
        String[] result = new String[i];
        int a=0;
        for(Person x : people){

            if(x.surname==birthDate){
                result[a]=x.firstName + " "+ x.surname + " "+ x.birthDate;
                a++;
            }
        }
        return result;
    }

}

Then I added new Web Service by clicking on class Search -> Web Services -> Create new Web Service ( In properties I chose Axis2 and Tomcat Server). Then I done similar thing with class Person.

I thought that everything is ok, so I added web service client with Search, and then I tried to add Person Web Service but that error occured: Exception occurred during code generation for WSDL : org.apache.axis2.AxisFault: No operation found in the portType element.

And also when I tried to call operation like:

SearchStub a = new SearchStub();
a.searchBySurname(...);

In place of dots Eclipse gives me advice like this:

a.searchBySurname(SearchBySurname searchBySurname2);

which is also strange for me, because this function as argument should accept String.

And here is my questions: What am I doing wrong? Could you help me to fix this problem? Maybe someone have good tutorial because what I found in Internet does not help.

Without first generating a valid WSDL, you cannot invoke your service. Based on your error, Eclipse thinks you're missing an Operation (method). This happened because your Person class does not have any methods. Hence, no Operation.

Luckily for the way you have it set up, you do not need to go through and generate a WSDL based on Person. However, your Search class uses Person and the WSDL should generate without problem. This is the only WSDL you will need.


Now for your searchBySurname method, that does not seem normal based on what you have. My thoughts (I debug from here): (a) Your Search source looks good so the WSDL should be good. (b) Client generation??? (c) The method expects a SearchBySurname object when your WSDL specified String so the Client generation must be bad. So it tells you to take SearchBySurname object instead of String.

(*) Check out this tutorial: http://www.java2blog.com/2013/03/web-service-tutorial.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM