gpt4 book ai didi

java - Jaxb 解码 SOAP 信封

转载 作者:行者123 更新时间:2023-12-01 16:31:13 26 4
gpt4 key购买 nike

我知道已经有类似的帖子,但没有一个对我有帮助。

我在反序列化/解码 xml 时遇到问题。

我的代码如下所示:

public class SoapTest {

public static String passarXMLParaString(Document xml, int espacosIdentacao){
try {
//set up a transformer
TransformerFactory transfac = TransformerFactory.newInstance();
transfac.setAttribute("indent-number", new Integer(espacosIdentacao));
Transformer trans = transfac.newTransformer();
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
trans.setOutputProperty(OutputKeys.INDENT, "yes");

//create string from xml tree
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource(xml);
trans.transform(source, result);
String xmlString = sw.toString();
return xmlString;
}
catch (TransformerException e) {
e.printStackTrace();
System.exit(0);
}
return null;
}


public static void main(String[] args) throws SOAPException, IOException, JAXBException {

// consumes
String requestSoap;
requestSoap = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:cli=\"http://cliente.bean.master.sigep.bsb.correios.com.br/\">\r\n" +
" <soapenv:Header/>\r\n" +
" <soapenv:Body>\r\n" +
" <cli:consultaCEP>\r\n" +
" <cep>38706400</cep>\r\n" +
" </cli:consultaCEP>\r\n" +
" </soapenv:Body>\r\n" +
"</soapenv:Envelope>";

SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
String url = "https://apps.correios.com.br/SigepMasterJPA/AtendeClienteService/AtendeCliente";
MimeHeaders headers = new MimeHeaders();
headers.addHeader("Content-Type", "text/xml");

MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage msg = messageFactory.createMessage(headers, (new ByteArrayInputStream(requestSoap.getBytes())));
SOAPMessage soapResponse = soapConnection.call(msg, url);
Document xmlRespostaARequisicao = soapResponse.getSOAPBody().getOwnerDocument();
String xml = passarXMLParaString(xmlRespostaARequisicao, 0);
System.out.println(passarXMLParaString(xmlRespostaARequisicao, 0));

// returns null
JAXBContext jc = JAXBContext.newInstance(CepResponse.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
CepResponse response = (CepResponse) unmarshaller.unmarshal(soapResponse.getSOAPBody().extractContentAsDocument().getDocumentElement());
System.out.println(response.bairro);
}

}

此消耗的返回为:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:consultaCEPResponse xmlns:ns2="http://cliente.bean.master.sigep.bsb.correios.com.br/">
<return>
<bairro>Cidade Nova</bairro>
<cep>38706400</cep>
<cidade>Patos de Minas</cidade>
<complemento2>- até 729/730</complemento2>
<end>Avenida Presidente Tancredo Neves</end>
<uf>MG</uf>
</return>
</ns2:consultaCEPResponse>
</soap:Body>
</soap:Envelope>

但是执行 urnmarshal 会返回 null。

我的模型类:

@XmlRootElement(name = "consultaCEPResponse", namespace = "http://cliente.bean.master.sigep.bsb.correios.com.br/")
@XmlAccessorType(XmlAccessType.FIELD)
public class CepResponse {

@XmlElement(name="bairro")
String bairro;

}

我尝试过其他类型的解码,但没有成功。你能帮我吗?

谢谢。

最佳答案

问题是 bairro 的 xpath是 /consultaCEPResponse/return/bairro 。 JAXB 字段 bairro应该嵌套在代表 <return> 的另一个类中标签。

@XmlRootElement(name = "consultaCEPResponse", namespace = "http://cliente.bean.master.sigep.bsb.correios.com.br/")
@XmlAccessorType(XmlAccessType.FIELD)
public static class CepResponse {

@XmlElement(name = "return")
Return returnTag;

@XmlAccessorType(XmlAccessType.FIELD)
public static class Return {
@XmlElement(name="bairro")
String bairro;
}
}

bairro可通过 response.returnTag.bairro 访问

关于java - Jaxb 解码 SOAP 信封,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62034937/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com