SpringBoot-2.X 學習筆記10 整合 WebFlux

1 添加依賴

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>

啓動項目如果控制檯打印出 Netty started on port(s): 8080 說明 WebFlux 沒有采用Tomcat而是採用了Netty,項目啓動成功。


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.9.RELEASE)

2019-10-12 14:39:24.759  INFO 8296 --- [  restartedMain] com.xu.springboot.WebFluxApplication     : Starting WebFluxApplication on ZWJ0R16WWO114LL with PID 8296 (E:\SourceCode\Eclipse-2019-06\WebFlux\target\classes started by Administrator in E:\SourceCode\Eclipse-2019-06\WebFlux)
2019-10-12 14:39:24.765  INFO 8296 --- [  restartedMain] com.xu.springboot.WebFluxApplication     : No active profile set, falling back to default profiles: default
2019-10-12 14:39:24.901  INFO 8296 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2019-10-12 14:39:24.901  INFO 8296 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2019-10-12 14:39:30.030  INFO 8296 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2019-10-12 14:39:31.883  INFO 8296 --- [  restartedMain] o.s.b.web.embedded.netty.NettyWebServer  : Netty started on port(s): 8080
2019-10-12 14:39:31.890  INFO 8296 --- [  restartedMain] com.xu.springboot.WebFluxApplication     : Started WebFluxApplication in 8.078 seconds (JVM running for 9.093)

2 測試代碼

2.1 Service 代碼

package com.xu.springboot.service;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.xu.springboot.entity.Student;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@Service
public class StudentService {
	
	private static final Map<String, Student> MAP = new HashMap<String, Student>();
	
	static {//未連連接數據庫時的測試代碼,在項目中可以連接數據庫
		Student student1=new Student();
		student1.setId(1);
		student1.setName("張三");
		MAP.put("1", student1);
		Student student2=new Student();
		student2.setId(2);
		student2.setName("李四");
		MAP.put("2", student2);
		Student student3=new Student();
		student3.setId(3);
		student3.setName("王五");
		MAP.put("3", student3);
		Student student4=new Student();
		student4.setId(1);
		student4.setName("趙六");
		MAP.put("4", student4);
	}
	
	public Flux<Object> list() {
		Collection<Student> list=StudentService.MAP.values();
		return Flux.fromIterable(list);
	}
	
	public Mono<Object> getById(String id) {
		return Mono.justOrEmpty(StudentService.MAP.get(id));
	}
	
	public Mono<Object> delById(String id) {
		return Mono.justOrEmpty(StudentService.MAP.remove(id));
	}
	
}

2.2 Controller 代碼

package com.xu.springboot.controller;

import java.time.Duration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xu.springboot.service.StudentService;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@RestController
@RequestMapping("/webflux")
public class WebFluxContriller {
	
	@Autowired
	private StudentService service;

	@RequestMapping(value = "/fluxdaly",produces = MediaType.APPLICATION_STREAM_JSON_VALUE)
	public Flux<Object> fluxdaly() {
		return service.list().delayElements(Duration.ofSeconds(2));
	}
	
	@RequestMapping(value = "/flux")
	public Flux<Object> flux() {
		return service.list().delayElements(Duration.ofSeconds(2));
	}

	@RequestMapping("/mono")
	public Mono<Object> mono(String id) {
		return service.getById(id);
	}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章