JAXB。自定义字段命名行为(camelCase为underscore_case) [英] Jaxb. Customize field naming behavior(camelCase to underscore_case)
问题描述
我想用强调名称序列化我的字段。例如: userName
- > user_name
。
I want to serialize my fields with underscored names. For example: userName
-> user_name
.
我知道可以使用 @XmlElement(name =user_name)
注释完成,但它是对我来说不是很方便。
I know that it can be done with the @XmlElement(name = "user_name")
annotation, but it's not very handy for my case.
有没有办法为JAXB设置默认命名策略?
Is there any way to set up a default naming policy for JAXB?
推荐答案
注意:我是EclipseLink JAXB(MOXy)的负责人,也是JAXB(JSR-222)专家组的成员。
Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.
MOXy的 XMLNameTransformer
扩展,使您可以覆盖元素,属性和类型的默认命名策略。
MOXy has an XMLNameTransformer
extension that enables you to override the default naming policy for elements, attributes, and types.
- http://blog.bdoughan.com/2011/05/overriding-jaxbs-name-mangling.html
如果您是从XML架构生成模型,则可以使用保留下划线的外部绑定文件。
If you generated your model from an XML schema you can use an external binding file to keep the underscores.
<?xml version="1.0" encoding="UTF-8"?>
<jxb:bindings
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
version="2.1">
<jxb:globalBindings underscoreBinding="asCharInWord"/>
</jxb:bindings>
这篇关于JAXB。自定义字段命名行为(camelCase为underscore_case)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!