javacharacter-encodingjaxbmarshallingjakarta-migration

Java migration JDK 11 to JDK 17 fail Junit Test with Jakarta Marshal


I have an application on JDK11 with Spring Boot 2.7.11 and a Junit Test that use jaxb javax.xml.bind.Marshaller. All is OK

But I have to migrate to JDK17 with Spring Boot 2.7.18. So I have replace all my import of javax.xml.bind by jakarta.xml.bind.

After that, my Junit Test is failed becaus marshal produce XML object with wrong encoding char

 try {
      final JAXBContext jaxbContext = getJaxbContext(classes);
      marshaller = jaxbContext.createMarshaller();
      marshaller.marshal(object, bos);
    } catch (final JAXBException ex) {
      throw new HttpMessageConversionException(
          "Could not create Marshaller for class [" + classes + "]: " + ex.getMessage(), ex);
    }

On marshaller.marshal(object, bos);, object contain all correct charecter but after marshaller, charter ar not ok

Before Marshal : lorsDe=Retour à la maison

After Marsha in XML : <LORS_DE>Retour à la maison</LORS_DE>

I don't understand where is the problem. Can you help me ?


Solution

  • The issue is with JDK17.

    The default file encoding was UTF8 before JDK17, now it’s not UTF8 anymore.

    You have to set it explicitly to UTF8 with:

    ‘java -jar -Dfile.encoding=UTF-8 myapp.jar”

    You have to set this in your Dockerfile and also for your unit tests via the Maven surefire plugin.