Skip to main content

Migrate to Spring Boot 2 from Spring Boot 1

In this guide we'll look at using OpenRewrite to perform an automated migration from Spring Boot 1 to Spring Boot 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:

This recipe has no required configuration options. It can be activated by adding a dependency on `org.openrewrite.recipe:rewrite-spring` in your build file or by running a shell command (in which case no build changes are needed):

  1. Add the following to your build.gradle file:
    build.gradle
    plugins {
    id("org.openrewrite.rewrite") version("latest.release")
    }

    rewrite {
    activeRecipe("org.openrewrite.java.spring.boot2.UpgradeSpringBoot_2_7")
    }

    repositories {
    mavenCentral()
    }

    dependencies {
    rewrite("org.openrewrite.recipe:rewrite-spring:6.32.1")
    }
  2. Run gradle rewriteRun to run the recipe.

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.

package org.springframework.samples.petclinic.owner;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.Collection;
import java.util.Map;

import static org.springframework.web.bind.annotation.RequestMethod.*;

@Controller
class OwnerController {

private static final String VIEWS_OWNER_CREATE_OR_UPDATE_FORM = "owners/createOrUpdateOwnerForm";
private final OwnerRepository owners;

@Autowired
public OwnerController(OwnerRepository clinicService) {
this.owners = clinicService;
}

@RequestMapping("/owners/{ownerId}/edit")
public String initUpdateOwnerForm(@PathVariable("ownerId") int id, Model model) {
Owner owner = this.owners.findById(id);
model.addAttribute(owner);
return VIEWS_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()) {
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
} else {
owner.setId(ownerId);
this.owners.save(owner);
return "redirect:/owners/{ownerId}";
}
}

@RequestMapping("/owners/{ownerId}")
public ModelAndView showOwner(@PathVariable("ownerId") int ownerId) {
ModelAndView mav = new ModelAndView("owners/ownerDetails");
mav.addObject(this.owners.findById(ownerId));
return mav;
}

}

Conditional Bean Configuration.

import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SchoolConfig {
@Bean
public DogSchool dogSchool() {
return new DogSchool();
}

@Bean
public CatSchool catSchool() {
return new CatSchool();
}

@Bean
@ConditionalOnBean({CatSchool.class, DogSchool.class})
public VetSchool vetSchool() {
return new VetSchool();
}
}

Known Limitations

Unsupported FunctionalityIssue
ConditionalOnAnyBean should create new CompilationUnit when the condition is on the parent class#34
JAXRS to Spring WebMVC annotations#69
@EmbeddedKafkaRule to @EmbeddedKafka test conversion#75