Fork me on GitHub

Spring Web Flux Tutorial

This tutorial demonstrates configuring Spring Web Flux Controllers within WoOF. This enables using an existing Spring Application out of the box within WoOF.

Tutorial Source

Spring Web Flux Controller

The Spring Web Flux Controller to be integrated is as follows:

@RestController
@RequestMapping("/rest")
public class SpringRestController {

	@Autowired
	private SpringDependency dependency;

	@GetMapping
	public Mono<ResponseModel> get() {
		return Mono.just(new ResponseModel("GET " + this.dependency.getMessage()));
	}

	@GetMapping("/path/{param}")
	public Mono<ResponseModel> path(@PathVariable String param) {
		return Mono.just(new ResponseModel(param));
	}

	@PostMapping("/update")
	public Flux<ResponseModel> post(@RequestBody RequestModel request) {
		return Flux.just(new ResponseModel(request.getInput()), new ResponseModel("ANOTHER"));
	}

}

With the request and response models:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class RequestModel {

	private String input;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ResponseModel {

	private String message;
}

Configuring Spring Web Flux Controller

To configure using Spring Web Flux Controller instances within WoOF, add the following dependency:

		<dependency>
			<groupId>net.officefloor.spring</groupId>
			<artifactId>officespring_webflux</artifactId>
		</dependency>

Spring will also need to be configured in application.objects:

<objects>
	
	<supplier source="SPRING">
		<property name="configuration.class" value="net.officefloor.tutorial.springwebfluxhttpserver.Application" />
	</supplier>

</objects>

This will have all the Spring Web Flux Controllers available at their request mapped paths.

Testing

The following tests demonstrate the Spring Web Flux Controllers servicing requests.

	@RegisterExtension
	public static final MockWoofServerExtension server = new MockWoofServerExtension();

	@Test
	public void get() {
		MockWoofResponse response = server.send(MockWoofServer.mockRequest("/rest"));
		response.assertJson(200, new ResponseModel("GET Spring Dependency"));
	}

	@Test
	public void pathParam() {
		MockWoofResponse response = server.send(MockWoofServer.mockRequest("/rest/path/parameter"));
		response.assertJson(200, new ResponseModel("parameter"));
	}

	@Test
	public void post() {
		MockWoofResponse response = server
				.send(MockWoofServer.mockJsonRequest(HttpMethod.POST, "/rest/update", new RequestModel("INPUT")));
		response.assertJson(200, new ResponseModel[] { new ResponseModel("INPUT"), new ResponseModel("ANOTHER") });
	}

Next

The next tutorial covers configuring a Spring Web Flux Controller as a procedure.