我正在使用如下定义的域类构建Grails 3.2.3应用程序:
class Entity {
String start
String arrival
Date date
String sessionId
String serviceName
Integer serviceVersion
Integer nodeNumber
Integer execTime
Integer contextExecTime
static hasMany = [
contextAttributes: ContextAttribute,
conditions: Condition,
tasks: Task
]
static constraints = {
contextAttributes nullable: true
conditions nullable: true
tasks nullable: true
}
static mapping = {
contextAttributes defaultValue: null
conditions defaultValue: null
tasks defaultValue: null
}
}
ContextAttribute
,Condition
和Task
是其他域类,它们具有对belongsTo
的Entity
引用,从而建立了一对多关系。然后
EntityServiceSpec
定义如下:@TestFor(EntityService)
@Mock(Entity)
class EntityServiceSpec extends Specification {
void "Create an entity"() {
service.create(new Entity(
start: "Start",
arrival: "Arrival",
date: new Date(0),
sessionId: "RANDOM",
serviceName: "RANDOM",
serviceVersion: 1,
nodeNumber: 1,
execTime: 1,
contextExecTime: 1))
expect:
Entity.count() == 1
}
}
方法
EntityService.create(Entity entity)
只是保存。运行测试,我得到以下堆栈跟踪:
org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type [java.util.Collections$UnmodifiableMap] to required type [java.util.Set] for property 'contextAttributes'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.util.Collections$UnmodifiableMap] to required type [logmadeeasy.ContextAttribute] for property 'contextAttributes[0]': no matching editors or conversion strategy found
at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:591)
at org.springframework.beans.AbstractNestablePropertyAccessor.convertForProperty(AbstractNestablePropertyAccessor.java:603)
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:216)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1532)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1491)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1231)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:385)
at org.grails.plugins.web.controllers.api.ControllersDomainBindingApi.autowire(ControllersDomainBindingApi.java:100)
at org.grails.plugins.web.controllers.api.ControllersDomainBindingApi.initialize(ControllersDomainBindingApi.java:53)
at logmadeeasy.EntityServiceSpec.Create an entity(EntityServiceSpec.groovy:15)
Caused by: java.lang.IllegalStateException: Cannot convert value of type [java.util.Collections$UnmodifiableMap] to required type [logmadeeasy.ContextAttribute] for property 'contextAttributes[0]': no matching editors or conversion strategy found
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:306)
at org.springframework.beans.TypeConverterDelegate.convertToTypedCollection(TypeConverterDelegate.java:574)
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:220)
at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:576)
... 9 more
这是什么意思?我在浪费时间:(
最佳答案
您有一个配置为contextAttributes
的Spring bean,它与域类的名为contextAttributes
的属性冲突。我建议您在类级别禁用 Autowiring :
static mapping = {
...
autowire false
}
或全局
grails-app/conf/application.groovy
中:grails.gorm.default.mapping = {
autowire false
}
关于grails - Grails应用程序在域类构建时抛出ConversionNotSupportedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40746006/