9

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.

mfkenson
  • 123
  • 1
  • 1
  • 7

1 Answers1

14

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.

Dave Syer
  • 56,583
  • 10
  • 155
  • 143
  • I expect that with springboot, the tendency is more and more to go from WAR to JAR. I am just going through a nice deal of trouble to try to setup a development friendly pom.xml that will allow me to work productively with springboot + gwt. I am embrancing the executable JAR concept, on going through the double of disable the GWT run jetty and making sure GWT compile puts the sources in the META-INF/resources, to work properly during dev time. But yeah, some people may want to move from JAR to WAR mostly to deploy on the coud e.g. app engine. But in general spring boot is nicer as a JAR. – 99Sono Jan 16 '16 at 15:54
  • what about mutli war project? http://stackoverflow.com/questions/35632394/converting-multi-war-application-to-spring-boot – Yosefki Feb 25 '16 at 16:14
  • @dave-syer, thanks for the notes, I am finding this very helpful. I know this is a very old post, I recently was asked to evaluate on migrating a relatively stable Non SpringMVC web application to Spring Boot. idea is to convert to an executable jar style to be able to deploy on cloud with autoscaling. could you advise what was your opinion on it, anything changed on Spring BOOT in last 3 years that can affect for or against. – Venkat Jun 14 '16 at 15:00
  • 1
    All of the above still applies AFAIK. Josh Long did a twitter stream about converting to Spring Boot last week. It was pretty similar (but shorter). – Dave Syer Jun 17 '16 at 16:03
  • How can I migrate the init-param elements of a servlet section? – Puce Aug 25 '16 at 12:37