Invoke soap webservice returning list of objects from java client with axis

不羁岁月 提交于 2019-12-05 09:37:40

First element of response using soap client.

The problem is coming from the maptypes name space : there is no namespace

So now, I have

    smr.mapTypes(Constants.NS_URI_SOAP_ENC,new QName("","student"),Student.class, null, new BeanSerializer());  
    smr.mapTypes(Constants.NS_URI_SOAP_ENC,new QName("","matricule"),Integer.class, null, new IntDeserializer());   
    smr.mapTypes(Constants.NS_URI_SOAP_ENC,new QName("","name"),Integer.class, null, new StringDeserializer()); 

And also add

@XmlRootElement(name = "Student",namespace="http://services.tuto.java.com/")

to the Student class to have

<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Body>
        <ns2:getStudentsResponse xmlns:ns2="http://services.tuto.java.com/">
            <student>
                <matricule>1236</matricule>
                <name>student1</name>
            </student>
            <student>
                <matricule>5678</matricule>
                <name>student2</name>
            </student>
        </ns2:getStudentsResponse>
    </S:Body>
</S:Envelope>

The axis client :

 public class AxisClient
{
    public static void main(String[] args) throws Exception
    {
    String endPoint = "http://localhost:8080/StudentServiceImplService/StudentServiceImpl";
    Service service2 = new Service();
    Call call2 = (Call) service2.createCall();
    call2.setTargetEndpointAddress(new java.net.URL(endPoint));
    call2.setOperationName(new QName("http://services.tuto.java.com/","getStudents"));
    call2.setReturnType(new QName("","student"), Student.class);
    call2.setReturnType(new QName("", "student"));
    call2.registerTypeMapping(Student.class, new QName("", "student"), null,new BeanDeserializerFactory(Student.class, new QName("", "student")));
    List<Student> students = (List<Student>) call2.invoke(new Object[0]);
    for (Student student : students)
    {
        System.out.println(student);
    }
    }
 }

Giving all students :

Student [matricule=1236, name=student1]
Student [matricule=5678, name=student2]

The axis2 client :

public static void main(String[] args) throws Exception
{
    String endPoint = "http://localhost:8080/StudentServiceImplService/StudentServiceImpl";

    ServiceClient sc = new ServiceClient();

    Options opts = new Options();
    opts.setTo(new EndpointReference("http://localhost:8080/StudentServiceImplService/StudentServiceImpl"));
    sc.setOptions(opts);

    OMFactory fac = OMAbstractFactory.getOMFactory();
    OMNamespace omNs = fac.createOMNamespace("http://services.tuto.java.com/","ns1");

    OMElement method = fac.createOMElement("getStudents", omNs);
    OMElement res = sc.sendReceive(method);
    System.out.println(res);

    Iterator<OMElement> it = res.getChildElements();
    while(it.hasNext())
    {
        System.out.println(it.next());
    }
}

Giving

<ns2:getStudentsResponse xmlns:ns2="http://services.tuto.java.com/"><student><matricule>1236</matricule><name>student1</name></student><student><matricule>5678</matricule><name>student2</name></student></ns2:getStudentsResponse>
<student><matricule>1236</matricule><name>student1</name></student>
<student><matricule>5678</matricule><name>student2</name></student>

But I don't know how to deserialize the omelement.

I tried with

Student student = (Student) BeanUtil.deserialize(Student.class,res,new DefaultObjectSupplier(),null);

but gives me

Student [matricule=null, name=null]

How can I do ?

Leaving problems :

  • don't know how to do the same with axis --OK See before--/axis 2
  • the 'resp' (soap client) contains only first student

Can you not get to the WSDL something like

http://localhost:8080/StudentServiceImplService/StudentServiceImpl?wsdl

and then use axis wsdl2java to let Axis create the client code for you (which will have all the correct type mappings and namespaces)?

Murugesh

Whats the SOAP binding style you are using,RPC/DOCUMENT ? If you are using RPC then the request and response will be encoded and processed,Axis2 is not supporting the RPC encoded format messages. Try using DOCUMENT SOAP binding style. (I should have put this in comments,kindly bear)

Refer https://stackoverflow.com/a/9598193/752129

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!