简体   繁体   English

JAXB元帅名称空间前缀

[英]JAXB marshal namespace prefix

I'm developing a client to consume a web service, but for some reason my requests are not properly processed unless all namespaces are correct and without any prefix. 我正在开发使用Web服务的客户端,但是由于某些原因,除非所有名称空间正确且没有任何前缀,否则我的请求将无法正确处理。

All my classes were created using the provide XSD's and WSDL's by the service provider. 服务提供商使用提供的XSD和WSDL创建了我所有的类。

NfeDadosMsg.class NfeDadosMsg.class

package br.inf.portalfiscal.nfe.wsdl.nfestatusservico4;

import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.bind.annotation.XmlMixed;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "content"
})
@XmlRootElement(name = "nfeDadosMsg")
public class NfeDadosMsg {

    @XmlMixed
    @XmlAnyElement(lax = true)
    protected List<Object> content;

    public List<Object> getContent() {
        if (content == null) {
            content = new ArrayList<Object>();
        }
        return this.content;
    }

}

package-info.java 包信息.java

@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.portalfiscal.inf.br/nfe/wsdl/NFeStatusServico4", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package br.inf.portalfiscal.nfe.wsdl.nfestatusservico4;

TConsStatServ.class package br.inf.portalfiscal.nfe; TConsStatServ.class包br.inf.portalfiscal.nfe;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.adapters.CollapsedStringAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(
    name = "TConsStatServ",
    propOrder = {"tpAmb", "cuf", "xServ"}
)
public class TConsStatServ {
    @XmlElement(
        required = true
    )
    protected String tpAmb;
    @XmlElement(
        name = "cUF",
        required = true
    )
    protected String cuf;
    @XmlElement(
        required = true
    )
    protected String xServ;
    @XmlAttribute(
        name = "versao",
        required = true
    )
    @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
    protected String versao;

    public TConsStatServ() {
    }

    ...

}

package-info.java 包信息.java

package br.inf.portalfiscal.nfe;

@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.portalfiscal.inf.br/nfe", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package br.inf.portalfiscal.nfe

Output xml: 输出xml:

<?xml version='1.0' encoding='UTF-8'?>
<S:Envelope xmlns:S="http://www.w3.org/2003/05/soap-envelope">
  <S:Body>
    <nfeDadosMsg xmlns="http://www.portalfiscal.inf.br/nfe/wsdl/NFeStatusServico4" xmlns:ns2="http://www.portalfiscal.inf.br/nfe">
      <ns2:consStatServ versao="4.00">
        <ns2:tpAmb>2</ns2:tpAmb>
        <ns2:cUF>33</ns2:cUF>
        <ns2:xServ>STATUS</ns2:xServ>
      </ns2:consStatServ>
    </nfeDadosMsg>
  </S:Body>
</S:Envelope>

For some reason the web service doesn't consumes it correctly if I have a prefixed namespace. 由于某种原因,如果我有带前缀的名称空间,Web服务将无法正确使用它。 So what I need 所以我需要

<?xml version='1.0' encoding='UTF-8'?>
<S:Envelope xmlns:S="http://www.w3.org/2003/05/soap-envelope">
  <S:Body>
    <nfeDadosMsg xmlns="http://www.portalfiscal.inf.br/nfe/wsdl/NFeStatusServico4">
      <consStatServ versao="4.00" xmlns="http://www.portalfiscal.inf.br/nfe">
        <tpAmb>2</tpAmb>
        <cUF>33</cUF>
        <xServ>STATUS</xServ>
      </consStatServ>
    </nfeDadosMsg>
  </S:Body>
</S:Envelope>

How can I change the namespace on the output xml? 如何更改输出xml上的名称空间?

Fixed by replacing 通过更换固定

@XmlMixed
@XmlAnyElement(lax = true)
protected List<Object> content;

With the expected entity 与预期实体

@XmlElement(namespace="http://www.portalfiscal.inf.br/nfe")
protected TRetEnviNfe;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM