Spring无法在Spring Data Mongodb中读取@query注释

时间:2018-12-18 07:26:03

标签: spring mongodb spring-mvc spring-data-jpa

亲爱的

我在尝试使用注释org.springframework.data.mongodb.repository.Query时遇到 java.lang.reflect.MalformedParameterizedTypeException ,并且我的Web应用程序无法在生产服务器上启动, 尽管测试服务器上具有相同WebSphere 9.0.0.8和JDK1.8的相同代码可以正常工作,请告知可能导致此问题的原因。

完整例外

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'registrationServiceImpl': Unsatisfied dependency expressed through field 'newRegistrationRepo'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'newRegistrationRepo': Cannot create inner bean '(inner bean)#e371922b' of type [org.springframework.data.repository.query.ExtensionAwareEvaluationContextProvider] while setting bean property 'evaluationContextProvider'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#e371922b': Initialization of bean failed; nested exception is java.lang.reflect.MalformedParameterizedTypeException
                at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:592)
                at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)

这是我的java类中的注释

@Query("{'$and':[ {'cif':?0}, {'identification': ?1} , {'mobileNumber' : ?2}, {'applicationStatus' : 'Active' } ] }")

这是spring数据mongo的Maven依赖项

   <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-mongodb</artifactId>
        <version>1.8.1.RELEASE</version>
        <exclusions>
            <exclusion>
                <artifactId>spring-core</artifactId>
                <groupId>org.springframework</groupId>
            </exclusion>
            <exclusion>
                <artifactId>spring-beans</artifactId>
                <groupId>org.springframework</groupId>
            </exclusion>
        </exclusions>
    </dependency>

这是Spring Maven的依赖项

<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-config</artifactId>
     <version>4.1.3.RELEASE</version> 
     <exclusions>
        <exclusion>
            <artifactId>spring-beans</artifactId>
            <groupId>org.springframework</groupId>
        </exclusion>
     </exclusions>
</dependency>
<dependency>
  <artifactId>spring-core</artifactId>
    <groupId>org.springframework</groupId>
  <version>4.3.4.RELEASE</version>
 </dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context-support</artifactId>
  <version>4.3.4.RELEASE</version>
    <exclusions>
    <exclusion>
      <artifactId>commons-logging</artifactId>
      <groupId>commons-logging</groupId>
    </exclusion>
    <exclusion>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
    </exclusion>
    <exclusion>
        <artifactId>spring-core</artifactId>
        <groupId>org.springframework</groupId>
    </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jms</artifactId>
    <version>4.3.4.RELEASE</version>
    <exclusions>
        <exclusion>
            <artifactId>spring-core</artifactId>
            <groupId>org.springframework</groupId>
        </exclusion>
        <exclusion>
            <artifactId>spring-beans</artifactId>
            <groupId>org.springframework</groupId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>4.3.4.RELEASE</version>
    <exclusions>
        <exclusion>
            <artifactId>spring-beans</artifactId>
            <groupId>org.springframework</groupId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.3.4.RELEASE</version>
    <exclusions>
        <exclusion>
            <artifactId>spring-core</artifactId>
            <groupId>org.springframework</groupId>
        </exclusion>
        <exclusion>
            <artifactId>spring-web</artifactId>
            <groupId>org.springframework</groupId>
        </exclusion>
        <exclusion>
            <artifactId>spring-beans</artifactId>
            <groupId>org.springframework</groupId>
        </exclusion>
    </exclusions>
</dependency>

3 个答案:

答案 0 :(得分:0)

通过在查询中放置value属性来检查一次。

@Query(value="{'$and':[ {'cif':?0}, {'identification': ?1} , {'mobileNumber' : ?2}, {'applicationStatus' : 'Active' } ] }")

答案 1 :(得分:0)

要使用@Query中的org.springframework.data.mongodb.repository.Query,必须添加以下依赖项

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

顺便说一句,很难弄清楚为什么它可以在测试服务器上工作

答案 2 :(得分:0)

首先,@ Query来自 导入org.springframework.data.mongodb.repository.Query;

您可以先尝试简单查询以检查配置问题。 喜欢:

@Query("{ 'name' :  ?0 }")
@Query(value = "{ 'name' :  ?0 }", fields = "{ '_id': 0, 'name':1, 'age':1 }")

对于复杂的查询,您可以同时满足约定问题和“映射/解析数据”问题,请分步检查

更多内容:默认情况下,Mongo存储库通过AND查询,因此您的查询可以减少为:

'cif':?0, 'identification': ?1, 'mobileNumber' : ?2, 'applicationStatus' : 'Active'