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> 
相关推荐
找了一圈尾巴28 分钟前
Wend看源码-Java-Collections 工具集学习
java·开发语言·学习
Ai 编码助手3 小时前
Go 语言 API 限流实战:保障系统稳定性的护盾
开发语言·后端·golang
广而不精zhu小白3 小时前
CentOS Stream 9 安装 JDK
java·linux·centos
程序员云帆哥3 小时前
【玩转23种Java设计模式】行为型模式篇:命令模式
java·设计模式·命令模式
玩大数据的龙威3 小时前
【ArcGIS Pro】完整的nc文件整理表格模型构建流程及工具练习数据分享
开发语言·python
赵谨言4 小时前
基于 Java 大数据的旅游推荐系统的设计与实现
java·经验分享·毕业设计
NHuan^_^4 小时前
RabbitMQ基础篇之Java客户端 Topic交换机
java·rabbitmq·java-rabbitmq
唐棣棣5 小时前
期末速成C++【知识点汇总完】
开发语言·c++
yannan201903135 小时前
【数据结构】(Python)差分数组。差分数组与树状数组结合
开发语言·python·算法
中國移动丶移不动5 小时前
Java List 源码解析——从基础到深度剖析
java·后端·list