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 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 application.objects:
<objects> <supplier source="SPRING"> <property name="configuration.class" value="net.officefloor.tutorial.springwebmvchttpserver.Application" /> </supplier> </objects>
This will have all the Spring Web MVC Controllers available at their request mapped paths.
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 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.