I came across a very annoying error when trying to marshal the JSON class using Eclipse Moxy.
I have an attribute with the following value in one of my domain classes: "the City's original city site" , which contains the code point u + 2019 ()
When Jaxb tries to marshal this value, I inexplicably return a weird control: "Citys original city site"
This results in invalid JSON, which returns a null value when decoding. I tried this with Jackson and got the escape ascii character, which is still wrong, but it at least makes JSON valid!
Moxy should be able to output this correctly as a valid Unicode character and valid in JSON. Is there anything I can do to output (and any other Unicode character) correctly and preferably convert this unnecessary character to a regular apostrophe.
Here is my provider class:
@Provider @Component("customMOXyJsonProvider") public class CustomMOXyJsonProvider extends MOXyJsonProvider { @Override protected void preWriteTo(Object object, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, Marshaller marshaller) throws JAXBException { marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, true); marshaller.setProperty(Marshaller.JAXB_ENCODING,"UTF-8"); } }
I am using version 2.5.1 from Moxy.
<dependency> <groupId>org.eclipse.persistence</groupId> <artifactId>org.eclipse.persistence.moxy</artifactId> <version>2.5.1</version> </dependency>
I have several components on my system that could theoretically mess up the value (postgres, jdbc, hibernate, cxf and tomcat), but I decided through testing that the value is correctly stored in my domain class and then corrupted like Elliot Spitzer visited a prostitute at the marshaling stage.