java - 在 Java 中将类作为参数传递给 SOAP Web 服务

标签 java web-services soap parameter-passing

您好,我想将一个类作为参数传递给 Web 服务。 这是我的网络服务代码:

<xs:complexType name="RegisterStudent">
<xs:sequence>
<xs:element name="student" type="tns:student" minOccurs="0"/>
<xs:element name="user" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>

这是我的调用函数:

public static String registerStudentClass(String user) {
soapAction="http://auth.ws.df.com/RegisterStudent";
methodName="RegisterStudent";
String resTxt = null;

Student student= new Student();
student.setAge(22);
student.setName("Jerry");

SoapObject request = new SoapObject(NAMESPACE, methodName);
request.addProperty("student", student);//Student class added here
request.addProperty("user", user);//User name, passed as string

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
System.out.println("androidHttpTransport envelope");

    try{
        androidHttpTransport.call(soapAction, envelope);
        SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
        resTxt = response.toString();
    }catch(Exception e){
        e.printStackTrace();
        resTxt = "Error occured\n"+e;
    }

    return resTxt;

}

这是我的学生类(class):

Public Class Student implements Serializable{
  private int age;
  private String name;
  public Student(){}
  public Student(int age, String name){
    this.age = age;
    this.name = name;
  }
  //setter and getter methods come here.
  //...
}

运行此命令后,出现以下错误: “无法序列化:Student@4329d250”。请帮忙。

提前致谢

最佳答案

所以我想出了如何做到这一点: 首先,使类可序列化

Public Class Student implements KvmSerializable{

private int age;
private String name;

public Student(){}

public Student(int age, String name){
   this.age = age;
   this.name = name;
  }
  //setter and getter methods come here.
  //...

  public Object getProperty(int arg0) {
      switch(arg0){
      case 0: 
          return getAge();
      case 1:
          return getName();
      }
      return null;
  }

  public int getPropertyCount() {
      return 2;
  }

  public void setProperty(int index, Object value) {
      switch(index){
      case 0 :
         age = value.toString();
         break;
      case 1 :
         name = value.toString();
         break;
      default:
         break;
      }
  }
}

WebService 方法如下所示:

public static String registerStudentClass(Student stud) {
  soapAction="http://auth.ws.df.com/RegisterStudent";
  methodName="RegisterStudent";
  String resTxt = null;

  //new change
  PropertyInfo pi = new PropertyInfo();
  pi.setName("stud");
  pi.setValue(stud);
  pi.setType(stud.getClass());

  SoapObject request = new SoapObject(NAMESPACE, methodName);
  request.addProperty(pi);

  SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
  SoapEnvelope.VER11);
  envelope.setOutputSoapObject(request);//new change
  envelope.addMapping(NAMESPACE, "Student", new Student().getClass());
  HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
  System.out.println("androidHttpTransport envelope");

  try{
      androidHttpTransport.call(soapAction, envelope);
      SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
      resTxt = response.toString();
   }catch(Exception e){
      e.printStackTrace();
      resTxt = "Error occured\n"+e;
  }

  return resTxt;
}

我假设响应是文本,例如 1 - 已成功注册 2 - 注册失败等

关于java - 在 Java 中将类作为参数传递给 SOAP Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30022937/

相关文章:

java - 如何从java运行MATLAB文件(.m文件)?

java - 如何仅在运行junit时从项目类路径中删除依赖项?

java - 如何在不使用任何集合和循环的情况下删除 List<MyObject> 中的重复对象

Java - 如何在不声明的情况下获取列表中的元素?

c++ - Boost C++ http post 获取服务 : How to make more than one run on same port?

java - 如何为@RestController 启用日志记录?

php - 如何让 PHP SOAP 客户端与通过 SSL 使用无效证书运行的服务进行通信

web-services - 如何使用 FastAPI 作为 RabbitMQ (RPC) 的消费者

python - 是否有 Zolera SOAP Infrastructure (ZSI) 的任何工作示例?

java - Spring boot + Apache CXF 用于 SOAP 网络服务