xml解析----元素型別 "XXXX" 必須由匹配的結束標記 "" 終止。
阿新 • • 發佈:2019-01-25
概述:
利用eclipse link 中moxy在弄微信的向微信寫入資料。報錯了。。。。
[Exception [EclipseLink-25004] (Eclipse Persistence Services - 2.7.0.v20170701-882318f): org.eclipse.persistence.exceptions.XMLMarshalException Exception Description: An error occurred unmarshalling the document Internal Exception: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[8,500] Message: 元素型別 "Content" 必須由匹配的結束標記 "</Content>" 終止。] at org.eclipse.persistence.jaxb.JAXBUnmarshaller.handleXMLMarshalException(JAXBUnmarshaller.java:1110) at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:638) at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:214) at com.rinlink.intelligent.weixin.service.impl.WeiXinMsgServiceImpl.textHandle(WeiXinMsgServiceImpl.java:126) at com.rinlink.intelligent.weixin.controller.WeiXinEventDispatchController.event(WeiXinEventDispatchController.java:87) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
問題就是如果我輸入2的整數倍的字不會報錯,一臉懵逼。。。。。。
最後發現,微信需要編碼為gbk,而我設定為utf-8。
@Override public void textHandle(String data, HttpServletResponse response) throws JAXBException, IOException { JAXBContext unmarshJc = JAXBContext.newInstance(MsgTextReqDTO.class); JAXBContext marshJc = JAXBContext.newInstance(MsgTextRespDTO.class); Unmarshaller unmarshaller = unmarshJc.createUnmarshaller(); MsgTextReqDTO msgTextReqDTO = (MsgTextReqDTO) unmarshaller.unmarshal(new StringReader(data)); logger.info("Unmarshal: " +msgTextReqDTO.toString()); Marshaller marshaller = marshJc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true); marshaller.setProperty(Marshaller.JAXB_ENCODING, "utf-8"); MsgTextRespDTO msgRespDTO = new MsgTextRespDTO(); //======================硬編碼================ msgRespDTO.setContent(msgTextReqDTO.getContent()); msgRespDTO.setCreateTime(WeiXinUtil.createTimestamp()); msgRespDTO.setFromUserName(msgTextReqDTO.getToUserName()); msgRespDTO.setMsgType(msgTextReqDTO.getMsgType()); msgRespDTO.setToUserName(msgTextReqDTO.getFromUserName()); //marshaller.marshal(msgRespDTO, response.getOutputStream()); marshaller.marshal(msgRespDTO, response.getOutputStream()); }
gbk和utf-8編碼方式不同的,具體細節比較難,打比喻就是中國用人民幣,美國用美元。它都可以衡量價值。只是媒介不一樣。有興趣可以去研究一下編碼方式。