I'm watching a course that's we are creating an API Rest with Spring Webflux with MongoDB Cluster (atlas). The course is outdates because the teacher was used the mLab that's now is a part of the MongoDB.
Well, I got the connection without any erros but when I try to add the collection "playlist" with some strings that's not working and my console dont show any erros.
Here is my console when I run the application
There's the code:
DummyData.java
package com.apirest.webflux;
import java.util.UUID;
import org.springframework.boot.CommandLineRunner;
import com.apirest.webflux.document.Playlist;
import com.apirest.webflux.repository.PlaylistRepository;
import reactor.core.publisher.Flux;
public class DummyData implements CommandLineRunner {
private final PlaylistRepository playlistRepository;
DummyData(PlaylistRepository playlistRepository) {
this.playlistRepository = playlistRepository;
}
@Override
public void run(String... args) throws Exception {
playlistRepository.deleteAll().thenMany(Flux.just("API REST Spring Boot", "Deploy de uma aplicação java no IBM Cloud", "Java 8",
"Github","Spring Security", "Web Service RESTFULL", "Bean no Spring Framework")
.map(nome -> new Playlist(UUID.randomUUID().toString(), nome)).flatMap(playlistRepository::save))
.subscribe(System.out::println);
}
}
WebfluxAppllication.java
package com.apirest.webflux;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class WebfluxApplication {
public static void main(String[] args) {
SpringApplication.run(WebfluxApplication.class, args);
}
}
Playlist.java
package com.apirest.webflux.document;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document
public class Playlist {
public Playlist(String id, String nome) {
super();
this.id = id;
this.nome = nome;
}
@Id
private String id;
private String nome;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
}
PlaylistRepository.java
package com.apirest.webflux.repository;
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
import com.apirest.webflux.document.Playlist;
public interface PlaylistRepository extends ReactiveMongoRepository<Playlist, String> {
}
You seem go be missing an @Component
on your DummyData
class. The command line runner is not started if spring cannot find it. The rest of your code looks fine.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.