Jaxb. Customize field naming behavior(camelCase to underscore_case)
java, jaxb, xml, xml-serialization
Solution
Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.
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
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>
Problem
I want to serialize my fields with underscored names. For example: `userName` -> `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. Is there any way to set up a default naming policy for JAXB?