Ashik’s IT Thoughts

June 4, 2015

Spring with Maven using Spring Boot

Filed under: Uncategorized — ashikuzzaman @ 5:20 pm

I started playing around Spring after a long time. I needed to quickly get ramped up and hence used Spring Boot module to get spring,maven, embedded tomcat up and running. The sample files in this example I have committed to my public github repository here – https://github.com/ashikuzzaman/springchecks

You can use the below steps so hat you can use too to get started –

1. make sure you have java installed and path, java home mentioned in bashrc file
1a) verify “java -version” or better yet “java -XX:+PrintCommandLineFlags -version”

2. sudo apt-get install maven
2a) verify maven installation by “mvn -v

3. create a directory for your project and create an emlty pom.xml file there to be used with maven.

4. Update pom.xml file with the content similar to what is in /home/auzzaman/dev/coding/java/maven/pom.xml

5. Once pom.xml is created, open up a terminal and go to that same folder where the file is. Lets say this is your project home or maven home. Now run the command “mvn package” from here.

6. Now run “mvn:dependency:tree“. It should download dependency files

7. Now update pom.xml to add a few more dependencies. For example add it –

org.springframework.boot
spring-boot-starter-web
8. Now create directory structure under maven home as src/main/java. For example, my directory structure is /home/auzzaman/dev/coding/java/maven/src/main/java

9. Create a new java source file AshikHelloWorldExample.java in that folder with the below content –

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;

@RestController
@EnableAutoConfiguration
public class AshikHelloWorldExample {

@RequestMapping(“/”)
String home() {
return “Hello World!”;
}

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

}

10. Run the command “mvn spring-boot:run” to compile the program, download tomcat and other dependent jar files and bring tomcat up and running with the webapp created that uses your java file to show output in the browser.

11. Open your browser and hit http://localhost:8080/ to see the response from the webapp as “Hello World!”. To access the app from a different machine in your network, use the following URL – http://auzzaman-wsl3:8080/

12. To gracefully exit the application hit “ctrl-c“.

13. Now lets make some changes to the application. For example, add a package to the class and change the text to display in the webapp a bit like below –

package com.github.ashikuzzaman.javaapichecks.spring;

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;

@RestController
@EnableAutoConfiguration
public class AshikHelloWorldExample {

@RequestMapping(“/”)
String home() {
return “Hello World from Ashik using Spring and Maven in an executable jar!”;
}

public static void main(String[] args) throws Exception {
SpringApplication.run(com.github.ashikuzzaman.javaapichecks.spring.AshikHelloWorldExample.class, args);
}

}

14. To create an executable jar first we need to add a maven plugin for spring boot. Add the below elements under dependenceies scetion in pom.xml –
   <dependencies>
        <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

15. Add an etnry if you want to change the JDK major version that you want to use. For example, if you want to specify that you want to use Java 7, here is an entry you can add just after the previous build element that you added –

    <properties>
        <java.version>1.7</java.version>
    </properties>

16. Save pom.xml and run “mvn package

17. If you look in maven’s target directory you should see myproject-0.0.1-SNAPSHOT.jar file. You can print verbose what’s in it by “jar tvf target/myproject-0.0.1-SNAPSHOT.jar”

18. To run the application from command line, use the following command “java -jar target/myproject-0.0.1-SNAPSHOT.jar

19. Again you have to open the browser and access http://localhost:8080/

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: