【问题标题】:Spring app saving documents to test db instead of custom dbSpring 应用程序将文档保存到测试数据库而不是自定义数据库
【发布时间】:2018-08-12 14:33:24
【问题描述】:

我有一个 Spring RESTful Web 服务,并试图保存到名为 little-data 的数据库,但我的应用程序一直保存到测试数据库。

下面是我的 application.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;
    }
}

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

【问题讨论】:

    标签: java spring mongodb yaml


    【解决方案1】:

    您使用个人资料吗?一个可能的 application.yaml 可能如下所示:

    spring:
        profiles:
            active: production
    ---
    spring:
        profiles: development
        data:
            mongodb:
                host: 127.0.0.1
                port: 27017
                database: test-db
    ---
    spring:
        profiles: production
        data:
            mongodb:
                host: 127.0.0.1
                port: 27017
                database: little-data
    

    正如Laabidi Raissi所说,要注意缩进。

    【讨论】:

      【解决方案2】:

      在您的 YAML 配置中,请注意 hostportmongodb子项,并且与您不在同一级别(如果还不是错字),例如这个:

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

      【讨论】:

        猜你喜欢
        • 2018-10-18
        • 2014-06-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-02
        • 1970-01-01
        相关资源
        最近更新 更多