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
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With