我正在观看一个课程,我们正在使用MongoDB Cluster(atlas(创建一个带有SpringWebflux的API Rest。这门课程已经过时了,因为老师使用的是mLab,现在它是MongoDB的一部分。
好吧,我得到了连接,没有任何错误,但当我试图添加集合";播放列表";有些字符串不起作用,我的控制台不会显示任何错误。
这是我运行应用程序时的控制台
https://i.stack.imgur.com/phHPJ.jpg
代码是:
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);
}
}
WebfluxApplication.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);
}
}
播放列表.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;
}
}
播放列表存储库.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> {
}
您的DummyData
类中似乎缺少一个@Component
。如果spring找不到命令行runner,它就不会启动。您的代码的其余部分看起来很好。