Java Web Service SOAP with objects

0
Hello, I'm learning to use web-services and I stopped with a problem, maybe it's a silly thing, but I did not find a solution, I'm hosting my application in Heroku and I'll leave the relevant information here, but first I must explain what I would like do:

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

I now leave my code and the following error 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 is the WSDL generated by this code. I'm 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 think there is nothing different about the git commands and the Maven compilation, since the system is running on the server, as can be seen in the link above.

However when running a client it is the error.

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_consumidor.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()));

    }

}

So I have the following error on the 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.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 you do not use underline in Java nomenclature, but at the time I was more concerned with testing and differentiate from other projects, just that.
  • In the "Loader" class she is leaving the system to define its location and door, if I am not mistaken the standard door of Heroku is to 5000.
  • This is a test project and the client is also having this error solved and learning the concepts, the goal is that in the application I can access with a client in C #.

Thank you in advance for your attention.

    
asked by anonymous 31.12.2017 / 07:27

1 answer

-1

I think the problem may be in the @WebService annotation. You defined that the PersonService interface and the class that implements PersonServiceImpl have the same name webservice name="Person". If it was up to you, I'd remove this from both notes and try the following:

  

QName qname = new   QName (" link ", "PersonServiceImpl" );

    
02.01.2018 / 14:30