我有一个客户的答复是 ByteArrayInputStream
我正在缓冲读取并创建一个stringreader来读取pojo表单中的xml。我想解开它,但我得到的名称空间问题,因此jaxbexception。
以下是我得到回复后的代码:
public void post() {
...
ByteArrayInputStream obj = (ByteArrayInputStream) response.getEntity();
BufferedReader br = new BufferedReader(new InputStreamReader(obj));
String line = null;
StringBuffer sb = new StringBuffer();
try{
while ((line = br.readLine()) != null){
System.out.println(line);
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
JAXBContext jaxbContext = JAXBContext.newInstance(FileTransmissionResponse.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
StringReader reader = new StringReader(sb.toString());
FileTransmissionResponse transmissionResponse = (FileTransmissionResponse)
unmarshaller.unmarshal(reader);
}
这个 StringBuffer
响应是以下字符串格式的xml:
<?xml version="1.0" encoding="UTF-8"?>
<fileTransmissionResponse xmlns="http://abc.xyz.com/schema/auditing/fileResponse-1.1">
<version>1.0</version>
<userId>xqas</userId>
<reNumber>1234</reNumber>
<reName>1234rfd</reName>
<status>FAILURE</status>
<fileName>file.xml</fileName>
<startTime>2021-04-06T09:16:54.065+10:00</startTime>
<finishTime>2021-04-06T09:16:54.743+10:00</finishTime>
<bytesTransferred>15530</bytesTransferred>
<emailSent>scd@xyz.com</emailSent>
....
</fileTransmissionResponse>
波乔班
package com.example.response;
import javax.xml.bind.annotation.*;
import java.util.List;
@XmlRootElement(name = "fileTransmissionResponse")
@XmlType(propOrder = { "version", "userId", "reNumber", "reName", "status", "fileName", "startTime", "finishTime", "bytesTransferred", "totalRead", "totalTransferred", "totalIntercepted", "emailSent", "message"})
@XmlAccessorType(XmlAccessType.PROPERTY)
public class FileTransmissionResponse {
private String version;
private String userId;
private List<String> reNumber;
private List<String> reName;
private int status;
private List<String> fileName;
private String startTime;
private String finishTime;
private List<Integer> bytesTransferred;
private List<Integer> totalRead;
private List<Integer> totalTransferred;
private List<Integer> totalIntercepted;
private List<String> austracReference;
private List<String> emailSent;
private List<ErrorMessage> message;
public String getVersion() {
return version;
}
@XmlElement(name = "version")
public void setVersion(final String version) {
this.version = version;
}
public String getUserId() {
return userId;
}
@XmlElement(name = "userId")
public void setUserId(final String userId) {
this.userId = userId;
}
public List<String> getReNumber() {
return reNumber;
}
@XmlElement(name = "reNumber")
public void setReNumber(final List<String> reNumber) {
this.reNumber = reNumber;
}
public List<String> getReName() {
return reName;
}
@XmlElement(name = "reName")
public void setReName(final List<String> reName) {
this.reName = reName;
}
public int getStatus() {
return status;
}
@XmlElement(name = "status")
public void setStatus(final int status) {
this.status = status;
}
public List<String> getFileName() {
return fileName;
}
@XmlElement(name = "fileName")
public void setFileName(final List<String> fileName) {
this.fileName = fileName;
}
public String getStartTime() {
return startTime;
}
@XmlElement(name = "startTime")
public void setStartTime(final String startTime) {
this.startTime = startTime;
}
public String getFinishTime() {
return finishTime;
}
@XmlElement(name = "finishTime")
public void setFinishTime(final String finishTime) {
this.finishTime = finishTime;
}
public List<Integer> getBytesTransferred() {
return bytesTransferred;
}
@XmlElement(name = "bytesTransferred")
public void setBytesTransferred(final List<Integer> bytesTransferred) {
this.bytesTransferred = bytesTransferred;
}
public List<Integer> getTotalRead() {
return totalRead;
}
@XmlElement(name = "totalRead")
public void setTotalRead(final List<Integer> totalRead) {
this.totalRead = totalRead;
}
public List<Integer> getTotalTransferred() {
return totalTransferred;
}
@XmlElement(name = "totalTransferred")
public void setTotalTransferred(final List<Integer> totalTransferred) {
this.totalTransferred = totalTransferred;
}
public List<Integer> getTotalIntercepted() {
return totalIntercepted;
}
@XmlElement(name = "totalIntercepted")
public void setTotalIntercepted(final List<Integer> totalIntercepted) {
this.totalIntercepted = totalIntercepted;
}
public List<String> getEmailSent() {
return emailSent;
}
@XmlElement(name = "emailSent")
public void setEmailSent(final List<String> emailSent) {
this.emailSent = emailSent;
}
public List<ErrorMessage> getMessage() {
return message;
}
@XmlElement(name = "message")
public void setMessage(final List<ErrorMessage> message) {
this.message = message;
}
}
在unmarshal调用之后,由于xmlns命名空间,我得到了一个jaxb异常。有什么方法可以避免这种情况?
1条答案
按热度按时间disho6za1#
你应该加上
package-info.java
文件到您的包FileTransmissionResponse.java
类并定义xml架构。