Migrate Spring Web MVC Tutorial
This tutorial provides the typical steps in migrating a Spring Web MVC Controller to avoid dependency on Spring. It also enables simpler code that can take advantage of all the OfficeFloor features.
Steps to migrate a Spring Web MVC Controller
The Spring Web MVC Controller to be migrated is taken from the previous tutorial:
@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());
}
}
To migrate a Spring Web MVC Controller:
- Replace the Spring parameter annotations with WoOF annotations.
- @PathVariable to @HttpPathParameter
- @RequestParam to @HttpQueryParameter
- @RequestHeader to @HttpHeaderParameter
- @CookieValue to @HttpCookieParameter
- @RequestBody to @HttpObject
- Remove @ResponseBody and send response object to ObjectResponse parameter rather than returning it.
- Note that can also continue to return object. This object is then used as a parameter to the next linked procedure.
- Any @PostContruct / @PreDestroy moved to ManagedObjectSource as injected object.
- See Transaction Tutorial for configuring transactions via YAML (removing need for Spring's @Transactional).
- Remove the remaining Spring annotations.
- Move dependencies to parameters of the method.
The resulting migrated code is as follows:
public class MigratedRestController {
public void get(SpringDependency dependency, ObjectResponse<ResponseModel> response) {
response.send(new ResponseModel("GET " + dependency.getMessage()));
}
public void path(@HttpPathParameter("param") String param, ObjectResponse<ResponseModel> response) {
response.send(new ResponseModel(param));
}
public void post(RequestModel request, ObjectResponse<ResponseModel> response) {
response.send(new ResponseModel(request.getInput()));
}
}
Next
The next tutorial covers migrating Spring Web Flux to WoOF.

