In this guide we'll look at using OpenRewrite to perform an automated migration from SpringBoot 1 to SpringBoot 2.
Example Configuration
The UpgradeSpringBoot_2_7 recipe has no required configuration options and can be activated directly after taking a dependency on rewrite-spring in your build file:
At this point, you're ready to execute the migration by running mvn rewrite:run or gradlew rewriteRun. After running the migration you can inspect the results with git diff (or equivalent), manually fix anything that wasn't able to be migrated automatically, and commit the results.
Before and After
For the full list of changes this recipe will make, see its reference page.
Request Mapping Annotations.
packageorg.springframework.samples.petclinic.owner;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Controller;importorg.springframework.ui.Model;importorg.springframework.validation.BindingResult;importorg.springframework.web.bind.WebDataBinder;importorg.springframework.web.bind.annotation.*;importorg.springframework.web.servlet.ModelAndView;importjavax.validation.Valid;importjava.util.Collection;importjava.util.Map;importstaticorg.springframework.web.bind.annotation.RequestMethod.*;@ControllerclassOwnerController{privatestaticfinalStringVIEWS_OWNER_CREATE_OR_UPDATE_FORM="owners/createOrUpdateOwnerForm";privatefinalOwnerRepositoryowners;@AutowiredpublicOwnerController(OwnerRepositoryclinicService) {this.owners=clinicService; }@RequestMapping("/owners/{ownerId}/edit")publicStringinitUpdateOwnerForm(@PathVariable("ownerId") intid,Modelmodel) {Ownerowner=this.owners.findById(id);model.addAttribute(owner);returnVIEWS_OWNER_CREATE_OR_UPDATE_FORM; }@RequestMapping(value="/owners/{ownerId}/edit",method=POST) public String processUpdateOwnerForm(@Valid Owner owner, BindingResult result, @PathVariable("ownerId") int ownerId) {
if (result.hasErrors()) {returnVIEWS_OWNER_CREATE_OR_UPDATE_FORM; } else {owner.setId(ownerId);this.owners.save(owner);return"redirect:/owners/{ownerId}"; } }@RequestMapping("/owners/{ownerId}")publicModelAndViewshowOwner(@PathVariable("ownerId") intownerId) {ModelAndViewmav=newModelAndView("owners/ownerDetails");mav.addObject(this.owners.findById(ownerId));returnmav; }}