Repository navigation
Description
My spring boot application is a none web application, and spring boot will not auto create the ConversionService bean (which class is WebConversionService), so I need to manually create one.
@Bean
public ConversionService conversionService() {
var conversionService = new DefaultFormattingConversionService();
return conversionService;
}Here is my converter and tests:
@Mapper(config = MapStructConfig.class)
public interface UserMapper extends Converter<UserBO, UserDTO> {
UserDTO convert(UserBO userBO);
@InheritInverseConfiguration
@DelegatingConverter
UserBO invertConvert(UserDTO userDTO);
}@MapperConfig(componentModel = "spring")
@SpringMapperConfig(
externalConversions = @ExternalConversion(sourceType = String.class, targetType = Locale.class)
)
public interface MapStructConfig {
}@SpringBootTest(classes = Application.class)
public class UserMapperTest {
@Autowired
private ConversionService conversionService;
@Test
public void shouldMapBoToDto() {
var userBo = UserBO.builder()
.id(1L)
.username("jaggerwang")
.build();
var userDto = conversionService.convert(userBo, UserDTO.class);
assertThat(userDto).isNotNull();
assertThat(userDto.getId()).isEqualTo(userBo.getId());
assertThat(userDto.getUsername()).isEqualTo(userBo.getUsername());
}
@Test
public void shouldMapDtoToBo() {
var userDto = UserDTO.builder()
.id(1L)
.username("jaggerwang")
.build();
var userBo = conversionService.convert(userDto, UserBO.class);
assertThat(userBo).isNotNull();
assertThat(userBo.getId()).isEqualTo(userDto.getId());
assertThat(userBo.getUsername()).isEqualTo(userDto.getUsername());
}
}The tests will pass when using the auto created ConversionService bean, but when run with the manually created one, it will fail with error "org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [ai.basic.basicai.client.dto.user.UserDTO] to type [ai.basic.basicai.user.entity.UserBO]".
org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [ai.basic.basicai.user.entity.UserBO] to type [ai.basic.basicai.client.dto.user.UserDTO]
at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:321)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:195)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:175)
at ai.basic.basicai.user.adapter.converter.dto.UserMapperTest.shouldMapBoToDto(UserMapperTest.java:25)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
And there is another error when using the manually created ConversionService bean, the @value annotaion cannot convert my custom enum type again.
public enum RunningMode {
CLOUD_GLOBAL("cloud-global"),
CLOUD_CHINA("cloud-china"),
@Deprecated
ONPREM("onprem");
private final String name;
RunningMode(String name) {
this.name = name;
}
public String getName() {
return name;
}
public static RunningMode convert(String value) {
for (var mode : values()) {
if (mode.name.equals(value)) {
return mode;
}
}
return null;
}
} @Value("${runningMode}")
protected RunningMode runningMode;runningMode: cloud-global
Caused by: java.lang.IllegalArgumentException: No enum constant ai.basic.basicai.client.enums.RunningMode.cloud-global
at java.base/java.lang.Enum.valueOf(Enum.java:273) ~[na:na]
at org.springframework.core.convert.support.StringToEnumConverterFactory$StringToEnum.convert(StringToEnumConverterFactory.java:54) ~[spring-core-6.0.14.jar:6.0.14]
at org.springframework.core.convert.support.StringToEnumConverterFactory$StringToEnum.convert(StringToEnumConverterFactory.java:39) ~[spring-core-6.0.14.jar:6.0.14]
at org.springframework.core.convert.support.GenericConversionService$ConverterFactoryAdapter.convert(GenericConversionService.java:436) ~[spring-core-6.0.14.jar:6.0.14]
at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:41) ~[spring-core-6.0.14.jar:6.0.14]
... 173 common frames omitted
If I change cloud-global to CLOUD_GLOBAL in property file, it became ok again.
Activity
jaggerwang commentedon Apr 22, 2024
If I change the class of manually created ConversionService bean from
DefaultFormattingConversionServicetoApplicationConversionService, the enum type convert error dissapeared, but the custom mappers still not be auto detected.Chessray commentedon Apr 22, 2024
Spring's
ConversionServiceinitialization doesn't follow standard procedures. You can try the generated ConverterScan annotations. This feature wasn't designed for your case, but may cover it nonetheless.jaggerwang commentedon Apr 23, 2024
@Chessray Thanks for your suggestion! It seems
ConverterScanis just for test only, I tried the following way but the problem is still there.The custom converter
UserMapperstill cannot be found in theconvertersfield of theconversionServicebean.jaggerwang commentedon Apr 23, 2024
Temporary resolve this problem by manually add converters to
ApplicationConversionService, but it's very inconvenient.jaggerwang commentedon Apr 24, 2024
Does it because the
ConversionServicebean created after custom converter beans, so the converters cannot be auto registered, as there is noConversionServicebean. But how to make the generated mapper implementation to depend on theConversionServicebean?Chessray commentedon Apr 25, 2024
The generated
ConverterScan(as opposed to the one provided intest-extensions) is intended for production use. Is there any way for you to share your project so I can help a bit more? In the meantime, you might wish to compare it to the example project.Can you confirm whether your generated code includes the
@ConverterScanannotation?jaggerwang commentedon Apr 26, 2024
Here is the testing project.
ai.basic.basicai.user.adapter.config.CommonConfigfiliphr commentedon Apr 27, 2024
@jaggerwang why don't you do something like:
Something like this is going to register all the beans that are implementing
Converterin theApplicationConversionService.jaggerwang commentedon Apr 28, 2024
@filiphr Thanks for your suggestion, it really works! But I still think it's the responsibility of MapStruct Spring Extension to add all converters generated by MapStruct to the global ConversionService, even it was created by myself.
filiphr commentedon Apr 28, 2024
I disagree with this @jaggerwang. When you are using Spring Boot the
WebMvcAutoConfigurationwith itsWebMvcAutoConfigurationAdapter#addFormatteris responsible for adding the required converters for the Spring MVCFormatterRegistry.From what I've learned reading from the comments from @Chessray and his work on this is that the
ConversionServiceis a special type of bean in the Spring application context. Therefore, I think it is the responsibility of the user to handle this appropriately.I'll leave the decision whether something should be done here to @Chessray.
Chessray commentedon Apr 29, 2024
Your test project works if you add the generated
ConverterScan:Add this dependency:
Add this line to
MapStructConfig:There's no need for any manual adds after doing this.
The one thing this highlighted though is that we should probably update the generated
@PostConstructimport so that it uses the newer packagejakarta.annotationinstead ofjavax.annotation.jaggerwang commentedon Apr 29, 2024
👍So is there any plan to fix this problem, and is there any posibility to not require user to include dependency
jakarta.annotation-apiGot compile error "package javax.annotation not exist" right now.

Chessray commentedon Apr 29, 2024
You need to include the older dependency
jsr250as outlined above.jakarta-annotationcomes with Spring Boot 3 anyway. I'll have to look into how we can add this in a nice fashion. Also, remember this is an open source project. You're welcome to submit a Fix yourself. 🙂jaggerwang commentedon Apr 29, 2024
OK,I got it!I'll try if I can:)
stickfigure commentedon Jul 17, 2024
PR here: #110
stickfigure commentedon Jul 17, 2024
If this issue is going to track compatibility with Spring Boot 3, maybe retitle it?
stickfigure commentedon Jul 17, 2024
Also worth mentioning - it's not harmless to add
javax.annotation-apito a Spring Boot 3 project. All it takes is one misclick to import the wrong annotation and you have a potentially serious bug that will only be caught by integration tests (if you have them).Chessray commentedon Aug 19, 2024
Closed by #110.