收藏
回答

事件推送群发结果,获取后转换为xml文件时报错,

事件推送群发结果,获取HttpServletRequest转换为xml文件时,将HttpServletRequest形式为xml文件的字符串时String requestXml = MessageUtil.parse2Xml(request);),获取的字符串为:

<xml><ToUserName><![CDATA[gh_4cb8fca54b13]]></ToUserName><FromUserName><![CDATA[oV_Tssw9sHOSXx2leMYE1ecHzSKk]]></FromUserName><CreateTime>1571039189</CreateTime><MsgType><![CDATA[event]]></MsgType><Event><![CDATA[MASSSENDJOBFINISH]]></Event><MsgID>3147483669</MsgID><Status><![CDATA[send success]]></Status><TotalCount>2</TotalCount><FilterCount>2</FilterCount><SentCount>2</SentCount><ErrorCount>0</ErrorCount>
<CopyrightCheckResult>
    <Count>0</Count>
    <ResultList>
    </ResultList>
    <CheckState>1</CheckState>
</CopyrightCheckResult>
<ArticleUrlResult>
    <Count>1</Count>
    <ResultList>
        <item>
            <ArticleIdx>1</ArticleIdx>
            <ArticleUrl><![CDATA[http://mp.weixin.qq.com/s/RrUoc2JYYfgVpVhF0H1qXQ]]></ArticleUrl>
        </item>
    </ResultList>
</ArticleUrlResult>
</xml>

发现<ArticleUrl> -> <ResultList> -> <item>的item项为小写开头的i,所以在之后将形式为xml的字符串解析为相应对象时出错,

ReqBaseMessage reqBaseMessage = parseWechatMessage(requestXml);
 
reqBaseMessage = convertResult(requestXml, ReqEventMessage.class, ReqBaseMessage.class);
 
@SuppressWarnings("unchecked")
    private <T, V> T convertResult(String result, Class<T> clazz, Class<V> parentClazz) {
        XStream xStream = new XStream();
        xStream.addDefaultImplementation(clazz, parentClazz);
        xStream.autodetectAnnotations(true);
        xStream.processAnnotations(clazz);
        Object transaction = xStream.fromXML(result);
        return (T)transaction;
    }
    

对应的类为:


错误日志如下:


com.thoughtworks.xstream.converters.ConversionException: item : item

---- Debugging information ----

message             : item

cause-exception     : com.thoughtworks.xstream.mapper.CannotResolveClassException

cause-message       : item

class               : java.util.ArrayList

required-type       : java.util.ArrayList

converter-type      : com.thoughtworks.xstream.converters.collections.CollectionConverter

path                : /xml/ArticleUrlResult/ResultList/item

line number         : 1

class[1]            : com.monster.hswx.utils.pojo.req.ArticleUrlResult

converter-type[1]   : com.thoughtworks.xstream.converters.reflection.ReflectionConverter

class[2]            : com.monster.hswx.utils.pojo.req.ReqEventMessage

version             : null

-------------------------------

at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:79)

at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)

at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)

at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:351)

at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:302)

at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:230)

at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)

at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)

at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)

at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:351)

at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:302)

at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:230)

at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)

at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)

at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)

at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50)

at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:134)

at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:32)

at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1035)

at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1019)

at com.thoughtworks.xstream.XStream.fromXML(XStream.java:895)

at com.thoughtworks.xstream.XStream.fromXML(XStream.java:886)

at com.monster.hswx.biz.wechat.impl.WeChatCoreServiceManagerImpl.convertResult(WeChatCoreServiceManagerImpl.java:373)

at com.monster.hswx.biz.wechat.impl.WeChatCoreServiceManagerImpl.parseWechatMessage(WeChatCoreServiceManagerImpl.java:273)

at com.monster.hswx.biz.wechat.impl.WeChatCoreServiceManagerImpl.handleMessage(WeChatCoreServiceManagerImpl.java:100)

at com.monster.hswx.webapp.controller.CheckController.index1(CheckController.java:89)

at sun.reflect.GeneratedMethodAccessor146.invoke(Unknown Source)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:606)

at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)

at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)

at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)

at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)

at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)

at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)

at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:754)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)

at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:70)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)

at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:840)

at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:622)

at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:560)

at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:488)

at org.tuckey.web.filters.urlrewrite.NormalRewrittenUrl.doRewrite(NormalRewrittenUrl.java:195)

at org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:159)

at org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:141)

at org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:90)

at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:417)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)

at com.opensymphony.sitemesh.webapp.SiteMeshFilter.obtainContent(SiteMeshFilter.java:129)

at com.opensymphony.sitemesh.webapp.SiteMeshFilter.doFilter(SiteMeshFilter.java:77)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)

at com.monster.hswx.webapp.common.filter.CheckFilter.doFilterInternal(CheckFilter.java:69)

at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)

at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)

at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)

at com.yunrich.monster.web.common.session.support.filter.YunRichSessionRequestFilter.doFilterInternal(YunRichSessionRequestFilter.java:40)

at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)

at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)

at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)

at com.monster.hswx.webapp.common.filter.ByPassOtherFilter.doFilterInternal(ByPassOtherFilter.java:62)

at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)

at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)

at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)

at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275)

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)

at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153)

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155)

at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)

at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368)

at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877)

at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671)

at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930)

at java.lang.Thread.run(Thread.java:748)

Caused by: com.thoughtworks.xstream.mapper.CannotResolveClassException: item

at com.thoughtworks.xstream.mapper.DefaultMapper.realClass(DefaultMapper.java:56)

at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)

at com.thoughtworks.xstream.mapper.DynamicProxyMapper.realClass(DynamicProxyMapper.java:55)

at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)

at com.thoughtworks.xstream.mapper.PackageAliasingMapper.realClass(PackageAliasingMapper.java:88)

at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)

at com.thoughtworks.xstream.mapper.ClassAliasingMapper.realClass(ClassAliasingMapper.java:79)

at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)

at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)

at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)

at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)

at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)

at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)

at com.thoughtworks.xstream.mapper.ArrayMapper.realClass(ArrayMapper.java:74)

at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)

at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)

at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)

at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)

at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)

at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)

at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)

at com.thoughtworks.xstream.mapper.CachingMapper.realClass(CachingMapper.java:45)

at com.thoughtworks.xstream.core.util.HierarchicalStreams.readClassType(HierarchicalStreams.java:29)

at com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter.readItem(AbstractCollectionConverter.java:70)

at com.thoughtworks.xstream.converters.collections.CollectionConverter.addCurrentElementToCollection(CollectionConverter.java:79)

at com.thoughtworks.xstream.converters.collections.CollectionConverter.populateCollection(CollectionConverter.java:72)

at com.thoughtworks.xstream.converters.collections.CollectionConverter.populateCollection(CollectionConverter.java:66)

at com.thoughtworks.xstream.converters.collections.CollectionConverter.unmarshal(CollectionConverter.java:61)

at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)

... 88 more

请各位大佬解疑,先谢谢啦



最后一次编辑于  2019-10-16
回答关注问题邀请回答
收藏

1 个回答

  • 笨小孩
    笨小孩
    2019-10-16

    经过结合上网查找,发现只需将对应的Item类前加上对应@XStreamAlias("item")注解就行了,谢谢啦。


    2019-10-16
    有用
    回复
登录 后发表内容
问题标签