Spring Data Basic Web支持DomainClassConverter不使用xml config

时间:2015-12-03 19:50:55

标签: java spring spring-mvc spring-data

我尝试创建一个简单的spring数据和mvc项目 根据Spring docs http://docs.spring.io/spring-data/commons/docs/current/reference/html/#core.web.basic,我希望在我的控制器中有一个简单的方法

@RequestMapping("/{id}")
public String showRoleForm(@PathVariable("id") Role role, Model model) {
    model.addAttribute("role", role);
    return "roleForm";
}

但它会产生运行时错误

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)

所以,我找到了一些建议 Spring Data DomainClassConverter not working (in combination with Java Config)
Issues using Spring's DomainClassConverter in Spring MVC

我尝试通过添加

来更改spring-servlet.xml
<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"/>-->

和app工作正常。

我也尝试用

改变方法
@RequestMapping("/{role}")
public String showRoleForm(@PathVariable("role") Role role, Model model) {
    model.addAttribute("role", role);
    return "roleForm";
}

但它也不起作用。

所以我有一个问题 - 为什么?

不要在这里传递我的代码。你可以看一下git https://github.com/dimalu85/JavaEE-Web-Template/tree/domainclassconverter

可能是java配置将在官方样本http://github.com/spring-projects/spring-data-book/blob/rest-extension/rest/src/main/java/com/oreilly/springdata/rest/order/web/ManualCustomerController.java上显示 - 我还没试过。但我们也有关于java配置的Spring Data DomainClassConverter not working (in combination with Java Config)问题。 它也是用户http://github.com/spring-projects/spring-data-examples/blob/master/web/example/src/main/java/example/users/web/UserController.java的示例 - 但它没有id的用户方法。

0 个答案:

没有答案