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):
- Gradle
- Gradle init script
- Gradle init script (Kotlin)
- Maven POM
- Maven Command Line
- Moderne CLI
- Add the following to your
build.gradlefile:build.gradleplugins {
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")
} - Run
gradle rewriteRunto run the recipe.
- Create a file named
init.gradlein the root of your project.init.gradleinitscript {
repositories {
maven { url "https://plugins.gradle.org/m2" }
}
dependencies { classpath("org.openrewrite:plugin:7.34.0") }
}
rootProject {
plugins.apply(org.openrewrite.gradle.RewritePlugin)
dependencies {
rewrite("org.openrewrite.recipe:rewrite-spring:6.32.1")
}
rewrite {
activeRecipe("org.openrewrite.java.spring.boot2.UpgradeSpringBoot_2_7")
}
afterEvaluate {
if (repositories.isEmpty()) {
repositories {
mavenCentral()
}
}
}
} - Run the recipe.shell
gradle --init-script init.gradle rewriteRun
- Create a file named
init.gradle.ktsin the root of your project.init.gradle.ktsinitscript {
repositories {
maven { url = uri("https://plugins.gradle.org/m2") }
}
dependencies { classpath("org.openrewrite:plugin:7.34.0") }
}
rootProject {
plugins.apply(org.openrewrite.gradle.RewritePlugin::class.java)
dependencies {
add("rewrite", "org.openrewrite.recipe:rewrite-spring:6.32.1")
}
extensions.configure<org.openrewrite.gradle.RewriteExtension> {
activeRecipe("org.openrewrite.java.spring.boot2.UpgradeSpringBoot_2_7")
}
afterEvaluate {
if (repositories.isEmpty()) {
repositories {
mavenCentral()
}
}
}
} - Run the recipe.shell
gradle --init-script init.gradle.kts rewriteRun
- Add the following to your
pom.xmlfile:pom.xml<project>
<build>
<plugins>
<plugin>
<groupId>org.openrewrite.maven</groupId>
<artifactId>rewrite-maven-plugin</artifactId>
<version>6.41.0</version>
<configuration>
<activeRecipes>
<recipe>org.openrewrite.java.spring.boot2.UpgradeSpringBoot_2_7</recipe>
</activeRecipes>
</configuration>
<dependencies>
<dependency>
<groupId>org.openrewrite.recipe</groupId>
<artifactId>rewrite-spring</artifactId>
<version>6.32.1</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project> - Run
mvn rewrite:runto run the recipe.
You will need to have Maven installed on your machine before you can run the following command.
mvn -U org.openrewrite.maven:rewrite-maven-plugin:run --define rewrite.recipeArtifactCoordinates=org.openrewrite.recipe:rewrite-spring:RELEASE --define rewrite.activeRecipes=org.openrewrite.java.spring.boot2.UpgradeSpringBoot_2_7
You will need to have configured the Moderne CLI on your machine before you can run the following command.
mod run . --recipe UpgradeSpringBoot_2_7
If the recipe is not available locally, then you can install it using:
mod config recipes jar install org.openrewrite.recipe:rewrite-spring:6.32.1
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.
- Before
- After
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;
}
}
package org.springframework.samples.petclinic.owner;
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;
public OwnerController(OwnerRepository clinicService) {
this.owners = clinicService;
}
@GetMapping("/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;
}
@PostMapping("/owners/{ownerId}/edit")
public String processUpdateOwnerForm(@Valid Owner owner, BindingResult result, @PathVariable int ownerId) {
if (result.hasErrors()) {
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
} else {
owner.setId(ownerId);
this.owners.save(owner);
return "redirect:/owners/{ownerId}";
}
}
@GetMapping("/owners/{ownerId}")
public ModelAndView showOwner(@PathVariable int ownerId) {
ModelAndView mav = new ModelAndView("owners/ownerDetails");
mav.addObject(this.owners.findById(ownerId));
return mav;
}
}
Conditional Bean Configuration.
- Before: ConditionalOnBean
- After: AnyNestedCondition
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();
}
}
import org.springframework.boot.autoconfigure.condition.AnyNestedCondition;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
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
@Conditional(ConditionCatSchoolOrDogSchool.class)
public VetSchool vetSchool() {
return new VetSchool();
}
private static class ConditionCatSchoolOrDogSchool extends AnyNestedCondition {
ConditionCatSchoolOrDogSchool() {
super(ConfigurationPhase.REGISTER_BEAN);
}
@ConditionalOnBean(CatSchool.class)
class CatSchoolCondition {
}
@ConditionalOnBean(DogSchool.class)
class DogSchoolCondition {
}
}
}
Known Limitations
| Unsupported Functionality | Issue |
|---|---|
| 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 |