Spring Data DomainClassConverter not working (in conjunction with Java Config)

I am trying to configure Spring / Data / JPA Spring Domain / Cluster to automatically convert (String) to domain classes.

My project uses Java Config (therefore no xml).

In my WebConfig, I have:

@Configuration @EnableWebMvc public class WebConfig extends WebMvcConfigurerAdapter { @Override public void addFormatters(FormatterRegistry registry) { registry.addConverter(new DomainClassConverter<DefaultFormattingConversionService>((DefaultFormattingConversionService) registry)); } } 

This looks like a successful DomainClassConverter connection, as I see it inside the print conversion service:

 ConversionService converters = ..<default converters>.. o rg.springframework.data.repository.support.DomainClassConverter@ 6ea4ce0d, org.springframework.core.convert.support.IdToEntityConverter@5d3 f03b, o rg.springframework.core.convert.support.ObjectToObjectConverter@ 1d40b47a 

But when sending a nested form (Order with client ref), the Client is not automatically converted, and therefore I get:

 Failed to convert property value of type java.lang.String to required type org.mycomp.domain.Customer for property customer; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.mycomp.domain.Customer] for property customer: no matching editors or conversion strategy found 

I wonder if I'm doing something wrong here?

+4
source share
2 answers

DomainClassConverter must be declared as a bean (because it is ApplicationContextAware ), and it is automatically registered in ConversionService , so you do not need to register it manually:

 @Bean @Autowired public DomainClassConverter domainClassConverter(ConversionService cs) { return new DomainClassConverter(cs); } 
+7
source

An exception indicates that you need a custom converter to turn String into Customer. You will need to make input tokenization and display it in the Customer fields. This is true for all of your domain classes.

A Google search also leads me to a topic in the Spring forum:

http://forum.springsource.org/showthread.php?122944-Help-with-DomainClassConverter-configuration

This suggests that there may be an error, depending on the version of Spring being used.

-1
source

Source: https://habr.com/ru/post/1434153/


All Articles