Fork me on GitHub

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.

Tutorial Source

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 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:

  1. Replace the Spring parameter annotations with WoOF annotations.
  2. 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.
  3. Any @PostContruct / @PreDestroy moved to ManagedObjectSource as injected object.
  4. See Transaction Tutorial for graphically configuring transactions (removing need for Spring's @Transactional).
  5. Remove the remaining Spring annotations.
  6. 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()));
	}

}

Migrating a Spring web page

The Spring web page is again taken from the previous tutorial:

@Controller
public class SpringController {

	@GetMapping("/html")
	public String html(@RequestParam String name, Model model) {
		model.addAttribute("name", name);
		return "simple";
	}
}
<html><body><p th:text="'Hello ' + ${name}" /></body></html>

To migrate the web page, can either:

The rewritten WoOF logic object and template are as follows:

public class MigratedController {

	@Value
	public static class Model {
		private String name;
	}

	public Model getTemplate(@HttpQueryParameter("name") String name) {
		return new Model(name);
	}
}
<html><body><p >Hello ${name}</p></body></html>

Next

The next tutorial covers migrating Spring Web Flux to WoOF.