Во время демаршалинга ответа Soap getSOAPBody возвращает ноль.Я пытался использовать XMLStreamReader, который работает в этом случае.Но я хочу знать способ сделать это через ByteArrayInputStream.Что бы исправить это правильно.Я прилагаю ниже файл POJOmand package-info.java.
Также, если я сделаю "xsi: type =" xsd: string "для" xsi: type = "string", это, похоже, будет работать.Как я должен преобразовать свой класс bean-компонентов таким образом, чтобы он работал?
package-info.java
@XmlSchema(
namespace = "http://tempuri.org/",
xmlns = {
@javax.xml.bind.annotation.XmlNs(prefix = "soap", namespaceURI = "http://schemas.xmlsoap.org/soap/envelope/"),
@javax.xml.bind.annotation.XmlNs(prefix = "xsi", namespaceURI = "http://www.w3.org/2001/XMLSchema-instance/"),
@javax.xml.bind.annotation.XmlNs(prefix = "xsd", namespaceURI = "http://www.w3.org/2001/XMLSchema")
})
package org.tempuri;
import javax.xml.bind.annotation.XmlSchema;
TestJAX.java
package org.test;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.stream.XMLStreamException;
import org.tempuri.WIResponse;
/**
* Test Class
*
* @author vijay.prakash
*
*/
public class TestJAX {
public static void main(String[] args) throws XMLStreamException, JAXBException, IOException, SOAPException {
// xmlReadingWay
/*
* XMLInputFactory xif = XMLInputFactory.newFactory(); XMLStreamReader xsr =
* xif.createXMLStreamReader(new FileReader("D:\\BACKUPWARS\\some.xml"));
*
* xsr.nextTag(); // Advance to Envelope tag xsr.nextTag(); // Advance to Body
* tag xsr.nextTag(); // Advance to getNumberResponconse tag
*/
// byteway
String object = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n"
+ "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"\r\n"
+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\r\n"
+ " <soap:Body>\r\n" + " <WIResponse xmlns=\"http://tempuri.org/\">\r\n"
+ " <WIResult xsi:type=\"xsd:string\">20702458ghhc</WIResult>\r\n"
+ " </WIResponse>\r\n" + " </soap:Body>\r\n" + "</soap:Envelope>";
String str = new String(object.getBytes(), "utf-8");
final MessageFactory messFac = MessageFactory.newInstance();
SOAPMessage message = messFac.createMessage(null, new ByteArrayInputStream(str.getBytes()));
message.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "utf-8");
System.out.println("message: " + message.getSOAPBody());
JAXBContext jaxbContext = JAXBContext.newInstance(WIResponse.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
System.out.println("message.getSOAPBody(): " + message.getSOAPBody());
Object je = unmarshaller.unmarshal(message.getSOAPBody().extractContentAsDocument());
WIResponse wi = (WIResponse) je;
/*
* //JAXBElement<WIResponse> je =
* unmarshaller.unmarshal(message.getSOAPBody().extractContentAsDocument(),
* WIResponse.class);
*/
System.out.println(wi.getWIResult());
}
}
мыло xml
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<WIResponse xmlns="http://tempuri.org/">
<WIResult xsi:type="xsd:string">20702458ghhc</WIResult>
</WIResponse>
</soap:Body>
</soap:Envelope>
WIResponse.java
package org.tempuri;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "WIResponse", namespace = "http://tempuri.org/")
public class WIResponse {
@XmlElement(name = "WIResult", required = true, type = java.lang.String.class)
String wIResult;
public String getWIResult() {
return wIResult;
}
public void setWIResult(String wIResult) {
this.wIResult = wIResult;
}
}