Monday, July 30, 2018

Creating First SpringBoot Application

1. created maven project

mvn archetype:generate -DgroupId=com.test -DartifactId=SpringBootTest -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

2. To make this is a springboot application, we need to add the dependencies related to Springboot.

https://docs.spring.io/spring-boot/docs/current/reference/html/getting-started-first-application.html

 add "spring-boot-starter-parent" in "parent"  section.

 This is special starter
   a.) Provides useful maven defaults
   b.) Provides a dependancy management section - this helps you to ommit the "version" tags for "blessed" dependancies

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

3. Add other "Starters" which need to build a specific type of application.
  Let's say , we are going to build web, we add "spring-boot-starter-web" as dependency
 
Before adding the "spring-boot-starter-web" as dependency , we can check what we have with us currently with "mvn dependency:tree" command
This will give us an output as below.

[INFO] com.test:SpringBootTest:jar:1.0-SNAPSHOT
[INFO] \- junit:junit:jar:3.8.1:test
[INFO] ------------------------------------------------------------------------


"mvn dependency:tree" command prints the tree version of the dependancies in your project.

Important : "spring-boot-starter-parent" does not provides any dependancy itself. Not in the list.

So let's add neccessary dependancies and check the tree. As we are going to make web app, let's add the dependancy.

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

If you run "mvn dependency:tree" again ,  you will see the number of dependancies including springboot

[INFO] com.test:SpringBootTest:jar:1.0-SNAPSHOT
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:2.0.3.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter:jar:2.0.3.RELEASE:compile
[INFO] |  |  +- org.springframework.boot:spring-boot:jar:2.0.3.RELEASE:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-autoconfigure:jar:2.0.3.RELEASE:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-starter-logging:jar:2.0.3.RELEASE:compile
[INFO] |  |  |  +- ch.qos.logback:logback-classic:jar:1.2.3:compile
[INFO] |  |  |  |  +- ch.qos.logback:logback-core:jar:1.2.3:compile
[INFO] |  |  |  |  \- org.slf4j:slf4j-api:jar:1.7.25:compile
[INFO] |  |  |  +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.10.0:compile
[INFO] |  |  |  |  \- org.apache.logging.log4j:log4j-api:jar:2.10.0:compile
[INFO] |  |  |  \- org.slf4j:jul-to-slf4j:jar:1.7.25:compile
[INFO] |  |  +- javax.annotation:javax.annotation-api:jar:1.3.2:compile
[INFO] |  |  +- org.springframework:spring-core:jar:5.0.7.RELEASE:compile
[INFO] |  |  |  \- org.springframework:spring-jcl:jar:5.0.7.RELEASE:compile
[INFO] |  |  \- org.yaml:snakeyaml:jar:1.19:runtime
[INFO] |  +- org.springframework.boot:spring-boot-starter-json:jar:2.0.3.RELEASE:compile
[INFO] |  |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.9.6:compile
[INFO] |  |  |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.9.0:compile
[INFO] |  |  |  \- com.fasterxml.jackson.core:jackson-core:jar:2.9.6:compile
[INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.9.6:compile
[INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.9.6:compile
[INFO] |  |  \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.9.6:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.0.3.RELEASE:compile
[INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-core:jar:8.5.31:compile
[INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-el:jar:8.5.31:compile
[INFO] |  |  \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:8.5.31:compile
[INFO] |  +- org.hibernate.validator:hibernate-validator:jar:6.0.10.Final:compile
[INFO] |  |  +- javax.validation:validation-api:jar:2.0.1.Final:compile
[INFO] |  |  +- org.jboss.logging:jboss-logging:jar:3.3.2.Final:compile
[INFO] |  |  \- com.fasterxml:classmate:jar:1.3.4:compile
[INFO] |  +- org.springframework:spring-web:jar:5.0.7.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-beans:jar:5.0.7.RELEASE:compile
[INFO] |  \- org.springframework:spring-webmvc:jar:5.0.7.RELEASE:compile
[INFO] |     +- org.springframework:spring-aop:jar:5.0.7.RELEASE:compile
[INFO] |     +- org.springframework:spring-context:jar:5.0.7.RELEASE:compile
[INFO] |     \- org.springframework:spring-expression:jar:5.0.7.RELEASE:compile
[INFO] \- junit:junit:jar:3.8.1:test
[INFO] ------------------------------------------------------------------------



4. writting code.

Let's create a class with name "Example" and put inside "src/main/java".
Imporatnt: By default, maven compiles sources from "src/main/java".

So the file name will be "src/main/java/Example.java"

-------------------------------------------------------------------------------

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

@RestController
@EnableAutoConfiguration
public class Example {

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

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

}

------------------------------------------------------------------------------

5. Run application.
 Simply run the main class. This will deploy your main class in tomcat server and you can view the output by simply calling "http://localhost:8080/" from your web browser.

 This will give the output

 ---------------
 Hello World

 ---------------

 6. Creating an executable jar

 We can create an executable jar using "spring-boot-maven-plugin" in "pom.xml". Add the below, just below the "dependencies"

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


but if you have more than one class with main method, then better to sepcify the class you need to run

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

<configuration>
<mainClass>Example.class</mainClass>
<layout>ZIP</layout>
</configuration>
</plugin>
</plugins>
</build>



Important :
"spring-boot-starter-parent" POM includes configuration to bind the "repackage" goal. In simple, If you do not use the parent POM, you have to declare this configuration of your own.



Below is the final POM file.

--------------------------------------------------------------------

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>SpringBootTest</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>SpringBootTest</name>
<url>http://maven.apache.org</url>

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

<dependencies>

<!-- Spring boot web app -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>

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

<configuration>
<mainClass>Example.class</mainClass>
<layout>ZIP</layout>
</configuration>
</plugin>
</plugins>
</build>
</project>





  Understanding annotations

 @RestController -> know as a stereotype annotation, provides a hint for the people reading the code and for the Spring , that class plays a specific role.
 So in this case, our class is a web @Controller.

 @RequestMapping --> provides  “routing” information.
 tells Spring that any HTTP request with the "/" path should be mapped to the "home " method.

 @RestController --> Spring to render the resulting string directly back to the caller.

 @RestController and @RequestMapping annotations are Spring MVC annotations. (They are not specific to Spring Boot.)

 @EnableAutoConfiguration -->
 tells Spring Boot to “guess” how you want to configure Spring, based on the jar dependencies that you have added. As we have added "spring-boot-starter-web" Tomcat and Spring MVC, the auto-configuration assumes that you are developing a web application and sets up Spring accordingly.


Our main method delegates to Spring Boot’s SpringApplication class by calling run. SpringApplication bootstraps our application, starting Spring, which, in turn, starts the auto-configured Tomcat web server. We need to pass Example.class as an argument to the run method to tell SpringApplication which is the primary Spring component. The args array is also passed through to expose any command-line arguments.

No comments:

Post a Comment