Skip to content

Commit

Permalink
+
Browse files Browse the repository at this point in the history
  • Loading branch information
Relucent committed Jun 18, 2020
1 parent 94006d5 commit 7b4321e
Show file tree
Hide file tree
Showing 8 changed files with 256 additions and 0 deletions.
51 changes: 51 additions & 0 deletions spring-boot-example-webflux/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Compile files
*.class

# Package Files
*.jar
*.war
*.ear
*.nar
*.zip
*.tar
*.tar.gz
*.rar

# Maven
target/
*.ser
*.ec

# J2ME
.mtj.tmp/

# Eclipse
.classpath
.project
.settings/
.metadata/
.factorypath
.springBeans

# IntelliJ Idea
.idea/
out/
*.ipr
*.iws
*.iml

# BlueJ Files
*.ctxt

# OS X
.DS_Store

# Window
Thumbs.db

# Virtual Machine Crash Logs
hs_err_pid*

# Log File
*.log
*.log.*
57 changes: 57 additions & 0 deletions spring-boot-example-webflux/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?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>

<groupId>yyl</groupId>
<artifactId>spring-boot-example-webflux</artifactId>
<version>0.0.1-SNAPSHOT</version>

<name>spring-boot-example-webflux</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>

<!-- _spring-boot-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

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

<!-- _test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<!--<([mvn clean package])> -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package yyl.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootWebfluxApplication {

public static void main(String[] args) {
SpringApplication.run(SpringBootWebfluxApplication.class, args);
System.out.println("startup success");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package yyl.springboot.controller;

import java.time.Duration;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
* |~ http://localhost:8080/
*/
@RestController
public class HelloWebFluxController {

@GetMapping("/hello")
public String hello() {
return "Hello, WebFlux !";
}

@GetMapping("/mono")
public Mono<String> mono() {
return Mono.just("mono");
}

@GetMapping("/flux")
public Flux<String> flux() {
return Flux.just("hello", "webflux", "spring", "boot");
}

@GetMapping(path = "/stream", produces = MediaType.APPLICATION_STREAM_JSON_VALUE)
public Flux<String> stream() {
return Flux.range(1, 9).delayElements(Duration.ofSeconds(1)).map(String::valueOf);
}
}
Empty file.
30 changes: 30 additions & 0 deletions spring-boot-example-webflux/src/main/resources/static/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello</title>
<style type="text/css">
.layout-side {
position: fixed
}

.layout-body {
position: absolute;
width: 60%;
height: 80%;
left: 30%;
}
</style>
</head>
<body>
<h1>Hello Spring Boot</h1>
<ul class="layout-side">
<li><a href="about:blank" target="frame">about:blank</a></li>
<li><a href="/hello" target="frame">/hello</a></li>
<li><a href="/mono" target="frame">/mono</a></li>
<li><a href="/flux" target="frame">/flux</a></li>
<li><a href="/stream" target="_black">/stream</a></li>
</ul>
<iframe class="layout-body" name="frame" src="about:blank"> </iframe>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package yyl.springboot;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootWebfluxApplicationTests {
@Test
public void contextLoads() {
System.out.println("Hello Webflux!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package yyl.springboot.controller;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockServletContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = MockServletContext.class)
public class HelloWebFluxControllerTests {

private MockMvc mvc;

@Before
public void setUp() throws Exception {
mvc = MockMvcBuilders.standaloneSetup(new HelloWebFluxController()).build();
}

@Test
public void helloTest() throws Exception {
perform("/hello", MediaType.APPLICATION_JSON);
}

@Test
public void monoTest() throws Exception {
perform("/mono", MediaType.APPLICATION_JSON);
}

@Test
public void fluxTest() throws Exception {
perform("/flux", MediaType.APPLICATION_JSON);
}

@Test
public void streamTest() throws Exception {
perform("/stream", MediaType.APPLICATION_STREAM_JSON);
}

private void perform(String uri, MediaType mediaType) throws Exception {
mvc.perform(MockMvcRequestBuilders.get(uri).accept(mediaType))//
.andExpect(MockMvcResultMatchers.status().isOk())//
.andDo(MockMvcResultHandlers.print())//
.andReturn();
}
}

0 comments on commit 7b4321e

Please sign in to comment.