Spring boot + MongoDB - localhost show whitelabel error

时间:2017-08-07 14:34:16

标签: java mongodb maven spring-boot localhost

所以我知道还有其他类似的问题,但我认为我的问题无法解决。

希望你们其中一个人能看出我做错了什么。

当我去localhost //:8080时,我得到“whitelabel error ....”

我的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>com.mycompany</groupId>
    <artifactId>mavenproject1</artifactId>
    <version>1.0-SNAPSHOT</version>  
    <packaging>war</packaging>

    <name>mavenproject1</name>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
    </parent>

    <properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
        <!--Spring--> 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.3.10.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.10.RELEASE</version>
        </dependency>
        <!--MONGO-->
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongo-java-driver</artifactId>
            <version>2.11.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-mongodb</artifactId>
            <version>1.10.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>2.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>2.4.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>          
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</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>
            <!--skal dette bruges?-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.1</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${endorsed.dir}</outputDirectory>
                            <silent>true</silent>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>javax</groupId>
                                    <artifactId>javaee-endorsed-api</artifactId>
                                    <version>6.0</version>
                                    <type>jar</type>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

我的应用程序类看起来像这样:

package com.example.mavenproject1;

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;

/**
 *
 * @author Steffen
 */
@RestController
@EnableAutoConfiguration
@ComponentScan
public class Application {

    //Run a function here which dives into the mongoDB and returns the info?
    @RequestMapping("/")
    String home() {

        DBPopulator dbp = new DBPopulator();

        dbp.saveNew();


//        return dbp.getFil();

        return "Hey wassup";
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

DBPopulator:

package com.example.mavenproject1;

import java.util.Date;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;


import org.springframework.context.support.GenericXmlApplicationContext;


/**
 *
 * @author Steffen
 */
public class DBPopulator {

    ApplicationContext ctx = new AnnotationConfigApplicationContext(ConnectToDB.class);
    MongoOperations mongoOperation = (MongoOperations) ctx.getBean("mongoTemplate");

    private int vers = 0;

    public void saveNew() {
        Fil f = new Fil("fil" + vers + "", new MetaData(new Date(), new Date(), 5));
        vers++;
        mongoOperation.save(f);
    };

    public String getFil(){
    // query to search user
        Query searchQuery = new Query(Criteria.where("filNavn").is("fil1"));

        Fil savedF = mongoOperation.findOne(searchQuery, Fil.class);

        List<Fil> listFiler = mongoOperation.findAll(Fil.class);



        System.out.println("Here is my file: " + savedF);

        return "File: " + savedF;
    } 
}

现在我的应用程序“正常工作”,因为它将新的“Fil”保存到我的mongoDB中。我想在Application类的“Home”函数中返回它们,但到目前为止我除了“whitelabel error”之外什么也得不到...

提前致谢!

**修改

文件结构如下。 enter image description here

来自localhost //:8080的完整错误是:

Whitelabel错误页面

此应用程序没有/ error的显式映射,因此您将此视为回退。 Mon Aug 07 16:39:18 CEST 2017 出现意外错误(type = Internal Server Error,status = 500)。 COM / mongodb的/ BulkWriteException

1 个答案:

答案 0 :(得分:2)

看起来它正在寻找可从2.12 mongo驱动程序版本获得的BulkWriteException

解决方案1 ​​

至少升级到

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

解决方案2(更好)

删除

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

并保持

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

引入正确的依赖关系,即2.4.x mongo驱动程序。

解决方案3(最佳)

删除

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

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

添加

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

拉出兼容的spring mongo和mongo依赖,这是2.4.x mongo驱动程序。