Cxf com ns2 no result : resolvido

1 resposta
D

Boa tarde!

Nessa semana estive com esse problema e, junto com Ana Lunardi, conseguimos resolver sem “muitos traumas”.

Segue a resposta :

package-info

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

ObjectFactory

import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlElementDecl;
import javax.xml.bind.annotation.XmlRegistry;
import javax.xml.namespace.QName;

/**
 * This object contains factory methods for each 
 * Java content interface and Java element interface 
 * generated in the br.inf.portalfiscal.nfe.wsdl.nfestatusservico2 package. 
 * <p>An ObjectFactory allows you to programatically 
 * construct new instances of the Java representation 
 * for XML content. The Java representation of XML 
 * content can consist of schema derived interfaces 
 * and classes representing the binding of schema 
 * type definitions, element declarations and model 
 * groups.  Factory methods for each of these are 
 * provided in this class.
 * 
 */
@XmlRegistry
public class ObjectFactory {

    private final static QName _NfeCabecMsg_QNAME = new QName("http://www.portalfiscal.inf.br/nfe/wsdl/NfeStatusServico2", "nfeCabecMsg");

    /**
     * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: br.inf.portalfiscal.nfe.wsdl.nfestatusservico2
     * 
     */
    public ObjectFactory() {
    }

    /**
     * Create an instance of {@link NfeCabecMsg }
     * 
     */
    public NfeCabecMsg createNfeCabecMsg() {
        return new NfeCabecMsg();
    }

    /**
     * Create an instance of {@link NfeStatusServicoNF2Result }
     * 
     */
    public NfeStatusServicoNF2Result createNfeStatusServicoNF2Result() {
        return new NfeStatusServicoNF2Result();
    }

    /**
     * Create an instance of {@link NfeDadosMsg }
     * 
     */
    public NfeDadosMsg createNfeDadosMsg() {
        return new NfeDadosMsg();
    }

    /**
     * Create an instance of {@link JAXBElement }{@code <}{@link NfeCabecMsg }{@code >}}
     * 
     */
    @XmlElementDecl(namespace = "http://www.portalfiscal.inf.br/nfe/wsdl/NfeStatusServico2", name = "nfeCabecMsg")
    public JAXBElement<NfeCabecMsg> createNfeCabecMsg(NfeCabecMsg value) {
        return new JAXBElement<NfeCabecMsg>(_NfeCabecMsg_QNAME, NfeCabecMsg.class, null, value);
    }

}

NFeStatusServicoNF2Result

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.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;


/**
 * <p>Java class for anonymous complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType>
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;any/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "nfeStatusServicoNF2Result")
public class NfeStatusServicoNF2Result {

	@XmlElement(name="retConsStatServ")
	protected List<Object> content;

    /**
     * Gets the value of the content property.
     * 
     * <p>
     * This accessor method returns a reference to the live list,
     * not a snapshot. Therefore any modification you make to the
     * returned list will be present inside the JAXB object.
     * This is why there is not a <CODE>set</CODE> method for the content property.
     * 
     * <p>
     * For example, to add a new item, do as follows:
     * <pre>
     *    getContent().add(newItem);
     * </pre>
     * 
     * 
     * <p>
     * Objects of the following type(s) are allowed in the list
     * {@link Object }
     * {@link String }
     * 
     * 
     */
	public List<Object> getContent() {
        if (content == null) {
            content = new ArrayList<Object>();
        }
        return this.content;
    }
}

NfeStatusServico2SoapImpl : dentro do metodo nfeStatusServicoNF2 colocar o codigo

...

NfeStatusServicoNF2Result result = null;
		
		try {
			Document xml = NfeDadosMsg.loadXMLFrom(retorno);
			
			result = new ObjectFactory().createNfeStatusServicoNF2Result();
			List<Object> values = result.getContent();
			
			values.add(xml.getDocumentElement());
			
		} catch (SAXException e) {
			log.info("ERRO SAXEXPTION montarRetornoWS STATUS_SERVICO_IMPL ");
			throw new RuntimeException(e);
			
		} catch (IOException e) {
			log.info("ERRO IOEXCEPTION montarRetornoWS DO STATUS_SERVICO_IMPL ");
			throw new RuntimeException(e);
			
		}
		
		return result;

NfeStatusSercivo2Soap

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.ws.BindingType;
import javax.xml.ws.Holder;

/**
 * This class was generated by Apache CXF 2.2.1
 * Thu Jan 14 16:38:23 BRST 2010
 * Generated source version: 2.2.1
 * 
 */
 
@WebService(targetNamespace = "http://www.portalfiscal.inf.br/nfe/wsdl/NfeStatusServico2", name = "NfeStatusServico2Soap")
@XmlSeeAlso({ObjectFactory.class})
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)
@BindingType(value = javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_BINDING)
public interface NfeStatusServico2Soap {

    @WebResult(name = "nfeStatusServicoNF2Result", targetNamespace = "http://www.portalfiscal.inf.br/nfe/wsdl/NfeStatusServico2", partName = "nfeStatusServicoNF2Result")
    @WebMethod(action = "http://www.portalfiscal.inf.br/nfe/wsdl/NfeStatusServico2/nfeStatusServicoNF2")
    public NfeStatusServicoNF2Result nfeStatusServicoNF2(
        @WebParam(partName = "nfeDadosMsg", name = "nfeDadosMsg", targetNamespace = "http://www.portalfiscal.inf.br/nfe/wsdl/NfeStatusServico2")
        NfeDadosMsg nfeDadosMsg,
        @WebParam(partName = "nfeCabecMsg", mode = WebParam.Mode.INOUT, name = "nfeCabecMsg", targetNamespace = "http://www.portalfiscal.inf.br/nfe/wsdl/NfeStatusServico2", header = true)
        Holder<NfeCabecMsg> nfeCabecMsg
    );
}

NfeDadosMsg

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;

import org.w3c.dom.Document;

import br.inf.portalfisca.nfe.wsdl.util.ManipulaCabecDadosWS;


/**
 * <p>Java class for anonymous complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType>
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;any/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "content"
})
@XmlRootElement(name = "nfeDadosMsg")
public class NfeDadosMsg {

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

    /**
     * Gets the value of the content property.
     * 
     * <p>
     * This accessor method returns a reference to the live list,
     * not a snapshot. Therefore any modification you make to the
     * returned list will be present inside the JAXB object.
     * This is why there is not a <CODE>set</CODE> method for the content property.
     * 
     * <p>
     * For example, to add a new item, do as follows:
     * <pre>
     *    getContent().add(newItem);
     * </pre>
     * 
     * 
     * <p>
     * Objects of the following type(s) are allowed in the list
     * {@link Object }
     * {@link String }
     * 
     * 
     */
    public List<Object> getContent() {
        if (content == null) {
            content = new ArrayList<Object>();
        }
        return this.content;
    }
    
    public static Document loadXMLFrom(String xml)throws org.xml.sax.SAXException, java.io.IOException {
		ManipulaCabecDadosWS manipula = new ManipulaCabecDadosWS();
		return 	manipula.loadStringDocument(xml);
	}
}

NfeCabecMsg

import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAnyAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.namespace.QName;


/**
 * <p>Java class for nfeCabecMsg complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType name="nfeCabecMsg">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="cUF" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 *         &lt;element name="versaoDados" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "nfeCabecMsg", propOrder = {
    "cuf",
    "versaoDados"
})
public class NfeCabecMsg {

    @XmlElement(name = "cUF")
    protected String cuf;
    protected String versaoDados;
    @XmlAnyAttribute
    private Map<QName, String> otherAttributes = new HashMap<QName, String>();

    /**
     * Gets the value of the cuf property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getCUF() {
        return cuf;
    }

    /**
     * Sets the value of the cuf property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setCUF(String value) {
        this.cuf = value;
    }

    /**
     * Gets the value of the versaoDados property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getVersaoDados() {
        return versaoDados;
    }

    /**
     * Sets the value of the versaoDados property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setVersaoDados(String value) {
        this.versaoDados = value;
    }

    /**
     * Gets a map that contains attributes that aren't bound to any typed property on this class.
     * 
     * <p>
     * the map is keyed by the name of the attribute and 
     * the value is the string value of the attribute.
     * 
     * the map returned by this method is live, and you can add new attribute
     * by updating the map directly. Because of this design, there's no setter.
     * 
     * 
     * @return
     *     always non-null
     */
    public Map<QName, String> getOtherAttributes() {
        return otherAttributes;
    }

}

ManipulaCabecDadosMsg

public class ManipulaCabecDadosWS {
	/**
	 * método resposnavel por transformar o conteudo do WS EmentNSI para uma string
	 * 
	 * @author 
	 * @param conteudoElement
	 * @return
	 */
	public String ElementoString(List<Object> conteudoElement) {
		ElementNSImpl doc = (ElementNSImpl) conteudoElement.get(0);
		Document document = doc.getOwnerDocument();
		Document doc1 = (org.w3c.dom.Document) document;
		ManipulaXml manipula = new ManipulaXml();
		return manipula.getStringFromDoc(doc1);
	}

	/**
	 * metodo responsavel por tarnsformar a String q retorna do processamento do
	 * WS para um Document para poder ser colocado num envelope 16/09/09
	 * 
	 * @author
	 * @param xml
	 * @return
	 * @throws org.xml.sax.SAXException
	 * @throws java.io.IOException
	 */
	public Document loadStringDocument(String xml) throws org.xml.sax.SAXException, java.io.IOException {
		ByteArrayInputStream inputStreamXML = new ByteArrayInputStream(xml.getBytes());
		Document doc = loadInputStreamDocument(inputStreamXML);
		return doc;
	}

	private Document loadInputStreamDocument(InputStream is) throws org.xml.sax.SAXException, java.io.IOException {
		// Instancia o documento a ser validado
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		dbf.setNamespaceAware(true);

		Document doc = null;
		try {
			doc = dbf.newDocumentBuilder().parse(is);
		
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		
		}

		return doc;
	}

	public String getStringFromDoc(org.w3c.dom.Document doc) {
		try {
			DOMSource domSource = new DOMSource(doc);
			StringWriter writer = new StringWriter();
			StreamResult result = new StreamResult(writer);
			TransformerFactory tf = TransformerFactory.newInstance();
			Transformer transformer = tf.newTransformer();
			transformer.transform(domSource, result);
			writer.flush();
			
			return writer.toString();
		
		} catch (TransformerException ex) {
			ex.printStackTrace();
			return null;
		
		}
	}
	
	public static String xmlToString(Node node) {
		try {
			Source source = new DOMSource(node);
			StringWriter stringWriter = new StringWriter();
			Result result = new StreamResult(stringWriter);
			TransformerFactory factory = TransformerFactory.newInstance();
			Transformer transformer = factory.newTransformer();
			transformer.transform(source, result);
			return stringWriter.getBuffer().toString();
		
		} catch (TransformerConfigurationException e) {
			e.printStackTrace();
		
		} catch (TransformerException e) {
			e.printStackTrace();
		
		}
		
		return null;
	}
}

Exemplo de solicitacao e retorno COM ns2 …

solicitacao:

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:nfes="http://www.portalfiscal.inf.br/nfe/wsdl/NfeStatusServico2">
   <soap:Header>
      <nfes:nfeCabecMsg>
         <nfes:cUF>31</nfes:cUF>
         <nfes:versaoDados>2.00</nfes:versaoDados>
      </nfes:nfeCabecMsg>
   </soap:Header>
   <soap:Body>
      <nfes:nfeDadosMsg>
         <consStatServ xmlns="http://www.portalfiscal.inf.br/nfe" versao="2.00">
				<tpAmb>2</tpAmb>
				<cUF>31</cUF>
				<xServ>STATUS</xServ>
			</consStatServ>
      </nfes:nfeDadosMsg>
   </soap:Body>
</soap:Envelope>

retorno:

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
   <soap:Header>
      <ns2:nfeCabecMsg xmlns:ns2="http://www.portalfiscal.inf.br/nfe/wsdl/NfeStatusServico2">
         <ns2:cUF>31</ns2:cUF>
         <ns2:versaoDados>2.00</ns2:versaoDados>
      </ns2:nfeCabecMsg>
   </soap:Header>
   <soap:Body>
      <ns2:nfeStatusServicoNF2Result xmlns:ns2="http://www.portalfiscal.inf.br/nfe/wsdl/NfeStatusServico2">
         <retConsStatServ versao="2.00" xmlns="http://www.portalfiscal.inf.br/nfe">
            <tpAmb>2</tpAmb>
            <verAplic>13_0_82</verAplic>
            <cStat>107</cStat>
            <xMotivo>Servico em operacao</xMotivo>
            <cUF>31</cUF>
            <dhRecbto>2013-05-16T14:31:50</dhRecbto>
            <tMed>1</tMed>
         </retConsStatServ>
      </ns2:nfeStatusServicoNF2Result>
   </soap:Body>
</soap:Envelope>

retorno SEM ns2:

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
   <soap:Header>
      <nfeCabecMsg xmlns="http://www.portalfiscal.inf.br/nfe/wsdl/NfeStatusServico2">
         <cUF>31</cUF>
         <versaoDados>2.00</versaoDados>
      </nfeCabecMsg>
   </soap:Header>
   <soap:Body>
      <nfeStatusServicoNF2Result xmlns="http://www.portalfiscal.inf.br/nfe/wsdl/NfeStatusServico2">
         <retConsStatServ versao="2.00" xmlns="http://www.portalfiscal.inf.br/nfe">
            <tpAmb>2</tpAmb>
            <verAplic>13_0_11</verAplic>
            <cStat>107</cStat>
            <xMotivo>Servico em operacao</xMotivo>
            <cUF>31</cUF>
            <dhRecbto>2013-05-16T14:34:34</dhRecbto>
            <tMed>1</tMed>
         </retConsStatServ>
      </nfeStatusServicoNF2Result>
   </soap:Body>
</soap:Envelope>

See ya

donatinho

1 Resposta

M

Amigo, não sei se ainda se lembra :smiley:

Mas creio que não estou conseguindo inserir o header na requisição e estou recebendo

Caused by: org.apache.cxf.transport.http.HTTPException: HTTP response ‘400: Bad Request’ when communicating with http …

Você terial alguma informação que pudesse ajudar? Tipo… codigo onde faz a requisição, etc.

Criado 16 de maio de 2013
Ultima resposta 13 de nov. de 2014
Respostas 1
Participantes 2