pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ctgu</groupId>
<artifactId>springwebflux</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springwebflux</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<!--<dependency>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-data-elasticsearch</artifactId>-->
<!--</dependency>-->
<!--<dependency>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-data-mongodb</artifactId>-->
<!--</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
<version>5.15.9</version>
</dependency>
<dependency>
<groupId>org.messaginghub</groupId>
<artifactId>pooled-jms</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml
spring:
# servlet:
# multipart:
# max-file-size: 20MB
# max-request-size: 20MB
# profiles:
# active: test
devtools:
restart:
exclude: application.yml
# trigger-file: trigger.txt
banner:
location: banner.txt
# data:
# elasticsearch:
# cluster-name: elasticsearch
# cluster-nodes: 127.0.0.1:9301
# repositories:
# enabled: true
# freemarker:
# cache: false
# charset: UTF-8
# allow-request-override: false
# check-template-location: true
# content-type: text/html
# expose-request-attributes: true
# expose-session-attributes: true
# suffix: .ftl
# template-loader-path: classpath:/templates/
# datasource:
# driver-class-name: com.mysql.jdbc.Driver
# url: jdbc:mysql://localhost:3306/spring_boot_mybatis?useUnicode=true&characterEncoding=utf8&useSSL=false
# username: root
# password: jsan456789
# type: com.zaxxer.hikari.HikariDataSource
redis:
database: 0
host: 127.0.0.1
port: 6379
timeout: 3000
lettuce:
pool:
maxIdle: 200
min-idle: 200
max-active: 2000
max-wait: 1000
# jms:
# pub-sub-domain: true
activemq:
broker-url: tcp://localhost:61616
user: admin
password: admin
in-memory: true
pool:
enabled: true
max-connections: 100
web:
file:
path: C:/Users/Jay/Desktop/
logging:
config: classpath:logback-spring.xml
server:
port: 8081
test:
name: CT
domain: Domain
JsonData.java
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import java.io.Serializable;
@Builder
@Data
@AllArgsConstructor
public class JsonData implements Serializable {
private static final long serialVersionUID = -2730031088089574823L;
private int code;
private Object data;
private String msg;
public JsonData(int code, String msg) {
this.code = code;
this.msg = msg;
}
public JsonData(int code, Object data) {
this.code = code;
this.data = data;
}
}
User.java
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private Integer id;
private String name;
}
UserService.java
import com.ctgu.springwebflux.domain.User;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@Service
public class UserService {
private static final Map<String, User> map = new HashMap<String, User>(){{
put("1", new User(1, "CT"));
put("2", new User(2, "CT"));
put("3", new User(3, "CT"));
put("4", new User(4, "CT"));
put("5", new User(5, "CT"));
put("6", new User(6, "CT"));
put("7", new User(7, "CT"));
put("8", new User(8, "CT"));
}};
public Flux<User> list(){
Collection<User> list = map.values();
return Flux.fromIterable(list);
}
public Mono<User> getById(final String id){
return Mono.justOrEmpty(map.get(id));
}
public Mono<User> deleteById(final String id){
return Mono.justOrEmpty(map.remove(id));
}
}
UserController.java
import com.ctgu.springwebflux.domain.User;
import com.ctgu.springwebflux.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.time.Duration;
@RestController
@RequestMapping("/api/user")
public class UserController {
// @Autowired
// private UserService userService;
private final UserService userService;
public UserController(final UserService userService){
this.userService = userService;
}
@GetMapping("test")
public Mono<String> test(){
return Mono.just("hello webflux");
}
@GetMapping("find")
public Mono<User> findById(final String id){
return userService.getById(id);
}
@GetMapping(value = "find_all",produces = MediaType.APPLICATION_STREAM_JSON_VALUE)
public Flux<User> findAll(){
return userService.list().delayElements(Duration.ofSeconds(1));
}
}
SpringWebFluxApplicationTests.java
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringWebFluxApplicationTests {
@Test
public void test1() {
Mono<String> bodyMono = WebClient.create().get()
.uri("http://localhost:8081/api/user/find_all")
.accept(MediaType.APPLICATION_STREAM_JSON)
.retrieve().bodyToMono(String.class);
System.out.println(bodyMono.block());
}
@Test
public void test2() {
Mono<String> bodyMono = WebClient.create().get()
.uri("http://localhost:8081/api/user/find?id={id}", 1)
.accept(MediaType.APPLICATION_JSON)
.retrieve().bodyToMono(String.class);
System.out.println(bodyMono.block());
}
}
网友评论