2012-03-12 19 views
5

के साथ ऑब्जेक्ट्स की साबुन webservice लौटने वाली सूची को शामिल करें, मैं webservices के साथ पागल हो रहा हूं।जावा क्लाइंट से अक्ष

@Remote 
public interface StudentService 
{ 
public String sayHello(); 
public List<Student> getStudents(); 
} 

और

@Stateless 
@WebService 
public class StudentServiceImpl implements StudentService 
{ 

@Override 
public String sayHello() 
{ 
    return "Hello World"; 
} 

public List<Student> getStudents() 
{ 
    List<Student> students = new ArrayList<Student>(); 

    Student st1 = new Student(); 
    st1.setMatricule(1234); 
    st1.setName("student1"); 
    students.add(st1); 

    Student st2 = new Student(); 
    st2.setMatricule(5678); 
    st2.setName("student2"); 
    students.add(st2); 

    return students; 
    } 
} 

और

public class Student implements Serializable 
{ 
private static final long serialVersionUID = 8286393242028201686L; 

private int matricule; 
private String name; 

public int getMatricule() 
{ 
    return matricule; 
} 
public void setMatricule(int matricule) 
{ 
    this.matricule = matricule; 
} 

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

मैं glassfish 3.1 के तहत सेवा को तैनात:

मैं एक बहुत ही सरल साबुन वेब सेवा की है।

ग्लासफ़िश कंसोल का उपयोग करके, यह काम कर रहा है।

<?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/"> 
      <return> 
       <matricule>1234</matricule> 
       <name>student1</name> 
      </return> 
      <return> 
       <matricule>5678</matricule> 
       <name>student2</name> 
      </return> 
     </ns2:getStudentsResponse> 
    </S:Body> 
</S:Envelope> 

php यह भी (दोनों तरीकों के लिए) काम कर रहा है का उपयोग करना।

एक जावा ग्राहक के साथ

अब:

import javax.xml.namespace.QName; 

import org.apache.axis.client.Call; 
import org.apache.axis.client.Service; 

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

    Service service = new Service(); 
    Call call = (Call) service.createCall(); 
    call.setTargetEndpointAddress(new java.net.URL(endPoint)); 
    call.setOperationName(new QName("http://services.tuto.java.com/","sayHello")); 

    System.out.println(call.invoke(new Object[0])); 

    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")); 

    System.out.println(call2.invoke(new Object[0])); 
    } 
} 

पहली कॉल काम कर रहा है, लेकिन दूसरी नहीं है।

Hello World 
12-mars-2012 14:53:23 org.apache.axis.client.Call invoke 
GRAVE: Exception: 
org.xml.sax.SAXException: SimpleDeserializer encountered a child element, which is NOT expected, in something it was trying to deserialize. 
at org.apache.axis.encoding.ser.SimpleDeserializer.onStartChild(SimpleDeserializer.java:145) 
at org.apache.axis.encoding.DeserializationContext.startElement(DeserializationContext.java:1035) 
at org.apache.axis.message.SAX2EventRecorder.replay(SAX2EventRecorder.java:165) 
at org.apache.axis.message.MessageElement.publishToHandler(MessageElement.java:1141) 
at org.apache.axis.message.RPCElement.deserialize(RPCElement.java:345) 
at org.apache.axis.message.RPCElement.getParams(RPCElement.java:384) 
at org.apache.axis.client.Call.invoke(Call.java:2467) 
at org.apache.axis.client.Call.invoke(Call.java:2366) 
at org.apache.axis.client.Call.invoke(Call.java:1812) 
at Client.main(Client.java:24) 

मैं क्या कर सकता हूं?

इंटरनेट पर खोज और विभिन्न समाधान अभी भी काम कुछ भी नहीं की कोशिश कर रहा घंटे के बहुत बाद ...

वहाँ एक सरल उपाय है?

धन्यवाद।

संपादित करें:

भी करने की कोशिश की है कि:

public class SoapClient 
{ 
public static void main(String[] args) throws Exception 
{ 
    SOAPMappingRegistry smr = new SOAPMappingRegistry(); 
    BeanSerializer beanSer = new BeanSerializer(); 
    smr.mapTypes(Constants.NS_URI_SOAP_ENC,new QName("http://services.tuto.java.com/", "StudentServiceImplService"),Student.class, beanSer, beanSer);  

    Call call = new Call(); 
    call.setSOAPMappingRegistry(smr); 
    call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC); 

    call.setTargetObjectURI("http://services.tuto.java.com/"); 
    call.setMethodName("getStudents"); 

    Response resp; 
    try 
    { 
     resp = call.invoke(new URL("http://8h9l45j:8080/StudentServiceImplService/StudentServiceImpl"), ""); 
    } 
    catch (SOAPException e) 
    { 
     System.err.println("Caught SOAPException (" + 
      e.getFaultCode() + "): " + e.getMessage()); 
     return; 
    } 

    if (!resp.generatedFault()) 
    { 
     Parameter ret = resp.getReturnValue(); 
     Object value = ret.getValue(); 
     if (value != null) 
     { 
      String[] tlist = (String[])value; 
      System.out.println(); 
      for (int i = 0; i < tlist.length; i++) 
       System.out.println(tlist[i]); 
     } 
    } 
    else 
    { 
     Fault fault = resp.getFault(); 
     System.err.println("Generated fault: "); 
     System.out.println (" Fault Code = " 
          + fault.getFaultCode()); 
     System.out.println (" Fault String = " 
          + fault.getFaultString()); 
    } 
} 
कि परिणाम के साथ

:

Caught SOAPException (SOAP-ENV:Client): No Deserializer found to deserialize a ':return' using encoding style 'http://schemas.xmlsoap.org/soap/encoding/'. 
+0

करता छात्र Serializable लागू? –

+0

हाँ - मेरी संपादित पोस्ट देखें। – tweetysat

+0

संदेश लॉग करने का प्रयास करें। त्रुटि संदेश संदेश के मुताबिक गलत है: एक बच्चे तत्व का सामना करना पड़ा, जिसकी उम्मीद नहीं है – jddsantaella

उत्तर

3

साबुन ग्राहक का उपयोग कर प्रतिक्रिया के पहले तत्व।

समस्या maptypes नाम अंतरिक्ष से आ रही है: वहाँ कोई नाम स्थान

तो अब, मैं

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

है और यह भी छात्र वर्ग के लिए

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

जोड़ने के लिए है

<?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> 

अक्ष ग्राहक:

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); 
    } 
    } 
} 

सभी छात्रों देते:

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

Axis2 ग्राहक:

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()); 
    } 
} 

देते

<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> 

लेकिन मैं omelement deserialize करने के लिए कैसे पता नहीं है।

मैं

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

साथ की कोशिश की लेकिन मुझे

Student [matricule=null, name=null] 

देता है मैं कैसे कर सकता है?

छोड़कर समस्याओं:

  • कैसे अक्ष --OK देखें साथ भी ऐसा ही करने के लिए पता नहीं है इससे पहले कि -/अक्ष 2
  • 'resp' (साबुन ग्राहक) शामिल हैं केवल पहले छात्र
+0

ओमेलेमेंट को deserialize कैसे पता नहीं है - संपादित reposnse देखें। – tweetysat

0

क्या सोप बाध्यकारी शैली आप उपयोग कर रहे, आरपीसी/दस्तावेज? यदि आप आरपीसी का उपयोग कर रहे हैं तो अनुरोध और प्रतिक्रिया एन्कोड और संसाधित की जाएगी, एक्सिस 2 आरपीसी एन्कोडेड प्रारूप संदेशों का समर्थन नहीं कर रहा है। दस्तावेज़ SOAP बाध्यकारी शैली का उपयोग करने का प्रयास करें। (मैं टिप्पणी में यह रखना चाहिए है, कृपया सहन)

देखें https://stackoverflow.com/a/9598193/752129

+0

धन्यवाद। मैंने @SOAP बाइंडिंग (शैली = स्टाइल.डोक्यूमेंट, पैरामीटर स्टाइल = पैरामीटर स्टाइल। रैपपेड) जोड़ने की कोशिश की एक्सिस क्लाइंट और सोप क्लाइंट दोनों काम नहीं कर रहे हैं। वही अपवाद लेकिन मैं अक्ष का उपयोग कर रहा हूं और अक्ष 2 नहीं। – tweetysat

2

आप नहीं

http://localhost:8080/StudentServiceImplService/StudentServiceImpl?wsdl 
तरह डबल्यूएसडीएल कुछ करने के लिए मिल सकता है

और फिर wsdl2java अक्ष का उपयोग एक्सिस आप के लिए ग्राहक कोड (जो सभी सही प्रकार मैपिंग और नामस्थान होगा) बनाने के लिए?

+0

धन्यवाद। मैंने आपके समाधान का उपयोग नहीं किया लेकिन अब मेरा अक्ष क्लाइंट काम कर रहा है। – tweetysat

संबंधित मुद्दे