写入db时Spring-Data-Mongodb NoSuchMethodError

时间:2014-09-23 10:38:47

标签: java spring mongodb maven spring-data

我正在使用mongodb构建一个spring webapp但最近我开始遇到写入DB的问题。我得到的是以下堆栈跟踪。

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。请尝试修复它,但我也使用了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 个答案:

答案 0 :(得分:0)

更新问题,在更改为spring bom和spring数据bom依赖关系管理后,问题仍然存在。

然而,删除&#34; @ PersistenceConstructor&#34;标记来自第一个构造函数,然后切换构造函数的顺序修复了问题。因此,默认构造函数必须是文件中的第一个。不知道为什么,但你去了!