Axis2客戶端呼叫服務端報org.xml.sax.SAXException
阿新 • • 發佈:2019-02-09
org.xml.sax.SAXException: SimpleDeserializer encountered a child element, which is NOT expected, in something it was trying to deserialize.
報錯原因:
服務端:public UserInfo getUserInfo(String token){
//業務處理
return userInfo;
}
這個方法返回的是一個物件,
客戶端:String result = (String) call.invoke(object);接收的是一個字串。
解決辦法:(1)UserInfo implements Serializable 序列化
(2)getUserInfo(String token)這個方法,返回的時候,將物件轉為json或者xml
轉為json:
String json = com.alibaba.fastjson.JSON.toJSON(userInfo).toString();
return json;
或者
JSONObject json = JSONObject.fromObject(obj);//將java物件轉換為json物件 String str = json.toString();//將json物件轉換為字串
接收端可這樣處理:
JSONObject jsonObject=JSONObject.fromObject(objectStr);
UserInfo=(UserInfo)JSONObject.toBean(jsonObject, UserInfo.class);
轉成XML形式:
import java.io.ByteArrayOutputStream; import java.io.StringReader; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; //直接展示的方法 public String getUserInfo(String xml){ String xmlObj = ""; try { //將獲取的xml形式的引數轉為java物件 JAXBContext context = JAXBContext.newInstance(UserInfo.class);//獲取上下文物件 Unmarshaller marchaller = context.createUnmarshaller(); UserInfo userInfo = (UserInfo) marchaller.unmarshal(new StringReader(xml)); String token = userInfo.getToken(); if (null != token && !"".equals(token)) { //如果token不為空,從快取中獲取username String username = RedisCache.get(token); if(null!=username && !"".equals(username)){ //根據賬戶查詢userid User user = userService.queryUserByLoginName(username); userInfo.setUserId(user.getId()); // userInfo.setToken(token); userInfo.setUserName(username); //設定當前登入時間 userInfo.setLoginTime(System.currentTimeMillis()); //token有效期 userInfo.setExpireDate(2); userInfo.setSuccess("ok"); } } //將物件轉成xml的形式 Marshaller marshaller = context.createMarshaller(); // 根據上下文獲取marshaller物件 marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");// 設定編碼字符集 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);// 格式化XML輸出,有分行和縮排 ByteArrayOutputStream baos = new ByteArrayOutputStream(); marshaller.marshal(userInfo, baos); xmlObj = new String(baos.toByteArray()); // 生成XML字串 } catch (JAXBException e1) { e1.printStackTrace(); } return xmlObj; // String json = JSON.toJSON(userInfo).toString(); // return json; }
userInfo.java需要加上標籤:
import java.io.Serializable;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/**
* 返回的物件
* @author
*
*/
@XmlRootElement(name="userInfo")
public class UserInfo implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private Long userId;
private String userName;
//認證憑證
private String token;
//當前登入時間
private long loginTime;
//token過期時間 單位小時
private Integer expireDate;
//成功標誌
private String success;
public Long getUserId() {
return userId;
}
@XmlElement
public void setUserId(Long userId) {
this.userId= userId;
}
public String getUserName() {
return userName;
}
@XmlElement
public void setUserName(String userName) {
this.userName = userName;
}
public String getToken() {
return token;
}
@XmlElement
public void setToken(String token) {
this.token = token;
}
public long getLoginTime() {
return loginTime;
}
@XmlElement
public void setLoginTime(long loginTime) {
this.loginTime = loginTime;
}
public Integer getExpireDate() {
return expireDate;
}
@XmlElement
public void setExpireDate(Integer expireDate) {
this.expireDate = expireDate;
}
public String getSuccess() {
return success;
}
@XmlElement
public void setSuccess(String success) {
this.success = success;
}
}