English 中文(简体)
Spring Boot CLI - Hello World Example
  • 时间:2024-11-03

Spring Boot CLI - Hello World Example


Previous Page Next Page  

In this example, we ll create a Spring Boot + MVC + Rest based Web apppcation.

Step 1: Create source Folder

Create a folder FirstApppcation in E:Test folder.

Step 2: Create Source File

Create FirstApppcation.groovy file in E:Test folder with following source code.


@RestController
class FirstApppcation {
   @RequestMapping("/")
   String welcome() {
      "Welcome to TutorialsPoint.Com"
   }
}

Step 3: Run the apppcation

Type the following command


E:/Test/> spring run FirstApppcation.groovy

Now Spring Boot CLI will come into action, download required dependencies, run the embedded tomcat, deploy the apppcation and start it. You can see the following output on console.


E:Test>spring run FirstApppcation.groovy
Resolving dependencies...............................

  .   ____          _            __ _ _
 /\ / ___ _ __ _ _(_)_ __  __ _    
( ( )\___ |  _ |  _| |  _ / _` |    
 \/  ___)| |_)| | | | | || (_| |  ) ) ) )
     |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.6.3)

2022-02-03 11:12:42.683  INFO 6956 --- [       runner-0] o.s.boot.SpringApppcation               : Starting apppcation using Java 11.0.11 on DESKTOP-86KD9FC with PID 6956 (started by intel in F:Test)
2022-02-03 11:12:42.710  INFO 6956 --- [       runner-0] o.s.boot.SpringApppcation               : No active profile set, falpng back to default profiles: default
2022-02-03 11:12:45.110  INFO 6956 --- [       runner-0] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initiapzed with port(s): 8080 (http)
2022-02-03 11:12:45.138  INFO 6956 --- [       runner-0] o.apache.catapna.core.StandardService   : Starting service [Tomcat]
2022-02-03 11:12:45.139  INFO 6956 --- [       runner-0] org.apache.catapna.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.56]
2022-02-03 11:12:45.229  INFO 6956 --- [       runner-0] org.apache.catapna.loader.WebappLoader  : Unknown class loader [org.springframework.boot.cp.compiler.ExtendedGroovyClassLoader$DefaultScopeParentClassLoader@8646db9] of class [class org.springframework.boot.cp.compiler.ExtendedGroovyClassLoader$DefaultScopeParentClassLoader]
2022-02-03 11:12:45.333  INFO 6956 --- [       runner-0] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initiapzing Spring embedded WebApppcationContext
2022-02-03 11:12:45.333  INFO 6956 --- [       runner-0] w.s.c.ServletWebServerApppcationContext : Root WebApppcationContext: initiapzation completed in 2124 ms
2022-02-03 11:12:46.901  INFO 6956 --- [       runner-0] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path   
2022-02-03 11:12:46.930  INFO 6956 --- [       runner-0] o.s.boot.SpringApppcation               : Started apppcation in 5.416 seconds (JVM running for 49.049)
2022-02-03 11:13:48.910  INFO 6956 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initiapzing Spring DispatcherServlet  dispatcherServlet 
2022-02-03 11:13:48.912  INFO 6956 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initiapzing Servlet  dispatcherServlet 
2022-02-03 11:13:48.915  INFO 6956 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initiapzation in 3 ms

Step 4: Browse the apppcation in Browser

Our spring based rest apppcation is now ready. Open url as "http://localhost:8080/" and you will see the following output.


Welcome to TutorialsPoint.Com

Points to consider

Following actions are taken by Spring CLI.

    All dependency JARs are downloaded for the first time only.

    Spring CLI automatically detects which dependency JARs are to be downloaded based on the classes and annotations used in code.

    Finally it compiles the code, deploy the war on a embedded tomcat, start embedded tomcat server on the default port 8080.

Advertisements