Java Spring Boot 3 Web Hello World with Gradle in IntelliJ


Java Spring Boot Web Hello World with Gradle Example
Pretext:

Spring Boot has made developing Java projects very easy, as it does take care of the configurations and boilerplate code, if you are new to Spring Boot and what to just see how to run a Hello World! with Spring Boot Web project with Gradle, you are at the right place.

Prerequisite:
  • Must have Java 8 or + installed,
  • IDEA IntelliJ IDE installed,
  • Basic understanding of Java and related frameworks like Spring,
  • Basic understanding of Gradle.


Steps to run Hello World! Program using String Web

  1. Open IntelliJ,
  2. Click on New Project,
    New IntelliJ Project
  3. Now Select Spring Initializr, choose your Java 8 or above SDK, and keep rest as it and hit next,
    Java Spring Initializr Project
  4. Now Select Project Type: Gradle Project and Select the Java Version (same that you selected in the previous step), keep the rest of the configurations as it is,
    New Project Spring Gradle Java 8
    Project properties
    Group Id: com.example
    Artifact Id: demo
    Version: 0.0.1-SNAPSHOT
    Project type: Gradle Project
    Language: Java
    Packaging: Jar
    Java version: 8
    Project name: demo
    Project description: Demo project for Spring Boot
    Package name: com.example.demo
  5. Now, Select Web -> Spring Web, Note as 3.0.0 (M4) is the latest version we will select that, click Next
    Spring Web Project
  6. Now you should see Gradle build scripts found, just click on Load Gradle Project,
    Gradle build scripts found
    , wait for it to complete,
  7. Now under the Project: src -> main -> java -> com -> example -> demo -> Create a new Java Class: HelloController,
     HelloController
  8. HelloController.java
    package com.example.demo;
    
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class HelloController {
    
        @RequestMapping("/")
        public String hello()
        {
            return "Hello from Spring Web!";
        }
    }
    
  9. Now run the DemoApplication class which contains the main method,
    Run DemoApplication.main
    Console logs:
    12:05:27 PM: Executing ':DemoApplication.main()'...
    
    > Task :compileJava
    > Task :processResources UP-TO-DATE
    > Task :classes
    
    > Task :DemoApplication.main()
    
      .   ____          _            __ _ _
     /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
    ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
     \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
      '  |____| .__|_| |_|_| |_\__, | / / / /
     =========|_|==============|___/=/_/_/_/
     :: Spring Boot ::                (v3.0.0)
    
    2022-08-26 12:06:29.116  INFO 9488 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication using Java 1.8.0_332 on Code2cares-MacBook-Air.local with PID 9488 (/Users/code2care/IdeaProjects/demo/build/classes/java/main started by code2care in /Users/code2care/IdeaProjects/demo)
    2022-08-26 12:06:29.121  INFO 9488 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to 1 default profile: "default"
    2022-08-26 12:06:31.521  INFO 9488 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
    2022-08-26 12:06:31.540  INFO 9488 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
    2022-08-26 12:06:31.540  INFO 9488 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.64]
    2022-08-26 12:06:31.687  INFO 9488 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
    2022-08-26 12:06:31.687  INFO 9488 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2365 ms
    2022-08-26 12:06:32.918  INFO 9488 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
    2022-08-26 12:06:32.949  INFO 9488 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 4.663 seconds (JVM running for 5.386)
    

    You must see in the logs: Tomcat started on port(s): 8080 (http)

  10. Now, open your web-browser (Safari/Chrome/Firefox) and type in the address: http://localhost:8080

    You should see the message: Hello from Spring Boot! displayed.

Java Spring Boot Web Hello World with Gradle Example
Advertisements
Try Out Code2care Dev Tools:

Advertisements

Advertisements
Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and software developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include learnings, tutorials, trouble-shooting steps, video tutorials, code snippets, how-to, blogs, articles, etc.

🎉 We are celebrating the 10th years of Code2care! Thank you for all your support!

We strongly support Gender Equality & Diversity.