Skip to content

Instantly share code, notes, and snippets.

@dineshbhagat
Last active April 26, 2021 06:16
Show Gist options
  • Save dineshbhagat/e9129bcb9cdba0d850e2d7ce2b2368bf to your computer and use it in GitHub Desktop.
Save dineshbhagat/e9129bcb9cdba0d850e2d7ce2b2368bf to your computer and use it in GitHub Desktop.
Steps to upgrade spring-boot 1.x to 2.x
  1. Upgrade gradle/maven wrapper, here gradle
    Ref
    Make sure execute this program on success build.

     ./gradlew wrapper --gradle-version 5.6.4  --distribution-type all  
  2. spring-boot-gradle-plugin changes,

    1. bootRepackage to bootJar or bootWar
    
    2. task wrapper(type: Wrapper) { gradleVersion = '5.6.4' }
    to 
    wrapper { gradleVersion = '5.6.4' }
  3. Check some of the features deprecate/renamed in gradle, deprecated etc here

  4. apache camel dependency on spring boot, kafka, etc, also other libs dependencies
    Note: Kafka 1.1: https://mvnrepository.com/artifact/org.apache.camel/camel-kafka/2.22.4
    kafka 2.0 : https://mvnrepository.com/artifact/org.apache.camel/camel-kafka/2.23.1
    https://github.com/apache/camel/blob/master/components/camel-kafka/src/main/docs/kafka-component.adoc

  5. Apache commons upgrade
    Change:
    package import org.apache.commons.lang -> import org.apache.commons.lang3
    groupId: commons-lang -> org.apache.commons
    artifactId: commons-lang -> commons-lang3

  6. Spring-boot-2 related properties changes
    https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.0-Migration-Guide

    https://altkomsoftware.pl/en/blog/spring-boot-migration-java/

    • Replace

    org.springframework.boot.test.TestRestTemplate with
    org.springframework.boot.test.web.client.TestRestTemplate

    extends WebMvcConfigurerAdapter  
    with  
    implements WebMvcConfigurer  
    • Change kebab_case properties to snake-case e.g. bank_service.can_be_enable=true to bank-service.can-be-enable=true
      (Note: Changing just property prefix from kebab_case to snake-case e.g. bank_service.can_be_enable=true to bank-service.can_be_enable=true is sufficient to make spring boot work but not encouraged because it may result in inconsist property structure)

    • Properties changes:

    Old property New property
    server.context-parameters.* server.servlet.context-parameters.*
    server.context-path server.servlet.context-path
    server.jsp.class-name server.servlet.jsp.class-name
    server.jsp.init-parameters.* server.servlet.jsp.init-parameters.*
    server.jsp.registered server.servlet.jsp.registered
    server.servlet-path server.servlet.path
    • Config Keys:
    Old property New property
    endpoints..* management.endpoint..*
    endpoints.cors.* management.endpoints.web.cors.*
    endpoints.jmx.* management.endpoints.jmx.*
    management.address management.server.address
    management.context-path management.server.servlet.context-path
    management.ssl.* management.server.ssl.*
    management.port management.server.port

    Exhaustive List of Properties

    • restTemplate change:
      restTemplateBuilder.requestFactory(clientHttpRequestFactory).build();
      To
      new RestTemplate(clientHttpRequestFactory);

    • Testing setup changes [Integration test]

    @ContextConfiguration(loader = SpringApplicationContextLoader.class, classes = MainApplication.class)
    @WebAppConfiguration
    @IntegrationTest("server.port=9090")

    to

    @SpringBootTest(classes = MainApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,properties = [ "server.port:9090", "spring.main.allow-bean-definition-overriding=true"])

    Ref: https://www.jianshu.com/p/6a14001729a7

    https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-external-config-relaxed-binding

  7. Mockito from 1.x to 2.x

    when(mock.method(anyString())).thenReturn(result);
    with
    when(mock.method(nullable(String.class))).thenReturn(result);

    mockitoVersion = '3.1.5'

    If there is any following type of code

    @InjectMock
    @Autowire
    interface interfaceImplementation
    
    // Change it to the following because InjectMock does not work on the interface, it needs concrete implementation
    
    @InjectMock 
    @Autowire
    InterfaceImpl interfaceImplementation

    If older code had used Whitebox functionality then it should be replaced since in Mockito2.x it is not supported
    Ref And If you are using Spring (the spring-test library specifically), you can simply use
    ReflectionTestUtils.setField instead of Whitebox.setInternalState

    incase if you want to use junit-5
    Ref: https://howtodoinjava.com/spring-boot2/junit5-with-spring-boot2/

  8. Kafka upgrade:

  9. If you need, just thin jar and not fat jar, then

    bootJar.enable=false
    jar.enable=true
  10. Testing spring boot2: @SpringApplicationConfiguration is no longer available, instead you must use @SpringBootTest.

  11. Analyze dependencies

  12. Dropwizard metrics support is not available for spring boot 2, need to use a micrometer, or need to add it explicitly

    ./gradlew dependencies
    ./gradlew -q dependencyInsight --dependency spring-boot-starter-web
    ./gradlew dependencyInsight --dependency logging

Ref1
Ref2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment