我尝试在 Java 类上映射一个 opennms rest api;我使用正确配置的 Java 客户端,但 jaxb 解组失败并出现错误:
[org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver] (134) - 解决来自处理程序的异常...
这是 Java 客户端和 jaxb 代码:
@Override
public List<User> getUserList() {
Client client = null;
try {
client = buildClient();
final WebResource webResource = client.resource(opennmsUrl + "users");
final ClientResponse response = webResource.get(ClientResponse.class);
if (response.getStatus() != 200) {
throw new IllegalStateException("Request to remote opennms server failed with error " + response.getStatus() + " : " + response.getStatusInfo().toString());
} else {
LOGGER.info("Response: result ({}), reason [{}]", response.getStatus(), response.getStatusInfo());
LOGGER.info("Response: body [{}]", response.getEntity(String.class));
final JAXBContext jaxbContext = JAXBContext.newInstance(opennmsUsersSchema);
final Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
final JAXBElement<Users> element = (JAXBElement<Users>) jaxbUnmarshaller.unmarshal(response.getEntityInputStream());
final Users usrList = (Users) element.getValue();
return usrList.getUser();
/*User admin = new User();
admin.setUserId("admin");
admin.setFullName("Administrator");
admin.setUserComments("Default administrator, do not delete");
admin.setPassword("!!opennms2015");
admin.setTuiPin("pin");
admin.setReadOnly(Boolean.FALSE);
List<User> users = new ArrayList<>();
users.add(admin);
return users;*/
}
} catch (IllegalStateException e) {
throw e;
} catch (Exception e) {
throw new IllegalStateException("Exception on Request", e);
} finally {
if (client != null) {
client.destroy();
}
}
}
编辑:响应正文 xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<users count="1" totalCount="1">
<user>
<user-id>admin</user-id>
<full-name>Administrator</full-name>
<user-comments>Default administrator</user-comments>
<email></email>
<password>123456</password>
<passwordSalt>true</passwordSalt>
</user>
</users>
编辑:评论中提交的信息
opennmsUsersSchema
包是否包含由 maven jaxb 插件从 xsd 模式自动生成的 java 类
xsd 架构在这里:xmlns.opennms.org/xsd/users
这是Users
java类:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = { "user" })
@XmlRootElement(name = "users")
public class Users {
@XmlElement(required = true)
protected List<User> user;
public List<User> getUser() { if (user == null) { user = new ArrayList<User>(); } return this.user; }
}
这是User
java类:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"userId",
"fullName",
"userComments",
"password",
"contact",
"dutySchedule",
"tuiPin"
})
@XmlRootElement(name = "user")
public class User {
@XmlElement(name = "user-id", required = true)
protected String userId;
@XmlElement(name = "full-name")
protected String fullName;
@XmlElement(name = "user-comments")
protected String userComments;
@XmlElement(required = true)
protected String password;
protected List<Contact> contact;
@XmlElement(name = "duty-schedule")
protected List<String> dutySchedule;
@XmlElement(name = "tui-pin")
protected String tuiPin;
@XmlAttribute(name = "read-only")
protected Boolean readOnly;
public String getUserId() {
return userId;
}
public void setUserId(String value) {
this.userId = value;
}
public String getFullName() {
return fullName;
}
public void setFullName(String value) {
this.fullName = value;
}
public String getUserComments() {
return userComments;
}
public void setUserComments(String value) {
this.userComments = value;
}
public String getPassword() {
return password;
}
public void setPassword(String value) {
this.password = value;
}
public List<Contact> getContact() {
if (contact == null) {
contact = new ArrayList<Contact>();
}
return this.contact;
}
public List<String> getDutySchedule() {
if (dutySchedule == null) {
dutySchedule = new ArrayList<String>();
}
return this.dutySchedule;
}
public String getTuiPin() {
return tuiPin;
}
public void setTuiPin(String value) {
this.tuiPin = value;
}
public boolean isReadOnly() {
if (readOnly == null) {
return false;
} else {
return readOnly;
}
}
public void setReadOnly(Boolean value) {
this.readOnly = value;
}
}