2014-09-23 40 views
1

我正在用mongodb構建一個spring webapp,但最近我開始寫數據庫時遇到問題。以下堆棧跟蹤是我得到的。寫數據庫時,Spring-Data-Mongodb NoSuchMethodError

SEVERE: Servlet.service() for servlet [DispatcherServlet] in context with path [/afcrowther_blog]  threw exception [Handler processing failed; nested exception is java.lang.NoSuchMethodError: org.springframework.data.mongodb.core.mapping.MongoPersistentProperty.isWritable()Z] with root cause 
java.lang.NoSuchMethodError:  org.springframework.data.mongodb.core.mapping.MongoPersistentProperty.isWritable()Z 
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$3.doWithPersistentProperty(MappingMongoConverter.java:415) 
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$3.doWithPersistentProperty(MappingMongoConverter.java:412) 
at org.springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:294) 
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:412) 
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:386) 
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.write(MappingMongoConverter.java:350) 
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.write(MappingMongoConverter.java:77) 
at org.springframework.data.mongodb.core.MongoTemplate.toDbObject(MongoTemplate.java:732) 
at org.springframework.data.mongodb.core.MongoTemplate.doInsert(MongoTemplate.java:714) 
at org.springframework.data.mongodb.core.MongoTemplate.insert(MongoTemplate.java:672) 
at org.springframework.data.mongodb.core.MongoTemplate.insert(MongoTemplate.java:663) 

讀了這最常見的原因後,似乎是依賴不匹配,但我不知道它的依賴性與在這種情況下彼此實際上是兼容的。

的pom.xml

<spring.version>4.0.7.RELEASE</spring.version> 
    <jackson.version>1.9.2</jackson.version> 
    <spring.security.version>3.2.5.RELEASE</spring.security.version> 

</properties> 

<dependencies> 

    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>4.11</version> 
     <scope>test</scope> 
    </dependency> 

    <dependency> 
     <groupId>org.mongodb</groupId> 
     <artifactId>mongo-java-driver</artifactId> 
     <version>2.12.3</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-context</artifactId> 
     <version>${spring.version}</version> 
    </dependency> 

    <dependency> 
     <groupId>cglib</groupId> 
     <artifactId>cglib</artifactId> 
     <version>3.1</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework.data</groupId> 
     <artifactId>spring-data-mongodb</artifactId> 
     <version>1.6.0.RELEASE</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework.data</groupId> 
     <artifactId>spring-data-commons</artifactId> 
     <version>1.9.0.RELEASE</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-core</artifactId> 
     <version>${spring.version}</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-web</artifactId> 
     <version>${spring.version}</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-webmvc</artifactId> 
     <version>${spring.version}</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-oxm</artifactId> 
     <version>${spring.version}</version> 
    </dependency> 

    <dependency> 
     <groupId>javax.servlet</groupId> 
     <artifactId>javax.servlet-api</artifactId> 
     <version>3.1.0</version> 
     <scope>provided</scope> 
    </dependency> 

    <dependency> 
     <groupId>javax.servlet</groupId> 
     <artifactId>jstl</artifactId> 
     <version>1.1.2</version> 
    </dependency> 

    <dependency> 
     <groupId>taglibs</groupId> 
     <artifactId>standard</artifactId> 
     <version>1.1.2</version> 
    </dependency> 

    <dependency> 
     <groupId>javax.servlet</groupId> 
     <artifactId>servlet-api</artifactId> 
     <version>2.5</version> 
     <scope>provided</scope> 
    </dependency> 

    <dependency> 
     <groupId>org.codehaus.jackson</groupId> 
     <artifactId>jackson-mapper-asl</artifactId> 
     <version>${jackson.version}</version> 
    </dependency> 

    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-validator</artifactId> 
     <version>4.2.0.Final</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-web</artifactId> 
     <version>${spring.security.version}</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-config</artifactId> 
     <version>${spring.security.version}</version> 
    </dependency> 

</dependencies> 

<repositories> 
    <repository> 
     <id>spring-libs-snapshot</id> 
     <name>Spring Snapshot Repository</name> 
     <url>http://repo.spring.io/libs-snapshot</url> 
    </repository> 
</repositories> 

<build> 

    <finalName>afcrowther blog</finalName> 

    <plugins> 
     <plugin> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <version>3.1</version> 
      <configuration> 
       <source>1.7</source> 
       <target>1.7</target> 
      </configuration> 
     </plugin> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-eclipse-plugin</artifactId> 
      <version>2.9</version> 
      <configuration> 
       <downloadSources>true</downloadSources> 
       <downloadJavadocs>true</downloadJavadocs> 
      </configuration> 
     </plugin> 
    </plugins> 

</build> 

這些主要的最新發行版本,雖然我改變了Spring框架4.0.7.Release,試圖修復它,但我也用4.1.0.Release併發生同樣的異常。

感謝

編輯:@Document註解類拋出錯誤,角色設置爲一個字符串,直到這一問題得到解決

@Document(collection = "user") 
public class UserModel{ 

@PersistenceConstructor 
public UserModel(String id, String firstName, String surname, String email, 
     String password, String role) { 
    this.id = id; 
    this.firstName = firstName; 
    this.surname = surname; 
    this.email = email; 
    this.password = password; 
    this.role = role; 
} 

public UserModel(){ 

} 

@Id 
private String id; 

@Field("firstName") 
@Indexed 
private String firstName; 

@Field("surname") 
private String surname; 

@Field("email") 
@Indexed(unique = true) 
private String email; 

@Field("password") 
private String password; 

@Field("role") 
private String role; 

public String getId() { 
    return id; 
} 

public void setId(String id) { 
    this.id = id; 
} 

public String getFirstName() { 
    return firstName; 
} 

public void setFirstName(String firstName) { 
    this.firstName = firstName; 
} 

public String getSurname() { 
    return surname; 
} 

public void setSurname(String surname) { 
    this.surname = surname; 
} 

public String getEmail() { 
    return email; 
} 

public void setEmail(String email) { 
    this.email = email; 
} 

public String getPassword() { 
    return password; 
} 

public void setPassword(String password) { 
    this.password = password; 
} 

public String getRole(){ 
    return role; 
} 

public void setRole(String role){ 
    this.role = role; 
} 
} 
+1

我很確定你在'@ Document'註釋類中缺少一個setter。 – 2014-09-23 10:39:46

+0

粘貼課程,任何跳動的內容不正確?謝謝 – afcrowther 2014-09-23 10:51:40

+0

你可以運行'dependency:resolve'嗎?有問題的方法是在Commons 1.9.0中添加的,它可能是一些傳遞性依賴使Maven pull成爲舊版本。 – chrylis 2014-09-23 11:30:54

回答

0

更新的問題,一旦轉到春天BOM和春季數據依賴關係管理,問題依然存在。

但是,從第一個構造函數中除去「@PersistenceConstructor」標記,然後切換構造函數的順序來解決問題。所以默認的構造函數必須是文件中的第一個。不知道爲什麼,但你去了!