具有类似名称的类的 ConversionNotSupportedException

发布于 2024-11-06 02:54:40 字数 1845 浏览 0 评论 0原文

更新:对类等做出了一些错误的假设。当我有一个“演示”项目时,现在会发生以下情况:

我有两个类,都名为“公司”。

  • 一个放在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
}

但是,这些是解决方法。我真的很感兴趣为什么会发生这种情况。有人有线索吗?

为了安全起见,我做了以下操作来创建这个问题:

  1. 创建域类:my.classes.domain.Company
  2. 创建域类:my.classes.domain.Account
  3. 如上所述修改域类
  4. 创建一个groovy类: my.clazz.Company
  5. 为这个 groovy 类提供 Validatable 注释。
  6. 将 my.clazz 包添加到
  7. 引导程序中的可验证包中,使用 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:

  1. create domain class: my.classes.domain.Company
  2. create domain class: my.classes.domain.Account
  3. modify the domain class as above
  4. create a groovy class: my.clazz.Company
  5. give this groovy class the Validatable annotation.
  6. add the my.clazz package to the validateable packages
  7. in the bootstrap, create a new Account with new Account(company:company)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丢了幸福的猪 2024-11-13 02:54:40

除了代码中的各种拼写错误导致问题难以确定之外,问题似乎是您正在尝试使用 my.class.domain 设置 my.class.Company 类型的 Account.company 属性。公司类型。您的引导程序需要更改为:

import my.class.Company
import my.class.Account

Company company = new Company
Account acccount = new Account(company: 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:

import my.class.Company
import my.class.Account

Company company = new Company
Account acccount = new Account(company: company)

Note, the correct import statement for Company.

活泼老夫 2024-11-13 02:54:40

根据您描述的错误,我认为Grails确定了错误:您转入帐户的公司被确定为my.clazz.Company,而不是my.classes.domain.Company。

您可以进行简单的检查来确定 bootstrap 中 company 的类型:

import java.lang.Class;
...
println company.getClass().getName()

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:

import java.lang.Class;
...
println company.getClass().getName()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文