【问题标题】:NoSuchMethodError in Jetty with Spring Data Mongo custom repository带有 Spring Data Mongo 自定义存储库的 Jetty 中的 NoSuchMethodError
【发布时间】:2014-05-24 20:58:06
【问题描述】:

我正在使用 Maven、Spring MVC 和 Spring Data Mongo 开发一个小型 Web 应用程序。当我的一个控制器尝试访问自定义存储库中定义的方法时,我收到了 java.lang.NoSuchMethodError。当通过扩展 AbstractJUnit4SpringContextTests 的 JUnit 4 测试并使用几乎相同的 XML 配置文件进行练习时,同样的方法也可以正常工作。

标准存储库:

public interface IndividualRepository extends MongoRepository<Individual, String>, IndividualRepositoryCustom {
    ...
}

自定义界面:

public interface IndividualRepositoryCustom {
    Individual findByIdentifier(String identifierType, String identifierValue);
}

自定义实现:

public class IndividualRepositoryImpl implements IndividualRepositoryCustom {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Individual findByIdentifier(String identifierType, String identifierValue) {
        String locator = String.format("identifiers.%s", identifierType);
        return mongoTemplate.findOne(query(where(locator).is(identifierValue)), Individual.class);
    }

}

dataaccess-config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:mongo="http://www.springframework.org/schema/data/mongo"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/data/mongo
    http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd">

    <mongo:repositories base-package="com.myco.dataaccess"/>

    <mongo:mongo host="mongo.myco.com" port="27017"/>

    <mongo:db-factory dbname="test" mongo-ref="mongo"/>

    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg ref="mongo"/>
        <constructor-arg value="test"/>
    </bean>

</beans>

在我的 JUnit 测试中,我有(摘录):

@Autowired
private IndividualRepository individualRepo;

...

List<Individual> foundList = individualRepo.findAll();
assertNotNull(foundList);
assertTrue(foundList.size() > 0);

Individual found = individualRepo.findByIdentifier("someid", "123456");
assertNotNull(found);
assertEquals("Bob", found.getFirstName());

测试通过了,调用了 findAll()(标准 Repository 方法)和 findByIdentifier()(自定义方法)。后者在被 Jetty 中的 Web 应用程序中运行的 Controller 调用时失败并出现 NoSuchMethodError,而同一个 Controller 可以毫无问题地调用 findAll()。

【问题讨论】:

    标签: spring maven spring-mvc spring-data-mongodb maven-jetty-plugin


    【解决方案1】:

    结果证明这与 Spring Data 无关,而是我在多模块构建中使用 maven-jetty-plugin 的方式存在问题。

    基本上,我正在为我的 web 模块运行 mvn jetty:run,该模块依赖于我的数据访问模块(我的 JUnit 测试所在的位置)。当我使用mvn clean package 重建项目时,最新版本没有放在我的本地存储库中,因此没有被与我的构建过程一起运行的mvn jetty:run 进程拾取。问题已通过使用mvn clean install 构建解决。

    因此,像往常一样,错误消息就出现了 - 在提供 Jetty 的 JAR 版本中确实不存在该方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-06
      • 2017-09-19
      • 2016-08-29
      • 1970-01-01
      • 2017-03-26
      • 1970-01-01
      • 1970-01-01
      • 2017-06-18
      相关资源
      最近更新 更多