Java Web Service SOAP with objects

Asked

Viewed 2,192 times

0

Hello, I am learning to use web-services and I stopped with a problem, maybe it is something silly, but I did not find solution, I am hosting my application in Heroku and I will leave here the relevant information, however first I must explain what I would like to do:

I did some simple tests of calculators and it worked, but when I tried to advance a little in the examples, something went very wrong, my intention is that I can work with objects, with this I can work with the database together, but I’m not getting, one last attempt was this tutorial where I basically made a copy of it with no changes, because if the example works then I could see where my error was.

I now leave my code and the error that follows using this same example:

On the server:

Person.java

package ws.basico.ws_basico.model;

import java.io.Serializable;

public class Person implements Serializable{

    private static final long serialVersionUID = -5577579081118070434L;

    private String name;
    private int age;
    private int id;

    public String getName() { return name; }
    public void setName(String name) { this.name = name; }

    public int getAge() { return age; }
    public void setAge(int age) { this.age = age; }

    public int getId() { return id; }
    public void setId(int id) { this.id = id; }

    @Override
    public String toString(){ return "id: "+id+", nome: "+name+", idade: "+age; }
}

Personservice.java

package ws.basico.ws_basico.interfaces;

import javax.jws.WebMethod;

import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import ws.basico.ws_basico.model.Person;

@WebService
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface PersonService {

    @WebMethod public boolean  addPerson(Person p);
    @WebMethod public boolean  deletePerson(int id);
    @WebMethod public Person   getPerson(int id);
    @WebMethod public Person[] getAllPersons();
}

Personserviceimpl.java

package ws.basico.ws_basico.controller.services;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.jws.WebService;
import ws.basico.ws_basico.interfaces.PersonService;
import ws.basico.ws_basico.model.Person;

@WebService(endpointInterface = "ws.basico.ws_basico.interfaces.PersonService", serviceName = "Person")  
public class PersonServiceImpl implements PersonService {

    private static Map<Integer,Person> persons = new HashMap<Integer,Person>();

    @Override
    public boolean addPerson(Person p) {
        if(persons.get(p.getId()) != null) { return false; }

        persons.put(p.getId(), p);
        return true;
    }

    @Override
    public boolean deletePerson(int id) {
        if(persons.get(id) == null)  { return false; }

        persons.remove(id);
        return true;
    }

    @Override
    public Person getPerson(int id) { return persons.get(id); }

    @Override
    public Person[] getAllPersons() {
        Set<Integer> ids = persons.keySet();
        Person[] p = new Person[ids.size()];
        int i=0;

        for(Integer id : ids){
            p[i] = persons.get(id);
            i++;
        }

        return p;
    }
}

Loader.java

package ws.basico.ws_basico;

import javax.xml.ws.Endpoint;
import ws.basico.ws_basico.controller.services.PersonServiceImpl;

public class Loader {

    public static void main(String[] args) {
        String port    = System.getenv("PORT");
        String host    = "http://0.0.0.0:";
        String service = "/person";

        String url = host + port + service;

        SorteiaMensagem sm    = new SorteiaMensagem();

        Endpoint.publish(url, new PersonServiceImpl());

    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>ws.basico</groupId>
    <artifactId>ws_basico</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>


 <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <version>3.1.0</version>

                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>ws.basico.ws_basico.Loader</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
</build> 
</project>

Here’s the WSDL generated by this code. I am generating and passing the application to Heroku using Maven with the following commands:

mvn package
git add.
git commit -m "texto aqui"
git push heroku master

I believe there is nothing different in the git commands and not in the compilation of Maven, because the system is running on the server, as can be seen in the link above.

However, when running a client the error is found.

Client code:

Personservice.java

package ws_basico_consumidor.interfaces;

import javax.jws.WebMethod;

import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import ws_basico_consumidor.model.Person;

@WebService(name = "Person", targetNamespace = "http://ws_basico.basico.ws/")
public interface PersonService {

    public boolean  addPerson(Person p);
    public boolean  deletePerson(int id);
    public Person   getPerson(int id);
    public Person[] getAllPersons();
}

Ws_basico_consumer.java

package ws_basico_consumidor;

import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import ws_basico_consumidor.model.Person;
import ws_basico_consumidor.interfaces.PersonService;
import java.util.Arrays;

public class Ws_basico_consumidor {

    public static void main(String[] args) throws MalformedURLException {
        PersonService ps;

        Person p1 = new Person();
        Person p2 = new Person();

        URL url = new URL("https://ws-basico.herokuapp.com/person?wsdl");
        QName qname = new QName("http://services.controller.ws_basico.basico.ws/","Person");
    Service ws = Service.create(url, qname);

        ps = ws.getPort(PersonService.class);

        p1.setId(0);
        p1.setName("bruno");
        p1.setAge(27);

        p2.setId(1);
        p2.setName("ana");
        p2.setAge(19);

        System.out.println("Adicionando " + p1.getName() + " status: " + ps.addPerson(p1));
        System.out.println("Adicionando " + p2.getName() + " status: " + ps.addPerson(p2));

        System.out.println(ps.getPerson(0)); // retorna o bruno.

        System.out.println(Arrays.asList(ps.getAllPersons()));

        System.out.println("Deletando " + p2.getName() + " status: " + ps.deletePerson(1));

        System.out.println(Arrays.asList(ps.getAllPersons()));

    }

}

Then I have the following error in client:

Exception in thread "main" javax.xml.ws.WebServiceException: Tipo de porta indefinido: {http://ws_basico.basico.ws/}Person
    at com.sun.xml.internal.ws.client.WSServiceDelegate.getPort(WSServiceDelegate.java:456)
    at com.sun.xml.internal.ws.client.WSServiceDelegate.getPort(WSServiceDelegate.java:463)
    at javax.xml.ws.Service.getPort(Service.java:188)
    at ws_basico_consumidor.Ws_basico_consumidor.main(Ws_basico_consumidor.java:25)
C:\Users\bruno\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
FALHA NA CONSTRUÇÃO (tempo total: 21 segundos)

Considerations:

  • First of all, I know that by default no underline is used in Java nomenclature, but at the time was more concerned with testing and differentiate from other projects, only this.
  • In the "Loader" class she is leaving it up to the system to define your location and door, if I’m not mistaken Heroku’s standard port is to 5000.
  • This is a test project and the client also having this error solved and learning the concepts, the goal is that in the application I can access with a C client#.

Thanks in advance for your attention.

1 answer

-1

I think the problem might be in the @Webservice annotation. You have defined that the Personservice interface and the class that implements Personserviceimpl have the same name webservice name name="Person". If I were you I’d remove that from both notes and try the following:

Qname qname = new Qname("http://services.controller.ws_basico.basico.ws/","Personserviceimpl");

  • I will do this test, but I basically did a Ctrl+c Ctrl+v of the site I mentioned, because I had done other tests and nothing worked, I can even generate the wsdl and leave the service running, but I can not consume.

  • Managed to solve the problem?

  • So far nothing, I do not know what I am doing wrong, because I have tried to copy and paste the code straight, I do not know if it is some error when generating the jar or what can be, I noticed that my wsdl is different than that of the tutorial, so I don’t know if this is some mistake or version issue only.

  • If the WSDL is different I advise you to put equal to confirm if this is not it

  • Yes, but I did everything as said in the tutorial, the only change I made was in the Loader file because it has to have the conditions of Heroku, but if you can, check there on the tutorial links and the WSDL itself that I left there.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.