Thursday, August 16, 2018

Some useful java commands

java -XX:+PrintFlagsFinal -version | findstr /i "HeapSize PermSize ThreadStackSize"


jps –lvm

Saturday, August 11, 2018

Create Simple Microservice project to understand the concepts

I have searched throughout the web and below is the sample code i have found.
https://spring.io/blog/2015/07/14/microservices-with-spring

below are the simplified steps in creating running the project



created pom with all dependencies and build the maven project


1.  create package "io.pivotal.microservices.services.registration"
a.)RegistrationServer.java
b.) registration-server.yml inside "src\main\resources"
c.) run RegistrationServer.java and access http://localhost:1111/
run RegistrationServer with jar created. 

2. Create account services
a.) create package "io.pivotal.microservices.services.accounts" and service class "AccountsServer.java"
This class need "AccountsServer.class" and "AccountRepository.java"
b.) Business logic for Account. create package "io.pivotal.microservices.accounts"
create "Account.java"  pojo
create "AccountRepository.java"
create "AccountsConfiguration.java"
create "AccountsController.java". --> need exceptioon class also for this.
create "HomeController.java"
c.) create exception class for account
create package "io.pivotal.microservices.exceptions" and class "AccountNotFoundException.java"

Now error in the "AccountsServer.java"  will be resolved.

Run "AccountsServer.java" -->

3. Errors
a.)error [db-config.properties] cannot be opened because it does not exist

Added "db-config.properties" to folder "src\main\resources"

b.)Error
class path resource [testdb/schema.sql] cannot be opened because it does not exist
create folder "testdb" inside "resources" and Added "schema.sql" and "data.sql"

4. Now run "http://localhost:1111/" , you can see the account service registered.
for more info gotot "http://localhost:1111/eureka/apps/"

Alternativel go and see "http://localhost:1111/eureka/apps/ACCOUNTS-SERVICE"

5. Configuration options
Registration takes upto 30s cause this is default client refresh time. Can change setting "eureka.instance.leaseRenewalIntervalInSeconds"
Note : Smaller numbers are not recommened in Prod

eureka:
  instance:
    leaseRenewalIntervalInSeconds: 5         # DO NOT DO THIS IN PRODUCTION



6. Acessing microservice web service

a.) create package "io.pivotal.microservices.services.web"
b.) create classes
"Account.java"
HomeController.java
SearchCriteria.java
WebAccountsController.java  --> needs "WebAccountsController"
WebAccountsService.java
WebServer.java

7. Make sure you have yml files for each service
registration-server.yml
accounts-server.yml
web-server.yml

8. Running Three services without building jar
mvn spring-boot:run -Dstart-class=io.pivotal.microservices.services.registration.RegistrationServer
mvn spring-boot:run -Dstart-class=io.pivotal.microservices.services.accounts.AccountsServer
mvn spring-boot:run -Dstart-class=io.pivotal.microservices.services.web.WebServer

9. Building jar file.
As we have 3 main classes we need to specify the main class inside POM file.
In this case we create onc single class with main method "Main.java"  and call other services via that class

inside POM

<properties>
<start-class>io.pivotal.microservices.services.Main</start-class>
</properties>

Then create Main class inside package "io.pivotal.microservices.services"

Call the each service and run as below

java -jar target/microservices-demo-2.0.0.RELEASE.jar registration
java -jar target/microservices-demo-2.0.0.RELEASE.jar accounts
java -jar target/microservices-demo-2.0.0.RELEASE.jar web










Create Micro service Project with many services and running one by one

You may have build the all the microservices in a single project or One project has many micro services.

In that case how to run those services picking up one by one as needed.

Example i have taken  from

https://spring.io/blog/2015/07/14/microservices-with-spring


eg : we have three micro services projects.
1. Registration server - eureka server
2. Account service
3. Web

How to run without building the jar:

we use maven and use spring-boot plugin to specify 'run' and give the main class with "-Dstart-class" tag

below is the three sample commands, run from the sample project directory where the POM file exist.(where you run mvn clean package  command)

mvn spring-boot:run -Dstart-class=io.pivotal.microservices.services.registration.RegistrationServer

mvn spring-boot:run -Dstart-class=io.pivotal.microservices.services.accounts.AccountsServer

mvn spring-boot:run -Dstart-class=io.pivotal.microservices.services.web.WebServer



How to run with Jar

Since we have few main classes

1. We need to specify the maven what is our main class

<properties>
<start-class>io.pivotal.microservices.services.Main</start-class>
</properties>

2. Create One single class with Main method. 
We call it Main.java
As we can. specify only one main class, we need to create a class with one main method which will guide to / call to other main methods (RegistrationServer.java , AccountsServer.java, WebServer.java)



3. Run jar with calling each service via Main.java
open three command lines and execute below three command, which will up three services

java -jar target/microservices-demo-2.0.0.RELEASE.jar registration
java -jar target/microservices-demo-2.0.0.RELEASE.jar accounts
java -jar target/microservices-demo-2.0.0.RELEASE.jar web


----------------------------------------------------------------below code snipet for Main.java--------------

If you do not create Main.java, still maven will build the project without error, but running this you will get 
Exception in thread "main" java.lang.ClassNotFoundException: io.pivotal.microservices.services.Main


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




package io.pivotal.microservices.services;

import io.pivotal.microservices.services.accounts.AccountsServer;
import io.pivotal.microservices.services.registration.RegistrationServer;
import io.pivotal.microservices.services.web.WebServer;

/**
 * Allow the servers to be invoked from the command-line. The jar is built with
 * this as the Main-Class in the jar's MANIFEST.MF.
 * 
 * @author Paul Chapman
 */
public class Main {

 public static void main(String[] args) {

  String serverName = "NO-VALUE";

  switch (args.length) {
  case 2:
   // Optionally set the HTTP port to listen on, overrides
   // value in the -server.yml file
   System.setProperty("server.port", args[1]);
   // Fall through into ..

  case 1:
   serverName = args[0].toLowerCase();
   break;

  default:
   usage();
   return;
  }

  if (serverName.equals("registration") || serverName.equals("reg")) {
   RegistrationServer.main(args);
  } else if (serverName.equals("accounts")) {
   AccountsServer.main(args);
  } else if (serverName.equals("web")) {
   WebServer.main(args);
  } else {
   System.out.println("Unknown server type: " + serverName);
   usage();
  }
 }

 protected static void usage() {
  System.out.println("Usage: java -jar ...  [server-port]");
  System.out.println(
    "     where server-name is 'reg', 'registration', " + "'accounts' or 'web' and server-port > 1024");
 }
}

Friday, August 10, 2018

Error building micro services app

https://spring.io/blog/2015/07/14/microservices-with-spring

Below shows the error. Solution is adding below snipet , "start-class" for the POM. Try building with "mvn clean package"

Solution :

<properties>
<start-class>io.pivotal.microservices.services.Main</start-class>
</properties>


Then run the jar with specifying which main method needs to be choose.

eg :
java -jar microservices-demo-2.0.0.RELEASE.jar account
java -jar microservices-demo-2.0.0.RELEASE.jar account
java -jar microservices-demo-2.0.0.RELEASE.jar account


------ Error --------------------------------

[INFO] Finished at: 2018-08-10T16:41:48+05:30
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.0.1.RELEASE:repackage (default) on project EurekaServer1: Execution default of goal org.springframework.boot:spring-boot-maven-plugin:2.0.1.RELEASE:repackage failed: Unable to find a single main class from the following candidates
[io.pivotal.microservices.services.registration.RegistrationServer, io.pivotal.microservices.services.web.WebServer] ->
[Help 1]org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.0.1.RELEASE:repackage (default) on project EurekaServer1: Execution default of goal org.springframework.boot:spring-boot-maven-plugin:2.0.1.RELEASE:repackage failed: Unable to find a single main class from the following candidates
[io.pivotal.microservices.services.registration.RegistrationServer, io.pivotal.microservices.services.web.WebServer] at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:213) at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:154) at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:146) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81) at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56) at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128) at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305) at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192) at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105) at org.apache.maven.cli.MavenCli.execute (MavenCli.java:954) at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288) at org.apache.maven.cli.MavenCli.main (MavenCli.java:192) 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:497) at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289) at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229) at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415) at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)Caused by: org.apache.maven.plugin.PluginExecutionException: Execution default of goal org.springframework.boot:spring-boot-maven-plugin:2.0.1.RELEASE:repackage failed: Unable to find a single main class from the following candidates
[io.pivotal.microservices.services.registration.RegistrationServer, io.pivotal.microservices.services.web.WebServer] at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:148) at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:208) at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:154) at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:146) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81) at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56) at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128) at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305) at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192) at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105) at org.apache.maven.cli.MavenCli.execute (MavenCli.java:954) at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288) at org.apache.maven.cli.MavenCli.main (MavenCli.java:192) 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:497) at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289) at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229) at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415) at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)Caused by: java.lang.IllegalStateException: Unable to find a single main class from the following candidates
[io.pivotal.microservices.services.registration.RegistrationServer, io.pivotal.microservices.services.web.WebServer] at org.springframework.boot.loader.tools.MainClassFinder$SingleMainClassCallback.getMainClassName (MainClassFinder.java:451) at org.springframework.boot.loader.tools.MainClassFinder$SingleMainClassCallback.access$100 (MainClassFinder.java:421) at org.springframework.boot.loader.tools.MainClassFinder.findSingleMainClass (MainClassFinder.java:205) at org.springframework.boot.loader.tools.Repackager.findMainMethod (Repackager.java:333) at org.springframework.boot.loader.tools.Repackager.findMainMethodWithTimeoutWarning (Repackager.java:322) at org.springframework.boot.loader.tools.Repackager.buildManifest (Repackager.java:293) at org.springframework.boot.loader.tools.Repackager.repackage (Repackager.java:238) at org.springframework.boot.loader.tools.Repackager.repackage (Repackager.java:195) at org.springframework.boot.maven.RepackageMojo.repackage (RepackageMojo.java:221) at org.springframework.boot.maven.RepackageMojo.execute (RepackageMojo.java:208) at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137) at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:208) at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:154) at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:146) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81) at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56) at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128) at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305) at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192) at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105) at org.apache.maven.cli.MavenCli.execute (MavenCli.java:954) at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288) at org.apache.maven.cli.MavenCli.main (MavenCli.java:192) 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:497) at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289) at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229) at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415) at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)
[ERROR]
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR]
[Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionExceptionC:\BackUp_2016_07_14\NonFilter\Projects\EAG\code\Spring\SpringBoot\EurekaServer1>




Now we have another error running jar with argument specifying the service.

Reason: we have not added the Main.java  class we have specified inside the POM. when we build the jar, earlier we had an issue with too many main methods. As we need to get rid of that we specifies a class which needs to be taken as a main class inside POM.
It build the jar without checking the existence of the class.

Solution :
So we create the Main.java class in the specified package.

Error is below when class in not present.

------------------------
Exception in thread "main" java.lang.ClassNotFoundException: io.pivotal.microservices.services.Main
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:93)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:46)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)



Tuesday, August 7, 2018

VB script to read and write file

1. To Read line by line in a given file



Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\listfile.txt",1)
Dim strLine
do while not objFileToRead.AtEndOfStream
     strLine = objFileToRead.ReadLine()
     'Do something with the line
loop
objFileToRead.Close
Set objFileToRead = Nothing

This will read "listfile.txt' line by line. 
Inside the loop you can do something you need
Let's say you need to write this output to another file.

Let's break the logic to few lines to understanding purposes.

1. create file object and assign to variable.

Set filesys = CreateObject("Scripting.FileSystemObject")


2. Declare the output file path

strOutputFile1= "C:\out.txt"

3. Open a file in writable format (8) and get an object reference

Set objReport1 = filesys.OpenTextFile(strOutputFile1, 8, True)

4. Inside the loop, write the reader content from other object line by line

objReport1.WriteLine strLine

5. close all open streams and close references

objReport1.Close

set objReport1 = nothing

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

Below is the complete vb script to read content from a file and write to another file

2. Read content from a given file line by line and do something and write back to another file



Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\emailList.txt",1)

' to write
Set filesys = CreateObject("Scripting.FileSystemObject")
strOutputFile1= "C:\out.txt"
Set objReport1 = filesys.OpenTextFile(strOutputFile1, 8, True)

Dim strLine
do while not objFileToRead.AtEndOfStream
     strLine = objFileToRead.ReadLine()
     'Do something with the line
 
'MsgBox strLine
' write
objReport1.WriteLine strLine
loop
objFileToRead.Close
Set objFileToRead = Nothing

'closing all for write
objReport1.Close

set objReport1 = nothing

Thursday, August 2, 2018

Some Useful SQL queries for MSSQL

 1.Check the Recovery model of the databases





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.