zhongming_software 2013-04-20 00:56
浏览 296
已采纳

spring mvc 使用XStream返回xml下划线问题

小弟今天用Spring mvc XStream 返回xml数据,发现给返回的pojo对象修改别名(改成有别名中有"_")的问题:

返回的POJO对象:
[img]http://dl.iteye.com/upload/attachment/0083/4207/76d5f0df-7ab7-3303-921b-d2ce21995052.jpg[/img]

返回到前端的XML数据:
[img]http://dl.iteye.com/upload/attachment/0083/4209/68166889-a55d-3a36-8c4f-1b0b3719612f.jpg[/img]

按道理graphicId应该是以grapic_id作为名称,但是实际返回的是grapic__id两个下划线,这个问题如何解决,大家帮忙。感谢

  • 写回答

2条回答 默认 最新

  • jinnianshilongnian 2013-04-20 08:39
    关注

    Why do field names suddenly have double underscores in the generated XML?

    XStream maps Java class names and field names to XML tags or attributes. Unfortunately this mapping cannot be 1:1, since some characters used for identifiers in Java are invalid in XML names. Therefore XStream uses an XmlFriendlyNameCoder to replace these characters with a replacement. By default this NameCoder uses an underscore as escape character and has therefore to escape the underscore itself also. You may provide a different configured instance of the XmlFriendlyNameCoder or a complete different implementation like the NoNameCoder to prevent name coding at all. However it is your responsibility then to ensure, that the resulting names are valid for XML.

    [url]http://xstream.codehaus.org/faq.html[/url]

    1、1.4及以后









    2、1.4之前

    <bean id="marshaller" class="org.springframework.oxm.xstream.AnnotationXStreamMarshaller">
        <property name="streamDriver">
            <bean class="com.thoughtworks.xstream.io.xml.XppDriver">
                <constructor-arg>
                    <bean class="com.thoughtworks.xstream.io.xml.XmlFriendlyReplacer">
                        <constructor-arg index="0" value="_-"/>
                        <constructor-arg index="1" value="_"/>
                    </bean>
                </constructor-arg>
            </bean>
        </property>
    </bean>
    

    3、注册到spring mvc
    mvc:annotation-driven
    mvc:message-converters




    /mvc:message-converters
    /mvc:annotation-driven

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!