How to send an object via SOAP web service

Asked

Viewed 1,511 times

8

To send a primitive data is simple, but when it comes to complex data as shown below, an exception is thrown:

java.lang.Runtimeexception: Cannot serialize: Person{name=given, address=given etc...}

Most tutorials and examples address the use of primitive types and not complex types as the question here presents.

Given an object represented below:

public class Pessoa {
    private String nome;
    private String endereco;
    private List<String> emails;
    private boolean ativo;

    // getters e setters

    @Override
    public String toString(){
        return "Pessoa{nome="+dado+", endereco="+dado+" etc...}";
    }
}

How to send it to a SOAP standard web service using the kSOAP library?

  • Quite broad this question, no? It seems to me that the valid for Sopt would be you do the implementation and post here the problems encountered. Apparently kSOAP is most commonly used on Android (http://www.emersonbarros.com.br/ksoap-2-consumindo-webservice-com-android/). Is this your requirement? For other needs I have always used JAX-WS for SOAP webservices.

  • Your link does not meet my need, because I need to send complex, not primitive types. But I will give an improvement on the question.

3 answers

1

I don’t know if this is the correct answer, because I have no way of testing what you are doing. But I will try to answer anyway:


java.lang.Runtimeexception: Cannot serialize: Person{name=given, address=given etc...}

This message occurs because you tried to serialize a person and when the exception was generated, the method toString() the person was summoned to build the exception message.

Normally, serializable objects implement the interface Serializable and contains all serializable fields. Primitive types are serializable, String is serializable. If the class implying its List is serializable (almost all are, including ArrayList) and all the elements in the list are serializable so the entire list is serializable.

So maybe your solution is just to add the implements Serializable in the definition of the class.

  • I did, but the exception is still cast.

  • 2

    @williamhk2 I understand your good intention in improving the formatting of the text, but in the case of error message, it is the exact result of the method toString(), including any formatting, grammar or spelling errors or anything. It is important that the error message is presented exactly as it was produced, otherwise it will not match the truth.

  • @Victor got it, thanks.

1

You have to implement the Serializable Interface

public class Employee implements java.io.Serializable
{
  public String name;
  public String address;
  public transient int SSN;
  public int number;

  public void mailCheck()
  {
     System.out.println("Mailing a check to " + name + " " + address);
  }
}

1

You do not send the whole Person object, the best would be to create a serializer of the object. A SOAP message is nothing more than a STRING with an XML format following some different patterns.

Example cited above is on the site: serializer & deserializer

Browser other questions tagged

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