Spring Boot 兼容 JDK1.6 运行遗留项目

Spring Boot 兼容 JDK1.6 运行遗留项目_第1张图片
Mr 1900

最近有几个遗留项目需要改造成 Rest API,为了统一想采用和新项目一致的 Spring Boot 框架。原型验证的阶段发现老项目用了一些 Java6 的独有API,主要是数字证书那部分API,Java7之后去除了这些sun的包。为了平稳过渡,第一阶段考虑先 Spring Boot 1.5 + Java 6,然后逐步重写老的业务逻辑,逐步去除Java 6依赖。

先看 Spring Boot 官方文档 对于Java 6的说法

85.11 How to use Java 6

If you want to use Spring Boot with Java 6 there are a small number of configuration changes that you will have to make. The exact changes depend on your application’s functionality.

85.11.1 Embedded servlet container compatibility

If you are using one of Boot’s embedded Servlet containers you will have to use a Java 6-compatible container. Both Tomcat 7 and Jetty 8 are Java 6 compatible. See Section 73.16, “Use Tomcat 7.x or 8.0” and Section 73.18, “Use Jetty 8” for details.

85.11.2 Jackson

Jackson 2.7 and later requires Java 7. If you want to use Jackson with Java 6 you will have to downgrade to Jackson 2.6.

Spring Boot uses the Jackson BOM that was introduced as of Jackson 2.7 so you can’t just override the jackson.version property. In order to use Jackson 2.6, you will have to define the individual modules in the dependencyManagement section of your build, check this example for more details.

主要限制如下:

  1. Tomcat支持到7.x
  2. Maven支持到3.2.x
  3. Jackson支持到2.6.x

Maven配置如下



   4.0.0

   com.demo2o.example
   spring-boot-legacy
   0.0.1-SNAPSHOT
   spring-boot-legacy
   jar
   Demo project for Spring Boot legacy support with Java 6

   
      org.springframework.boot
      spring-boot-starter-parent
      1.5.21.RELEASE
   


   

      1.6
      7.0.70
      2.6.7
      UTF-8
      UTF-8

   

   
      
         org.springframework.boot
         spring-boot-starter-web
      

      
      
         org.apache.tomcat
         tomcat-juli
         ${tomcat.version}
      

      
      
         com.fasterxml.jackson.core
         jackson-core
         ${jackson.version}
      

      
         com.fasterxml.jackson.core
         jackson-annotations
         ${jackson.version}
      

      
         com.fasterxml.jackson.core
         jackson-databind
         ${jackson.version}
      


      
         org.springframework.boot
         spring-boot-starter-test
         test
      
   

    
    
        
            java6
            
                
                    central
                    http://repo1.maven.org/maven2
                    
            
        
     

   
      
         
            org.springframework.boot
            spring-boot-maven-plugin
            1.5.21.RELEASE
         

         
                org.apache.maven.plugins
            maven-compiler-plugin
         

         
            org.apache.maven.plugins
            maven-surefire-plugin
            2.19.1
            
               ${env.JAVA_HOME}/bin/java
            
         
      
   

   
   
      
         aliyun
         aliyun
         http://maven.aliyun.com/nexus/content/groups/public
      
   

除了修改部分依赖版本,增加了一个 java6profile,强制使用http协议访问中央库(Java6 不支持 TLS1.2,Maven 2018年开始不支持TLS1.0/1.1)。

通过上述配置,项目已经可以在Java 6环境下正常运行。为了方便编译和运行,做了2个bat脚本。指定编译时使用特定的JDK和Maven。

set JAVA_HOME=C:\home\dev\java\jdks\jdk1.6.0_45
set M2_HOME=C:\home\dev\java\tools\apache-maven-3.2.5

C:\home\dev\java\tools\apache-maven-3.2.5\bin\mvn.bat -e -Pjava6 clean package spring-boot:repackage

pause

Github:https://github.com/kenzen/spring-boot-legacy-demo

你可能感兴趣的:(Spring Boot 兼容 JDK1.6 运行遗留项目)