xml解析----元素类型 "XXXX" 必须由匹配的结束标记 "</XXXX>" 终止。

概述:

利用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编码方式不同的,具体细节比较难,打比喻就是中国用人民币,美国用美元。它都可以衡量价值。只是媒介不一样。有兴趣可以去研究一下编码方式。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值