Migrate to Spring Boot 2 from Spring Boot 1
In this guide we'll look at using OpenRewrite to perform an automated migration from SpringBoot 1 to SpringBoot 2.

Example Configuration

The SpringBoot1To2Migration recipe has no required configuration options and can be activated directly after taking a dependency on rewrite-spring in your build file:
Gradle
Maven
build.gradle
1
plugins {
2
id("org.openrewrite.rewrite") version("5.12.0")
3
}
4
5
rewrite {
6
activeRecipe("org.openrewrite.java.spring.boot2.SpringBoot1To2Migration")
7
}
8
9
repositories {
10
mavenCentral()
11
}
12
13
dependencies {
14
rewrite("org.openrewrite.recipe:rewrite-spring:4.13.0")
15
}
Copied!
pom.xml
1
<project>
2
<build>
3
<plugins>
4
<plugin>
5
<groupId>org.openrewrite.maven</groupId>
6
<artifactId>rewrite-maven-plugin</artifactId>
7
<version>4.13.0</version>
8
<configuration>
9
<activeRecipes>
10
<recipe>org.openrewrite.java.spring.boot2.SpringBoot1To2Migration</recipe>
11
</activeRecipes>
12
</configuration>
13
<dependencies>
14
<dependency>
15
<groupId>org.openrewrite.recipe</groupId>
16
<artifactId>rewrite-spring</artifactId>
17
<version>4.13.0</version>
18
</dependency>
19
</dependencies>
20
</plugin>
21
</plugins>
22
</build>
23
</project>
Copied!
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
1
package org.springframework.samples.petclinic.owner;
2
3
import org.springframework.beans.factory.annotation.Autowired;
4
import org.springframework.stereotype.Controller;
5
import org.springframework.ui.Model;
6
import org.springframework.validation.BindingResult;
7
import org.springframework.web.bind.WebDataBinder;
8
import org.springframework.web.bind.annotation.*;
9
import org.springframework.web.servlet.ModelAndView;
10
11
import javax.validation.Valid;
12
import java.util.Collection;
13
import java.util.Map;
14
15
import static org.springframework.web.bind.annotation.RequestMethod.*;
16
17
@Controller
18
class OwnerController {
19
20
private static final String VIEWS_OWNER_CREATE_OR_UPDATE_FORM = "owners/createOrUpdateOwnerForm";
21
private final OwnerRepository owners;
22
23
@Autowired
24
public OwnerController(OwnerRepository clinicService) {
25
this.owners = clinicService;
26
}
27
28
@RequestMapping("/owners/{ownerId}/edit")
29
public String initUpdateOwnerForm(@PathVariable("ownerId") int id, Model model) {
30
Owner owner = this.owners.findById(id);
31
model.addAttribute(owner);
32
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
33
}
34
35
@RequestMapping(value = "/owners/{ownerId}/edit", method = POST)
36
public String processUpdateOwnerForm(@Valid Owner owner, BindingResult result, @PathVariable("ownerId") int ownerId) {
37
if (result.hasErrors()) {
38
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
39
} else {
40
owner.setId(ownerId);
41
this.owners.save(owner);
42
return "redirect:/owners/{ownerId}";
43
}
44
}
45
46
@RequestMapping("/owners/{ownerId}")
47
public ModelAndView showOwner(@PathVariable("ownerId") int ownerId) {
48
ModelAndView mav = new ModelAndView("owners/ownerDetails");
49
mav.addObject(this.owners.findById(ownerId));
50
return mav;
51
}
52
53
}
Copied!
1
package org.springframework.samples.petclinic.owner;
2
3
import org.springframework.stereotype.Controller;
4
import org.springframework.ui.Model;
5
import org.springframework.validation.BindingResult;
6
import org.springframework.web.bind.WebDataBinder;
7
import org.springframework.web.bind.annotation.*;
8
import org.springframework.web.servlet.ModelAndView;
9
10
import javax.validation.Valid;
11
import java.util.Collection;
12
import java.util.Map;
13
14
import static org.springframework.web.bind.annotation.RequestMethod.*;
15
16
@Controller
17
class OwnerController {
18
19
private static final String VIEWS_OWNER_CREATE_OR_UPDATE_FORM = "owners/createOrUpdateOwnerForm";
20
private final OwnerRepository owners;
21
22
public OwnerController(OwnerRepository clinicService) {
23
this.owners = clinicService;
24
}
25
26
@GetMapping("/owners/{ownerId}/edit")
27
public String initUpdateOwnerForm(@PathVariable("ownerId") int id, Model model) {
28
Owner owner = this.owners.findById(id);
29
model.addAttribute(owner);
30
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
31
}
32
33
@PostMapping("/owners/{ownerId}/edit")
34
public String processUpdateOwnerForm(@Valid Owner owner, BindingResult result, @PathVariable int ownerId) {
35
if (result.hasErrors()) {
36
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
37
} else {
38
owner.setId(ownerId);
39
this.owners.save(owner);
40
return "redirect:/owners/{ownerId}";
41
}
42
}
43
44
@GetMapping("/owners/{ownerId}")
45
public ModelAndView showOwner(@PathVariable int ownerId) {
46
ModelAndView mav = new ModelAndView("owners/ownerDetails");
47
mav.addObject(this.owners.findById(ownerId));
48
return mav;
49
}
50
51
}
Copied!

Conditional Bean Configuration.

Before: ConditionalOnBean
After: AnyNestedCondition
1
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
2
import org.springframework.context.annotation.Bean;
3
import org.springframework.context.annotation.Configuration;
4
5
@Configuration
6
public class SchoolConfig {
7
@Bean
8
public DogSchool dogSchool() {
9
return new DogSchool();
10
}
11
12
@Bean
13
public CatSchool catSchool() {
14
return new CatSchool();
15
}
16
17
@Bean
18
@ConditionalOnBean({CatSchool.class, DogSchool.class})
19
public VetSchool vetSchool() {
20
return new VetSchool();
21
}
22
}
Copied!
1
import org.springframework.boot.autoconfigure.condition.AnyNestedCondition;
2
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
3
import org.springframework.context.annotation.Bean;
4
import org.springframework.context.annotation.Conditional;
5
import org.springframework.context.annotation.Configuration;
6
7
@Configuration
8
public class SchoolConfig {
9
@Bean
10
public DogSchool dogSchool() {
11
return new DogSchool();
12
}
13
14
@Bean
15
public CatSchool catSchool() {
16
return new CatSchool();
17
}
18
19
@Bean
20
@Conditional(ConditionCatSchoolOrDogSchool.class)
21
public VetSchool vetSchool() {
22
return new VetSchool();
23
}
24
25
private static class ConditionCatSchoolOrDogSchool extends AnyNestedCondition {
26
ConditionCatSchoolOrDogSchool() {
27
super(ConfigurationPhase.REGISTER_BEAN);
28
}
29
30
@ConditionalOnBean(CatSchool.class)
31
class CatSchoolCondition {
32
}
33
34
@ConditionalOnBean(DogSchool.class)
35
class DogSchoolCondition {
36
}
37
}
38
}
Copied!

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
Last modified 10d ago