WebFlux 构建的 REST 接口应用,使用 Redis 作为存储
dependencies {
implementation('org.springframework.boot:spring-boot-starter-webflux')
implementation('org.springframework.boot:spring-boot-starter-data-redis-reactive')
implementation('org.projectlombok:lombok')
testImplementation('org.springframework.boot:spring-boot-starter-test')
testImplementation('io.projectreactor:reactor-test')
}
- Model
@Data
@ToString
@Builder
@NoArgsConstructor
@AllArgsConstructor
@RedisHash("posts")
public class Post {
@Id
private String id;
private String title;
private String content;
}
- REST 接口
@RestController
@RequestMapping("/posts")
public class PostController {
@Autowired
private PostRepository postRepository;
@GetMapping("")
public Flux<Iterable<Post>> list() {
return Flux.just(postRepository.findAll());
}
}
- Repository
public interface PostRepository extends KeyValueRepository<Post, String> {
}
- 初始化数据
@Component
@Slf4j
public class DataInitializer implements CommandLineRunner {
@Autowired
private PostRepository postRepository;
@Override
public void run(String... args) throws Exception {
postRepository.deleteAll();
Stream.of("Post One", "Post Two")
.forEach(title -> postRepository.save(
Post.builder()
.id(UUID.randomUUID().toString())
.title(title)
.content("Content of " + title)
.build()
));
}
}
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=123456
- 启动应用
- 获取列表
curl --request GET \
--url http://localhost:8080/posts