首页 文章

带有Spring Data Mongo自定义存储库的Jetty中的NoSuchMethodError

提问于
浏览
0

我使用Maven,Spring MVC和Spring Data Mongo开发了一个小型Web应用程序 . 当我的一个控制器试图访问自定义存储库中定义的方法时,我得到一个java.lang.NoSuchMethodError . 通过扩展AbstractJUnit4SpringContextTests并使用几乎相同的XML配置文件的JUnit 4测试来执行相同的方法 .

标准存储库:

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);
    }

}

数据访问-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()而没有任何问题 .

1 回答

  • 0

    事实证明这与Spring Data没有任何关系,但是我使用maven-jetty-plugin与我的多模块构建的方式存在问题 .

    基本上,我为我的web模块运行了 mvn jetty:run ,它依赖于我的dataaccess模块(我的JUnit测试所在的位置) . 当我使用 mvn clean package 重建项目时,最新版本没有放在我的本地仓库中,因此没有被我的构建过程中运行的 mvn jetty:run 进程接收 . 通过 mvn clean install 构建解决了问题 .

    因此,像往常一样,错误消息是正确的 - 该方法确实不存在于提供Jetty的JAR版本中 .

相关问题