Creating Simple Spring Boot Web Application Using Maven

What is spring-boot ?
To answer this, I directly quoted from the official website :

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run".

"Just run" refers that it has embedded Tomcat [servlet container] so that we can run the application anywhere [where installed java]. No need to configure any server. 
Before Starting this tutorial we need to ensure that
  1. Maven installed properly. [ if not installed then you can follow the tutorial how to install ]
  2. Installed Java properly.
You can create a spring-boot project from your favorite IDE (In my case Netbeans 8.0.2) or from the direct web (http://start.spring.io/). Let me describe both.

How to Create maven project in Netbeans:

  1. Click on File ->new project ->maven (category) -> web application and click next.
  2. Give your project a name and click next to select server (tomcat) and then click finish.
  3. Open POM xml and <packaging>war</packaging> to <packaging>jar</packaging>
To be able for your project to work with spring boot, your project should be a child of the parent spring boot module. So we need to add the parent tag in our pom.xml and point to spring-boot-starter-parent. (just before <dependencies> tag)

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

Add this 2 dependencies under the <dependencies> tag,
 <dependency>  
     <groupId>org.springframework.boot</groupId>  
     <artifactId>spring-boot-starter</artifactId>  
  </dependency>  
  <dependency>  
    <groupId>org.springframework.boot</groupId>  
    <artifactId>spring-boot-starter-web</artifactId>  
 </dependency>  

spring-boot-starter : Responsible for spring boot autoconfigure your application.
spring-boot-starter-web : this dependency tells spring boot that your application is a web application. This adds the functionality of @Controller, @RequestMapping, etc.

We should add spring boot maven plugin in the plugin section in pom

 <plugin>  
     <groupId>org.springframework.boot</groupId>  
     <artifactId>spring-boot-maven-plugin</artifactId>  
 </plugin>  

Just like other java application we need a main class to run the application.
 @SpringBootApplication  
 public class Application {  
   public static void main(String[] args) throws Exception {  
     SpringApplication.run(Application.class, args);  
   }  
 }  

Add a spring-mvc controller to handle request
 @RestController  
 @EnableAutoConfiguration  
 public class UserController {  
   @RequestMapping("/")  
   String home() {  
     return "Hello World!";  
   }  
 }  

Now clean and build your project from Netbeans. maven will automatically download jars from central repo if not found in local repo [ Usually inside in .m2 folder ].

Build from the command line:

You can build your project from command prompt. Open command prompt and go to your project directory and execute these command below.


  1. mvn clean build
  2. mvn package
It will create a folder named /target. Go to target directory and from command prompt and you found your desired jar named something like that "your_project_name-1.0-SNAPSHOT.jar".
Now run the jar using command java -jar your_project_name-1.0-SNAPSHOT.jar
This command will start embedded tomcat servlet container on default port 8080. After running you can access your web-app by hitting the url in browser. http://localhost:8080

How to create a spring-boot project from direct web:

Go to the url http://start.spring.io/ and put a group name and artifact name. If you are not sure then keep it as it is. Under the Dependency section write web. This will suggest you a package and you should select the package. Then hit the generate project button. You have download a project is in zip format. Extract the project and go to the project directory. Remember that this project is an empty project where no controller added.If you want then you can create a controller as your choice. you'll see the pom.xml and other folder and necessary files. Open the command window and then build the project as I said earlier labeled "Build from the command line:"

Cheers!!!!! You just learn how to create spring boot web application.

Comments

Popular posts from this blog

UUID to BigInteger conversion and BigInteger to UUID conversion

ActiveMQ message producer and consumer with durable subscriber example

Create Maven Local Repository, Upload your own artifact & download jar from local mirror with Artifactory and Nexus OSS