This tutorial demonstrates configuring Spring Web MVC Controllers within WoOF. This enables using an existing Spring Application out of the box within WoOF.
The Spring Web MVC Controller to be integrated is as follows:
@RestController
@RequestMapping("/rest")
public class SpringRestController {
@Autowired
private SpringDependency dependency;
@GetMapping
public ResponseModel get() {
return new ResponseModel("GET " + this.dependency.getMessage());
}
@GetMapping("/path/{param}")
public ResponseModel path(@PathVariable("param") String param) {
return new ResponseModel(param);
}
@PostMapping("/update")
public ResponseModel post(@RequestBody RequestModel request) {
return new ResponseModel(request.getInput());
}
}
With the request and response models:
@Data
@NoArgsConstructor
@AllArgsConstructor
public class RequestModel {
private String input;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ResponseModel {
private String message;
}
To configure using Spring Web MVC Controller instances within WoOF, add the following dependency:
<dependency> <groupId>net.officefloor.spring</groupId> <artifactId>officespring_webmvc</artifactId> </dependency>
Spring will also need to be configured in officefloor/suppliers/Spring.yml:
source: SPRING properties: configuration.class: net.officefloor.tutorial.springwebmvchttpserver.Application
The Spring Web MVC Controllers are then configured as REST endpoints. The following configures the GET rest endpoint:
get: resource: net.officefloor.tutorial.springwebmvchttpserver.SpringRestController procedure: SpringWebMvcController method: get
The path parameter endpoint:
path: resource: net.officefloor.tutorial.springwebmvchttpserver.SpringRestController procedure: SpringWebMvcController method: path
The PUT endpoint:
post: resource: net.officefloor.tutorial.springwebmvchttpserver.SpringRestController procedure: SpringWebMvcController method: post
The HTML page endpoint:
html: resource: net.officefloor.tutorial.springwebmvchttpserver.SpringController procedure: SpringWebMvcController method: html
Integration of Spring Web MVC Controllers is not limited to only REST. The following Spring Web MVC Controller:
@Controller
public class SpringController {
@GetMapping("/html")
public String html(@RequestParam("name") String name, Model model) {
model.addAttribute("name", name);
return "simple";
}
}
Provides the following web page:
<html><body><p th:text="'Hello ' + ${name}" /></body></html>
The following tests demonstrate the Spring Web MVC 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("INPUT"));
}
@Test
public void html() {
MockWoofResponse response = server.send(MockWoofServer.mockRequest("/html?name=Daniel"));
response.assertResponse(200, "<html><body><p >Hello Daniel</p></body></html>");
}
The next tutorial covers configuring a Spring Web MVC Controller as a procedure.