web-services - 返回 Pojo WebService JaxWS。重命名 <return> 节点

标签 web-services jaxb jax-ws pojo

我认为这应该很容易,但我真的做不到。

我从 WebMethod 返回一个 Pojo:

@WebMethod
public SubCategoria getSubCategorias() throws JAXBException {

    SubCategoria a = subCategoriaEJB.getAllSubCategorias().get(1);

    return a;
}

我只是返回第一个,尝试一下。

我正在使用 soapUI 来测试我的 Ws。

响应是:

    <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
       <S:Body>
          <ns2:getSubCategoriasResponse xmlns:ns2="http://webService/">
             <return>
                <categoria>
                   <descripcion>Categoria Unica</descripcion>
                   <idCategoria>1</idCategoria>
                </categoria>
                <descripcion>asd123213</descripcion>
                <idSubCategoria>2</idSubCategoria>
             </return>
          </ns2:getSubCategoriasResponse>
       </S:Body>
    </S:Envelope>

我希望将“返回”节点称为“SubCategoria”。我真的不能让它与 XmlRootElement 注释一起工作。

这是我的 Pojo(子类别)

    package ejb.Entidades;

    import javax.persistence.Entity;
    import javax.persistence.Id;
    import javax.persistence.ManyToOne;
    import javax.xml.bind.annotation.XmlRootElement;


    @Entity
    @XmlRootElement(name="SubCategoria")
    public class SubCategoria {

        @Id
        private Integer idSubCategoria;

        @ManyToOne 
        private Categoria categoria;


        private String descripcion;


        public Integer getIdSubCategoria() {
            return idSubCategoria;
        }
        public void setIdSubCategoria(Integer idSubCategoria) {
            this.idSubCategoria = idSubCategoria;
        }

        public String getDescripcion() {
            return descripcion;
        }
        public void setDescripcion(String descripcion) {
            this.descripcion = descripcion;
        }
        public Categoria getCategoria() {
            return categoria;
        }
        public void setCategoria(Categoria categoria) {
            this.categoria = categoria;
        }

    }

有人知道吗?

提前致谢。

最佳答案

你应该使用@WebResult注解:

@WebMethod
@WebResult(name = "subCategoria")
public SubCategoria getSubCategorias() throws JAXBException {

    SubCategoria a = subCategoriaEJB.getAllSubCategorias().get(1);

    return a;
}

关于web-services - 返回 Pojo WebService JaxWS。重命名 <return> 节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12169350/

相关文章:

java - Web 服务异常 - 没有 "?wsdl"的链接,HTTP GET PATH_INFO

web-services - 由于异常 : XML reader error: com. ctc.wstx.exc.WstxParsingException:未声明的命名空间前缀 "ws",无法创建 SOAP 消息

java - 日期和日期时间应该如何序列化 SOAP (xml) 消息

android - 如何使用凭据从 Android 调用 .NET Web 服务?

spring - 如何在 Spring 从 jaxb 获取格式化的 xml 输出?

java - 用于响应的 JAX-WS 和 SOAP 映射

web-services - JAX-RPC GenericHandler 在 Websphere Application Server v6.0.2.35 上失败

java - 初学者 Web 服务问题

java - 使用 CXF/JAXB2 生成和自定义 equals/hashcode 方法

JAVA/JAXB 需要帮助