0

I have a Spring Boot application, which uses MapStruct. I try to use a @ValueMapping to map String to an enum. My Strings contain non-english characters. This characters are wrong in the generated code.

POM

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <source>11</source>
        <target>11</target>
        <forceJavacCompilerUse>true</forceJavacCompilerUse>
        <annotationProcessorPaths>
            <path>
                <groupId>org.mapstruct</groupId>
                <artifactId>mapstruct-processor</artifactId>
                <version>1.5.2.Final</version>
            </path>
            <path>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.24</version>
            </path>
            <path>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok-mapstruct-binding</artifactId>
                <version>0.2.0</version>
            </path>
        </annotationProcessorPaths>
        <compilerArgs>
            <arg>
                -Amapstruct.defaultComponentModel=spring
            </arg>
        </compilerArgs>
    </configuration>
</plugin>

Code

@Mapper
public interface TestMapper {

  @ValueMapping(source = "Menschenwürde", target = "TEST_VALUE")
  TestEnum map(String value);
}

Generated Code

@Generated(
    value = "org.mapstruct.ap.MappingProcessor",
    date = "2022-09-02T18:22:58+0200",
    comments = "version: 1.5.2.Final, compiler: Eclipse JDT (IDE) 3.21.0.v20200304-1404, environment: Java 11.0.8 (Oracle Corporation)"
)
@Component
public class TestMapperImpl implements TestMapper {

    @Override
    public TestEnum map(String value) {
        if ( value == null ) {
            return null;
        }

        TestEnum testEnum;

        switch ( value ) {
            case "Menschenw�rde": testEnum = TestEnum.TEST_VALUE;
            break;
            case "TEST_VALUE": testEnum = TestEnum.TEST_VALUE;
            break;
            default: throw new IllegalArgumentException( "Unexpected enum constant: " + value );
        }    

        return testEnum;
    }
}

Research

  • My TestMapper class is saved in Spring Tool Suite 4 with encoding UTF-8.

    Spring Tool Suite

  • I checked the variable ${project.build.sourceEncoding} in POM and the value is UTF-8.

  • If I run a Maven Build from command line, the encoding of the generated code is correct.

  • If I run a Maven Build in Spring Tool Suite 4, the encoding of the generated code is correct.

  • If I run a clean build in Spring Tool Suite 4, the encoding of the generated code is wrong.

  • I read Encoding problem when compiler is Eclipse JDT (IDE), but this issue was closed 2018 without any solution.

  • I read How to support UTF-8 encoding in Eclipse and tried following solutions:

    • I changed Window > Preferences > General > Content Types to UTF-8.
    • I changed Window > Preferences > General > Workspace to UTF-8

    but none worked.

Question

Is there any way to set the encoding for the generated code?

dur
  • 15,689
  • 25
  • 79
  • 125
  • 1
    [Did you try all the suggestions listed here?](https://stackoverflow.com/questions/9180981/how-to-support-utf-8-encoding-in-eclipse) – Ben Zegveld Sep 03 '22 at 15:07
  • @BenZegveld Thanks for the link, this answer: https://stackoverflow.com/a/42365557/5277820 solved my problem. But now all my workspaces have to be encoded with UTF-8, that's bad. I have some workspaces with CP1252. So I need for these workspaces another installation of Spring Tool Suite. – dur Sep 03 '22 at 15:50

0 Answers0