本文介绍了Java Webservices和SOAP-更改元素名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个返回自定义类型的Java网络服务。一切正常,除非当我查看SOAP响应时,它不使用名称 myType,而是使用返回。

I'm writing a java web service that returns a custom type. Everything works fine except when I look at the SOAP response it doesn't use the name "myType" - it uses "return"

这是我的SOAP响应-基本上它说 return,我想说 mytype

This is my SOAP response - basically where it says "return", I want it to say "mytype"

S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <ns2:MethodResponse xmlns:ns2="http://myWebservice/">
         <return>
            <field1>sdf</field1>
            <field2>sdf</field2>
       </return>
      </ns2:MethodResponse >
   </S:Body>
</S:Envelope>

Class
包myWebserivce

Classpackage myWebserivce

import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;


@WebService(serviceName = "myWebserivce")
public class myWebserivce{

    @WebMethod(operationName = "Method")
    public MyType Method(@WebParam(name = "string1") String string1, @WebParam(name = "string2") String string2) {

        MyType mt = new MyType();
        mt.setField1(string1);
        mt.setfield2(string2);

        return mt;
    }
}

MyType类

import javax.xml.bind.annotation.XmlRootElement;


@XmlRootElement(name="MyType")
public class MyType {

    private String field1;
    private String field2;

    public String getField1() {
        return field1;
    }

    public void setField1(String field1) {
        this.field1 = field1;
    }

    public String getField2() {
        return field2;
    }

    public void setField2(String field2) {
        this.field2 = field2;
    }


}

解决方案

import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;


@WebService(serviceName = "myWebserivce")
public class myWebserivce{

    @WebMethod(operationName = "Method")
    @WebResult(name="MyType")
    public MyType Method(@WebParam(name = "string1") String string1, @WebParam(name = "string2") String string2) {

        MyType mt = new MyType();
        mt.setField1(string1);
        mt.setfield2(string2);

        return mt;
    }
}


推荐答案

您'需要确保 myType @XmlRootElement(name = myType)进行注释。 (您可能也需要使用 @WebResult(name = myType)注释该方法。

You'll need to make sure myType is annotated with @XmlRootElement(name="myType"). (You might need to annotate the method with @WebResult(name="myType") too.

(在Java,类名以大写字母开头,因此实际上应该为 MyType

(In Java, class names start with an uppercase letter so it should really be MyType)

这篇关于Java Webservices和SOAP-更改元素名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 01:34