有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在Java中使用非字符串值创建MongoDB文档

我一辈子都不能用非字符串的值在Java中创建MongoDB文档。我尝试过使用“put”和“append”方法,这两种方法都会返回与下面类似的错误

BasicDBObject doc = new BasicDBObject();
doc.put("foo", "bar"); // this works
doc.put("ugh", 5); // this doesn't
doc.put("plz", 7L); // nor does this

// [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.0.2:compile (default-compile) on project league: Compilation failure
// [ERROR] /home/calvin/league/src/main/java/gg/bram/league/App.java:[60,19] error: no suitable method found for put(String,int)
// Relevant bit: "error: no suitable method found for put(String,int)"

java -version的输出:

java version "1.7.0_65"
OpenJDK Runtime Environment (IcedTea 2.5.1) (7u65-2.5.1-5~deb7u1)
OpenJDK 64-Bit Server VM (build 24.65-b04, mixed mode)

使用mongo java驱动程序2.12.4,在pom中进行验证。xml文件:

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

这让我很难受,我做的和我能在网上找到的所有教程/文档没有什么不同,请帮忙

谢谢

编辑:我的整个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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>gg.bram.league</groupId>
  <artifactId>league</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>league</name>
  <url>http://maven.apache.org</url>


  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.json</groupId>
      <artifactId>json</artifactId>
      <version>20140107</version>
    </dependency>

    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpclient</artifactId>
      <version>4.3.6</version>
    </dependency>

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


  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-jar-plugin</artifactId>
          <version>2.5</version>
        </plugin>
      </plugins>
    </pluginManagement>

    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.5</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <mainClass>gg.bram.league.App</mainClass>
              <classpathMavenRepositoryLayout>true</classpathMavenRepositoryLayout>
              <classpathPrefix>/home/calvin/.m2/repository/</classpathPrefix>
            </manifest>
          </archive>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

共 (1) 个答案

  1. # 1 楼答案

    你把你的来源和目标设定在错误的地方;他们应该在maven-compiler-plugin