NoSuchMethodException 使用 datastax cassandra 映射器

Posted

技术标签:

【中文标题】NoSuchMethodException 使用 datastax cassandra 映射器【英文标题】:NoSuchMethodException using the datastax cassandra mapper 【发布时间】:2017-04-09 08:23:11 【问题描述】:

我刚刚开始尝试使用 cassandra 和 java,以及 spring REST 库。

但是,当我尝试从 cassandra 取回对象时出现此错误,我收到此错误

java.lang.NoSuchMethodException: objects.DataSet.<init>()
at java.lang.Class.getConstructor0(Class.java:3082) ~[na:1.8.0_111]
at java.lang.Class.getConstructor(Class.java:1825) ~[na:1.8.0_111]
at com.datastax.driver.mapping.ReflectionUtils.newInstance(ReflectionUtils.java:37) ~[cassandra-driver-mapping-3.1.0.jar:na]
at com.datastax.driver.mapping.EntityMapper.newEntity(EntityMapper.java:62) ~[cassandra-driver-mapping-3.1.0.jar:na]

这就是我使用映射器的方式

@RequestMapping(value="/data_sets/dataSetId", method=RequestMethod.GET)
public DataSet show(@PathVariable(value="dataSetId") String dataSetId)
    Cluster cluster = Cluster.builder().withClusterName("testCluster").addContactPoint("127.0.0.1").build();
    Session session = cluster.connect("testks");

    MappingManager manager = new MappingManager(session);

    Mapper<DataSet> mapper = manager.mapper(DataSet.class);

    System.out.println("loading data set ....");
    DataSet dataSet = mapper.get(dataSetId);
    return dataSet;

这是对象

@Table(keyspace="testks", name="DataSet", caseSensitiveKeyspace=false, caseSensitiveTable=false)
public class DataSet 

@PartitionKey
@Column
String id;
@Column
String name;

public String getName() 
    return name;


public DataSet setName(String name) 
    this.name = name;
    return this;


public DataSet setId(String id)
    this.id = id;
    return this;


public DataSet(String name)
    this.id = UUIDs.timeBased().toString();
    this.name = name;

我错过了什么?

编辑:

这里是 pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Banana</groupId>
<artifactId>Banana</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <source />
                <target />
            </configuration>
        </plugin>
    </plugins>
</build>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.2.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

<dependencies>
    <!--<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-cassandra</artifactId> 
        <version>1.4.4.RELEASE</version> </dependency> -->
    <dependency>
        <groupId>com.datastax.cassandra</groupId>
        <artifactId>cassandra-driver-core</artifactId>
        <version>3.1.0</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-xml</artifactId>
    </dependency>
    <dependency>
        <groupId>com.datastax.cassandra</groupId>
        <artifactId>cassandra-driver-mapping</artifactId>
        <version>3.1.0</version>
    </dependency>
    <dependency>
        <groupId>com.datastax.cassandra</groupId>
        <artifactId>cassandra-driver-extras</artifactId>
        <version>3.1.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
        <version>4.2.1.BUILD-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>3.0.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>com.jayway.jsonpath</groupId>
        <artifactId>json-path</artifactId>
        <version>2.2.0</version>
    </dependency>
</dependencies>

<properties>
    <java.version>1.8</java.version>
</properties>

<repositories>
    <repository>
        <id>spring-milestone</id>
        <name>Spring Maven MILESTONE Repository</name>
        <url>http://repo.spring.io/libs-milestone</url>
    </repository>
    <repository>
        <id>spring-releases</id>
        <url>https://repo.spring.io/libs-release</url>
    </repository>
    <repository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/libs-snapshot</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>

<pluginRepositories>
    <pluginRepository>
        <id>spring-releases</id>
        <url>https://repo.spring.io/libs-release</url>
    </pluginRepository>
</pluginRepositories>

【问题讨论】:

看起来如果你的依赖有冲突,你能发布你的 pom.xml 吗? 【参考方案1】:

哦,我现在看到了问题

你需要添加一个构造函数

@Table(keyspace="testks", name="DataSet", caseSensitiveKeyspace=false, caseSensitiveTable=false)
public class DataSet 

@PartitionKey
@Column
String id;
@Column
String name;

public Dataset() 


public String getName() 
    return name;


public DataSet setName(String name) 
    this.name = name;
    return this;


public DataSet setId(String id)
    this.id = id;
    return this;


public DataSet(String name)
    this.id = UUIDs.timeBased().toString();
    this.name = name;


【讨论】:

以上是关于NoSuchMethodException 使用 datastax cassandra 映射器的主要内容,如果未能解决你的问题,请参考以下文章

java.lang.NoSuchMethodException 尝试使用 OncePerRequestFilter [重复]

部署期间出现 NoSuchMethodException

使用 BeanUtils.getProperty 获取属性时出现 NoSuchMethodException: Unknown property 问题分析

Appium Pagefactory 从 POM 运行时抛出 noSuchMethodException

使用 XML onClick 时出现 Android Dialog NoSuchMethodException 错误

Java 8,流过滤器,反射,NoSuchMethodException [重复]