具有类似名称的类的 ConversionNotSupportedException
更新:对类等做出了一些错误的假设。当我有一个“演示”项目时,现在会发生以下情况:
我有两个类,都名为“公司”。
- 一个放在grails-app/domain/my.classes.domain.Company
- 另一个放在src/groovy/my.clazz.Company
最后一个有@Validateable注解,Config.groovy中包含grails.validateable.packages = ['my.clazz']
我还有一个 Account 类,位于 grails-app/domain/my.classes.domain.Account:
包 my.classes.domain
导入 java.io.Serializable;
class Account 实现 Serialized { Company company }
然后我使用以下代码(在 bootstrap.groovy 中):
import my.classes.domain.Company
import my.classes.domain.Account
...
Company company = new Company
Account acccount = new Account(company: company)
运行此应用程序时,显示以下错误:
Caused by: org.springframework.beans.ConversionNotSupportedException: Cannot convert value of type [my.clazz.Company] to required type [my.classes.domain.Company] for property 'company': no matching editors or conversion strategy found ... 33 more Caused by: java.lang.IllegalStateException: Cannot convert value of type [my.clazz.Company] to required type [my.classes.domain.Company] for property 'company': no matching editors or conversion strategy found
这是一个非常奇怪的异常,因为一切似乎都很好。 一些测试证明了以下“提示”: 当我修改 config.groovy 以显式命名类(即使用 grails.validateable.classes = ['my.classes.domain.Company'])时,不会发生此错误, 当我将帐户的公司属性修改为不同的名称(并相应地修改引导程序)时,不会发生此错误,即:
class Account extends Serializable {
Company cmp
}
但是,这些是解决方法。我真的很感兴趣为什么会发生这种情况。有人有线索吗?
为了安全起见,我做了以下操作来创建这个问题:
- 创建域类:my.classes.domain.Company
- 创建域类:my.classes.domain.Account
- 如上所述修改域类
- 创建一个groovy类: my.clazz.Company
- 为这个 groovy 类提供 Validatable 注释。
- 将 my.clazz 包添加到
- 引导程序中的可验证包中,使用 new Account(company:company) 创建一个新帐户
UPDATED: made some wrong assumptions about classes etc. The following occurs now when I have a 'demo' project:
I have two classes, both named 'Company'.
- One is placed in grails-app/domain/my.classes.domain.Company
- the other is in src/groovy/my.clazz.Company
The last one has a @Validateable annotation, and the Config.groovy contains grails.validateable.packages = ['my.clazz']
I also have an Account class, in grails-app/domain/my.classes.domain.Account:
package my.classes.domain
import java.io.Serializable;
class Account implements Serializable { Company company }
Then I use the following code (in the bootstrap.groovy):
import my.classes.domain.Company
import my.classes.domain.Account
...
Company company = new Company
Account acccount = new Account(company: company)
When running this app, the following error is shown:
Caused by: org.springframework.beans.ConversionNotSupportedException: Cannot convert value of type [my.clazz.Company] to required type [my.classes.domain.Company] for property 'company': no matching editors or conversion strategy found ... 33 more Caused by: java.lang.IllegalStateException: Cannot convert value of type [my.clazz.Company] to required type [my.classes.domain.Company] for property 'company': no matching editors or conversion strategy found
This is a very strange exception, as everything seems to be fine.
Some testing proved the following 'hints':
This error does NOT occur when I modify the config.groovy to explicitly name the classes (i.e. use grails.validateable.classes = ['my.classes.domain.Company']),
This error does NOT occur when I modify the Account's company property to be named differently (and modify the bootstrap accordingly), i.e.:
class Account extends Serializable {
Company cmp
}
However, these are workarounds. I'm really interested in WHY this is happening. Anybody got a clue?
Just to be on the safe side, I did the following to create this problem:
- create domain class: my.classes.domain.Company
- create domain class: my.classes.domain.Account
- modify the domain class as above
- create a groovy class: my.clazz.Company
- give this groovy class the Validatable annotation.
- add the my.clazz package to the validateable packages
- in the bootstrap, create a new Account with new Account(company:company)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除了代码中的各种拼写错误导致问题难以确定之外,问题似乎是您正在尝试使用 my.class.domain 设置 my.class.Company 类型的 Account.company 属性。公司类型。您的引导程序需要更改为:
注意,公司的正确导入语句。
Aside from the various typos in your code which makes the problem hard to determine, the problem would seem to be that you are trying to set the Account.company property which is of type my.class.Company with the my.class.domain.Company type. Your bootstrap would need to be changed to:
Note, the correct import statement for Company.
根据您描述的错误,我认为Grails确定了错误:您转入帐户的公司被确定为my.clazz.Company,而不是my.classes.domain.Company。
您可以进行简单的检查来确定 bootstrap 中
company
的类型:According to the error you describe, I think that Grails have determined wrong: the company you transfer into Account was determined as a my.clazz.Company, not my.classes.domain.Company.
You can put a simple check to know for sure what's the type of
company
in bootstrap: