有 Java 编程相关的问题?

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

java Spring应用程序将文档保存到测试数据库而不是自定义数据库

我有一个spring restful web服务,正在尝试保存到一个名为little data的数据库中,但我的应用程序一直保存到测试数据库中

下面是我的申请表。yml文件:

spring:
  data:
    mongodb:
    port: 27017
    uri: mongodb://127.0.0.1/little-data
    repositories:
      enabled: true
    authentication-database: admin


server:
  port: 8090

我也在我的应用程序yaml文件中尝试过:

spring:
  data:
    mongodb:
    host: 127.0.0.1
    port: 27017
    database: little-data
    repositories:
      enabled: true
    authentication-database: admin


server:
  port: 8090

以下是我的帖子模型:

import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Field;
import lombok.*;

@Data
@NoArgsConstructor
@Document(collection = "posts")

public class Post {

    @Id
    @Field("id")
    private int id;

    @Field("gameName")
    private String gameName;

    @Indexed(unique=true)
    @Field("gameGenre")
    private String gameGenre;

    public Post(int id, String game, String genre) {
        this.id = id;
        this.gameName = game;
        this.gameGenre = genre;
    }
}

所有请求都可以正常工作,但它们保存到了错误的数据库。任何帮助都将不胜感激。谢谢


共 (1) 个答案

  1. # 1 楼答案

    在您的YAML配置中,请注意hostportmongodb子级,并且与您的级别不同(如果它还不是打字错误),如下所示:

    spring:
        data:
            mongodb:
                host: 127.0.0.1
                port: 27017
                database: little-data