深入学习java笔记-15.SpringBoot2.1之WebFlux

pom.xml


    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.4.RELEASE
         
    
    com.ctgu
    springwebflux
    0.0.1-SNAPSHOT
    springwebflux
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-activemq
        
        
            
            
        
        
            
            
        
        
            org.springframework.boot
            spring-boot-starter-data-redis
        
        
            org.apache.commons
            commons-pool2
            2.6.2
        
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
        
            org.springframework.boot
            spring-boot-starter-webflux
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
            org.springframework.boot
            spring-boot-devtools
            true
        
        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        
        
            org.projectlombok
            lombok
            true
        
        
            org.apache.activemq
            activemq-pool
            5.15.9
        
        
            org.messaginghub
            pooled-jms
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            io.projectreactor
            reactor-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


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 map = new HashMap(){{
       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 list(){
        Collection list = map.values();
        return Flux.fromIterable(list);
    }

    public Mono getById(final String id){
        return Mono.justOrEmpty(map.get(id));
    }

    public Mono 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 test(){
        return Mono.just("hello webflux");
    }

    @GetMapping("find")
    public Mono findById(final String id){
        return userService.getById(id);
    }

    @GetMapping(value = "find_all",produces = MediaType.APPLICATION_STREAM_JSON_VALUE)
    public Flux 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 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 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());
    }

}

你可能感兴趣的:(深入学习java笔记-15.SpringBoot2.1之WebFlux)