【pom.xml】
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-webflux</artifactId><version>2.3.12.RELEASE</version>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId><version>2.3.12.RELEASE</version>
</dependency>
【application.properties】
server.port=8081
【MyWebFluxTest.java】
package com.chz.myWebFlux;@SpringBootApplication
public class MyWebFluxTest {public static void main(String[] args) {SpringApplication.run(MyWebFluxTest.class, args);}
}
【HelloWebFluxController.java】
package com.chz.myWebFlux.controller;@RestController
public class HelloWebFluxController {@GetMapping("/hello")public String hello() {return "Hello, WebFlux !";}@GetMapping("/hello2")public Mono<String> hello2() {return Mono.just("Hello, WebFlux 2!");}@GetMapping("/user")public Mono<User> getUser() {User user = new User();user.setName("chz");user.setDesc("chz_desc");return Mono.just(user);}
}
【User.java】
package com.chz.myWebFlux.entity;@Getter
@Setter
public class User {private String name;private String desc;
}
启动【MyWebFluxTest】
访问【http://localhost:8081/hello】,正常显示
访问【http://localhost:8081/hello2】,正常显示
访问【http://localhost:8081/user】,正常显示