gpt4 book ai didi

java - Spring boot + MongoDB - localhost 显示白标错误

转载 作者:行者123 更新时间:2023-12-02 02:40:31 25 4
gpt4 key购买 nike

所以我知道还有其他类似的问题,但我看不到解决我的问题的方法。

希望你们中的一个人能够看到我做错了什么。

当我访问 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 的完整错误是:

白标错误页面

此应用程序没有/error 的显式映射,因此您将其视为后备。2017 年欧洲夏令时间 8 月 7 日星期一 16:39:18出现意外错误(类型=内部服务器错误,状态=500)。com/mongodb/BulkWriteException

最佳答案

看起来它正在寻找 BulkWriteException从 2.12 mongo 驱动程序版本开始可用。

解决方案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 驱动程序。

关于java - Spring boot + MongoDB - localhost 显示白标错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45549486/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com