2012-03-12 23 views
5

Tôi phát điên với các dịch vụ web.Gọi xà phòng webservice trả về danh sách các đối tượng từ máy khách java với trục

Tôi có một webservice xà phòng rất đơn giản:

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

tôi triển khai dịch vụ theo glassfish 3.1.

Sử dụng bảng điều khiển thủy tinh, nó hoạt động.

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

Sử dụng php nó cũng hoạt động (cho cả hai phương pháp).

Bây giờ với một khách hàng java:

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

Cuộc gọi đầu tiên được làm việc nhưng không phải là người thứ hai.

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) 

Tôi có thể làm gì?

Sau nhiều giờ tìm kiếm trên internet và thử các giải pháp khác nhau vẫn không hoạt động ...

Có giải pháp đơn giản nào không?

Cảm ơn.

Edit:

Cũng cố gắng rằng:

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

Với kết quả rằng:

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

Sinh viên có triển khai Serializable không? –

+0

Có - xem bài viết đã chỉnh sửa của tôi. – tweetysat

+0

Cố gắng đăng nhập tin nhắn. Theo thông báo lỗi tin nhắn sai: gặp phải phần tử con, không được mong đợi – jddsantaella

Trả lời

3

yếu tố đầu tiên của phản ứng sử dụng client xà phòng.

Vấn đề là đến từ không gian maptypes tên: không có không gian tên

Vì vậy, bây giờ, tôi có

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

Và cũng thêm

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

đến lớp sinh viên có

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

Client trục:

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 tất cả sinh viên:

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

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

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> 

Nhưng tôi không biết làm thế nào để deserialize omelement.

Tôi đã thử với

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

nhưng mang lại cho tôi

Student [matricule=null, name=null] 

Làm thế nào tôi có thể làm gì?

vấn đề Rời:

  • không biết làm thế nào để làm điều tương tự với trục --OK Xem trước -/trục 2
  • của resp '(xà phòng client) chứa chỉ đầu tiên sinh viên
+0

Không biết cách loại bỏ quy trình bỏ qua - xem reposnse đã chỉnh sửa. – tweetysat

0

Whats SOAP phong cách ràng buộc bạn đang sử dụng, RPC/TÀI LIỆU? Nếu bạn đang sử dụng RPC thì yêu cầu và phản hồi sẽ được mã hóa và xử lý, Axis2 không hỗ trợ các tin nhắn định dạng được mã hóa RPC. Hãy thử sử dụng phong cách ràng buộc DOCUMENT SOAP. (Tôi nên đã đặt này trong ý kiến, xin vui lòng chịu)

Tham khảo https://stackoverflow.com/a/9598193/752129

+0

Cảm ơn. Tôi đã thử thêm @SOAPBinding (style = Style.DOCUMENT, parameterStyle = ParameterStyle.WRAPPED) Cả AxisClient và SoapClient đều không hoạt động. Ngoại lệ tương tự. Nhưng tôi đang sử dụng trục và không trục2. – tweetysat

2

bạn có thể không nhận được đến một cái gì đó giống như WSDL

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

và sau đó sử dụng trục wsdl2java để Axis tạo mã máy khách cho bạn (sẽ có tất cả các ánh xạ và không gian tên loại chính xác)?

+0

Cảm ơn. Tôi đã không sử dụng giải pháp của bạn nhưng bây giờ khách hàng trục của tôi đang làm việc. – tweetysat

Các vấn đề liên quan