Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring app saving documents to test db instead of custom db

I have a spring restful web service and am trying to save to a database named little-data but my app keeps saving to the test database instead.

Below is my application.yml file:

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


server:
  port: 8090

I have also tried this for my application yaml file:

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


server:
  port: 8090

And here is my model for posts:

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;
    }
}

All of the requests work fine but they save to the wrong database. Any help would be appreciated. Thanks

like image 990
RyFasch Avatar asked Mar 05 '18 00:03

RyFasch


2 Answers

In your YAML config, please notice that host and port are children of mongodb and not in the same level as you do (if it's not already a typo), like this:

spring:
    data:
        mongodb:
            host: 127.0.0.1
            port: 27017
            database: little-data
like image 106
Laabidi Raissi Avatar answered Oct 23 '22 11:10

Laabidi Raissi


Do you use profiles? A possible application.yaml could look like this:

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

As Laabidi Raissi said, it be careful to get the indentation right.

like image 32
Daniel Avatar answered Oct 23 '22 09:10

Daniel