Monday, August 17, 2015

Migrating Spring Rest Service Application to Spring Boot

I have a relatively  simple Spring-based REST Service application. I wanted to see how difficult it will be to migrate to Spring Boot.
Some of the details of the application I have:
  • Simple REST Service which uses cassandra-data in the backend
  • Configuration in web.xml which includes filters and listeners
  • Spring Configuration in Java 
  • Logback logging 
  • Maven configuration
I had an older version of maven installed. I had to install the latest version because Spring Boot supports maven 3.3 and higher.

With  this out of the way I added the new dependencies to pom.xml:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.5.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
   .....
</dependencies>
I also commented out all spring dependencies I had execpt for spring-data-cassandra, which is not yet suppported directly by Spring Boot.

Then I created Application.java in the root package with the following code:
 
package basepackage;
import .....
 
@Configuration@EnableAutoConfiguration@ComponentScan@EnableCassandraRepositories(basePackages = { "basepackage" })
public class Application  extends SpringBootServletInitializer{


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

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

and removed @Configuration and @ComponentScan annotations from WebConfig, which did not have anything else in it.

After that I ran:

 mvn spring-boot:run

and application compiled, started and then spit out a nice big stack trace:

java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:483)
        at org.springframework.boot.maven.RunMojo$LaunchRunner.run(RunMojo.java:418)
        at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.BeanCreationException: 
To be continued ....

No comments:

Post a Comment