4

I'm trying to setup the Spring Data/JPA DomainClassConverter to automatically convert (String) id's to the domain classes itself.

My project is uses Java Config (so no xml).

In my WebConfig I have currently:

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new DomainClassConverter<DefaultFormattingConversionService>((DefaultFormattingConversionService) registry));
    }
}

This seems to hook up the DomainClassConverter successfully as I can see it inside the conversion service when printing that:

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

But when submitting a nested form (Order with Customer ref) the Customer is not converted automatically and hence I get a:

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'm wondering if I'm doing something wrong here?

Marcel Overdijk
  • 11,041
  • 17
  • 71
  • 110

2 Answers2

7

DomainClassConverter should be declared as a bean (because it's ApplicationContextAware), and it registers itself in ConversionService automatically, so that you don't need to register it manually:

@Bean @Autowired
public DomainClassConverter domainClassConverter(ConversionService cs) {
    return new DomainClassConverter(cs);
}
axtavt
  • 239,438
  • 41
  • 511
  • 482
  • 1
    An example of this can be seen in https://github.com/SpringSource/spring-data-book/blob/rest-extension/rest/src/main/java/com/oreilly/springdata/rest/RestWebApplicationInitializer.java – Oliver Drotbohm Sep 14 '12 at 09:56
  • Yes thanks this works partially now. The String id to Domain class works. However when printing a Order domain class in a jsp view I get a `org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type @javax.validation.constraints.NotNull @javax.persistence.ManyToOne org.mycomp.domain.Customer to type java.lang.String` error. Am I still missing something here? – Marcel Overdijk Sep 14 '12 at 10:18
  • That's the way it's intended to work. The converter's purpose is to ease binding to a Spring MVC controller or a Spring bean in general. I don't think it's reasonable to default the common toString() representation of an entity to a string representation of its id. It shouldn't be a too big issue to just reference ${entity.id} instead. – Oliver Drotbohm Sep 14 '12 at 10:40
  • 1
    Forget my last comment, it had nothing to do with DomainClassConverter as it gave the same error without having it configured. Problem was related to ``. That spring:eval tag causing the problem. `${order.customer}` works fine. – Marcel Overdijk Sep 14 '12 at 10:59
  • 2
    `@Bean public DomainClassConverter> domainClassConverter(FormattingConversionService conversionService) { return new DomainClassConverter(conversionService); }` is the variant I'm using now. – Marcel Overdijk Sep 14 '12 at 11:00
-1

The exception is telling you that you need a custom converter to turn a String into a Customer. You'll have to tokenize the input and map it into Customer fields. That's true for all your domain classes.

A Google search also lead me to a thread in the Spring forum:

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

It suggests that there might be a bug, depending on which version of Spring you're using.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • Thanks for your reply @duffymo. However I'm using latest Spring and Spring Data and as Oliver Gierke it should be working in these versions. – Marcel Overdijk Sep 14 '12 at 09:35