I try to create a simple spring data and mvc project.
According to Spring docs http://docs.spring.io/spring-data/commons/docs/current/reference/html/#core.web.basic I want to have a simple method in my controller
@RequestMapping("/{id}")
public String showRoleForm(@PathVariable("id") Role role, Model model) {
model.addAttribute("role", role);
return "roleForm";
}
But it create runtime error
type Exception report
message Failed to convert value of type 'java.lang.String' to required type 'com.nzsoft.testweb.persistence.Role'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.nzsoft.testweb.persistence.Role]: no matching editors or conversion strategy found
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.springframework.web.method.annotation.MethodArgumentConversionNotSupportedException: Failed to convert value of type 'java.lang.String' to required type 'com.nzsoft.testweb.persistence.Role'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.nzsoft.testweb.persistence.Role]: no matching editors or conversion strategy found
org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:111)
root cause
java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.nzsoft.testweb.persistence.Role]: no matching editors or conversion strategy found
org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:302)
So, i found some advices
Spring Data DomainClassConverter not working (in combination with Java Config)
and
Issues using Spring's DomainClassConverter in Spring MVC.
I've tried to change my spring-servlet.xml with adding
<mvc:annotation-driven conversion-service="conversionService" />
<bean class="org.springframework.data.repository.support.DomainClassConverter">
<constructor-arg ref="conversionService" />
</bean>
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"/>
<!-- same OK result with -->
<!--<bean id="conversionService" class="org.springframework.core.convert.support.DefaultConversionService"/>-->
and app became working OK.
also I've tried to change method with
@RequestMapping("/{role}")
public String showRoleForm(@PathVariable("role") Role role, Model model) {
model.addAttribute("role", role);
return "roleForm";
}
but it doesn't work too.
So I have a question - why?
Dont want to pass here al my code. You can look on git https://github.com/dimalu85/JavaEE-Web-Template/tree/domainclassconverter
may be java config will working as it shown on official sample http://github.com/spring-projects/spring-data-book/blob/rest-extension/rest/src/main/java/com/oreilly/springdata/rest/order/web/ManualCustomerController.java - i haven't tried. But we have issue Spring Data DomainClassConverter not working (in combination with Java Config) about java config too. also it is example with users http://github.com/spring-projects/spring-data-examples/blob/master/web/example/src/main/java/example/users/web/UserController.java - but it has no such method user by id.