Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to migrate from traditional java web application (with web.xml) to spring boot?

I wanna switch my projects to spring-based product.

My first step is to transform my java web application from a generated WAR file, to a standalone executable jar, powered by spring boot.

Let's take a open source web application example from github.: Vaadin-Spring Web Application

The web.xml file can be found here.

The root-context file can be found here.

I hope that there are some guides for me to perform the transformation.

I have also submit an issue in the spring-boot project.

like image 234
mfkenson Avatar asked Nov 27 '13 11:11

mfkenson


1 Answers

This application is not a Spring MVC application as far as I can tell - it would probably be a lot easier to migrate if it was. The goal (per the github issue) is to obtain an executable JAR. The basic plan though might be to migrate first to a WAR using Spring Boot and then to a JAR once that is working. It's a pretty simple app so all we really need to do is look at the web.xml and translate it into the relevant Spring Boot features. Here are some general guides:

Create a deployable WAR by extending SpringBootServletInitializer (e.g. in a class called Application), and add the Spring Boot @EnableAutoConfiguration annotation. Example:

    @Configuration
    @EnableAutoConfiguration
    @ComponentScan
    public class Application extends SpringBootServletInitializer {

        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(Application.class);
        }

Then add some configuration:

  • A @Bean of type Servlet or ServletRegistrationBean installs that bean in the container as if it was a <servlet/> and <servlet-mapping/> in web.xml

  • A @Bean of type Filter or FilterRegistrationBean behaves similarly (like a <filter/> and <filter-mapping/>).

  • The ApplicationContext in this case is rooted in an XML file, so the easiest first step is to @Import that into the Spring Application. This one is so simple that it can be recreated in a few lines as @Bean definitions.

  • Static resources can be moved to /public (or /static or /resources or /META-INFO/resources) in the classpath root

Once the WAR is working we make it executable by adding a main method to our Application, e.g.

public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
}

See also the Getting Started Guide on Converting a JAR to a WAR.

As I said, the biggest problem with this specific app is that it isn't a Spring MVC app. As the Irishman might say "If I wanted to get to there, sir, I wouldn't be starting from here." This is an interesting question in general, but I recommend anyone else looking to migrate a Spring application to Spring Boot read the general advice here but maybe start another discussion somewhere else.

Anyway, I'll have a bash at converting this specific app (source code jars would be nice), and update this response if I learn anything new.

like image 54
Dave Syer Avatar answered Sep 18 '22 07:09

Dave Syer