Java:XStream 下划线

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.

1、1.4及以后
复制代码
<bean id="marshaller" class="org.springframework.oxm.xstream.AnnotationXStreamMarshaller">
    <property name="streamDriver">
        <bean class="com.thoughtworks.xstream.io.xml.StaxDriver">
            <constructor-arg>
                <bean class="com.thoughtworks.xstream.io.naming.NoNameCoder()">
                </bean>
            </constructor-arg>
        </bean>
    </property>
</bean>
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>
    <bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
        <property name="marshaller" ref="marshaller" />
        <property name="unmarshaller" ref="marshaller" />
    </bean>
  </mvc:message-converters>
</mvc:annotation-driven>

<!-- 根据客户端的不同的请求决定不同的view进行响应, 如 /blog/1.json /blog/1.xml -->
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
	<property name="ignoreAcceptHeader" value="true" />
	<property name="defaultContentType" value="application/json" />
	<!-- 扩展名至mimeType的映射,即 /blog.json => application/json -->
	<property name="mediaTypes">
		<map>
			<entry key="json" value="application/json" />
			<entry key="xml" value="application/xml" />
		</map>
	</property>
	<!-- 用于开启 /blog/123?format=json 的支持 -->
	<property name="favorParameter" value="false" />
	<property name="viewResolvers">
		<list>
			<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
			<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
				<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
				<property name="prefix" value="/pages" />
				<property name="suffix" value=".jsp"></property>
			</bean>
		</list>
	</property>
	<property name="defaultViews">
		<list>
			<bean  class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
				<property  name="extractValueFromSingleKeyModel" value="true" />
			</bean>
			<bean  class="org.springframework.web.servlet.view.xml.MarshallingView">
				<property name="marshaller">
					<bean  class="org.springframework.oxm.xstream.XStreamMarshaller">
						<property name="autodetectAnnotations" value="true"/>
						<!-- 针对bean中下划线处理 -->
						<property name="streamDriver">
							<bean class="com.thoughtworks.xstream.io.xml.StaxDriver">
								<constructor-arg>
									<bean class="com.thoughtworks.xstream.io.naming.NoNameCoder" />
								</constructor-arg>
							</bean>
						</property>
					</bean>
				</property>
			</bean>
		</list>
	</property>
</bean> 
相关推荐
武子康20 分钟前
Java-72 深入浅出 RPC Dubbo 上手 生产者模块详解
java·spring boot·分布式·后端·rpc·dubbo·nio
_殊途1 小时前
《Java HashMap底层原理全解析(源码+性能+面试)》
java·数据结构·算法
椰椰椰耶2 小时前
【Spring】拦截器详解
java·后端·spring
没有bug.的程序员3 小时前
JAVA面试宝典 - 《MyBatis 进阶:插件开发与二级缓存》
java·面试·mybatis
倔强青铜33 小时前
苦练Python第18天:Python异常处理锦囊
开发语言·python
u_topian4 小时前
【个人笔记】Qt使用的一些易错问题
开发语言·笔记·qt
没有羊的王K4 小时前
SSM框架学习——day1
java·学习
珊瑚里的鱼4 小时前
LeetCode 692题解 | 前K个高频单词
开发语言·c++·算法·leetcode·职场和发展·学习方法
又菜又爱coding4 小时前
安装Keycloak并启动服务(macOS)
java·keycloak
AI+程序员在路上4 小时前
QTextCodec的功能及其在Qt5及Qt6中的演变
开发语言·c++·qt