From c75065d06ef895ec1d32fb1cb2b88fe42b3206bd Mon Sep 17 00:00:00 2001 From: Bunty Raghani Date: Mon, 3 May 2021 01:00:04 +0530 Subject: [PATCH] add spring boot hello world code --- .gitignore | 18 +++---- README.md | 53 ++++++++++++++++++- pom.xml | 49 +++++++++++++++++ .../helloworld/HelloWorldApplication.java | 13 +++++ .../controller/HelloWorldController.java | 13 +++++ src/main/resources/application.properties | 2 + .../HelloWorldApplicationTests.java | 23 ++++++++ .../controller/HelloWorldControllerTest.java | 27 ++++++++++ 8 files changed, 184 insertions(+), 14 deletions(-) create mode 100644 pom.xml create mode 100644 src/main/java/com/example/helloworld/HelloWorldApplication.java create mode 100644 src/main/java/com/example/helloworld/controller/HelloWorldController.java create mode 100644 src/main/resources/application.properties create mode 100644 src/test/java/com/example/helloworld/HelloWorldApplicationTests.java create mode 100644 src/test/java/com/example/helloworld/controller/HelloWorldControllerTest.java diff --git a/.gitignore b/.gitignore index a1c2a238..b3fae007 100644 --- a/.gitignore +++ b/.gitignore @@ -4,20 +4,14 @@ # Log file *.log -# BlueJ files -*.ctxt - -# Mobile Tools for Java (J2ME) -.mtj.tmp/ - -# Package Files # +# Package Files *.jar -*.war -*.nar -*.ear *.zip *.tar.gz *.rar -# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml -hs_err_pid* +# Generated Sources +target + +# IDE Files +.idea \ No newline at end of file diff --git a/README.md b/README.md index a67d7dac..b9f38350 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,51 @@ -# spring-boot-hello-world -A simple Spring Boot app to send hello world message to a user +# Spring Boot Hello World + +**A simple Spring Boot app to send hello world message to a user** + +## How to Run Application + +**Start the application using any of the commands mentioned below** + +> **Note:** First two commands need to run inside the root folder of this project i.e inside the **spring-boot-hello-world** folder + + +- **Using maven**
``` mvn spring-boot:run``` + + +- **From jar file** + Create a jar file using '**mvn clean install**' command and then execute +
```java -jar target/hello-world-1.0.1-SNAPSHOT.jar``` + + +- **Directly from IDE** +
```Right click on HelloWorldApplication.java and click on 'Run' option``` +

+ +> **Note:** By default spring boot application starts on port number 8080. If port 8080 is occupied in your system then you can change the port number by uncommenting and updating the **server.port** property inside the **application.properties** file that is available inside the **src > main > resources** folder. + +
+ +**Send an HTTP GET request to '/hello' endpoint using any of the two methods** + +- **Browser or REST client** +
```http://localhost:8080/hello``` + + +- **cURL** +
```curl --request GET 'http://localhost:8080/hello'``` + + +## How to Run Unit Test Cases + +**Run the test cases using any of the commands mentioned below** + +> **Note:** These commands need to run inside the root folder of this project i.e inside the **spring-boot-hello-world** folder + +- **To run all the test cases** +
```mvn test``` + + +- **To run a particular test class** +
```mvn -Dtest=HelloWorldControllerTest test``` +
or +
```mvn -Dtest=HelloWorldApplicationTests test``` \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 00000000..bdf8307a --- /dev/null +++ b/pom.xml @@ -0,0 +1,49 @@ + + + 4.0.0 + + + org.springframework.boot + spring-boot-starter-parent + 2.4.5 + + + com.example + hello-world + 1.0.1-SNAPSHOT + + hello-world + A simple Spring Boot app to send hello world message to a user + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/src/main/java/com/example/helloworld/HelloWorldApplication.java b/src/main/java/com/example/helloworld/HelloWorldApplication.java new file mode 100644 index 00000000..478ef261 --- /dev/null +++ b/src/main/java/com/example/helloworld/HelloWorldApplication.java @@ -0,0 +1,13 @@ +package com.example.helloworld; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class HelloWorldApplication { + + public static void main(String[] args) { + SpringApplication.run(HelloWorldApplication.class, args); + } + +} diff --git a/src/main/java/com/example/helloworld/controller/HelloWorldController.java b/src/main/java/com/example/helloworld/controller/HelloWorldController.java new file mode 100644 index 00000000..27a3dcfe --- /dev/null +++ b/src/main/java/com/example/helloworld/controller/HelloWorldController.java @@ -0,0 +1,13 @@ +package com.example.helloworld.controller; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class HelloWorldController { + + @GetMapping("/hello") + public String sendGreetings() { + return "Hello, World!"; + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 00000000..145d8319 --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1,2 @@ +# configuring port number on which application should run +#server.port=8081 \ No newline at end of file diff --git a/src/test/java/com/example/helloworld/HelloWorldApplicationTests.java b/src/test/java/com/example/helloworld/HelloWorldApplicationTests.java new file mode 100644 index 00000000..35bba04e --- /dev/null +++ b/src/test/java/com/example/helloworld/HelloWorldApplicationTests.java @@ -0,0 +1,23 @@ +package com.example.helloworld; + +import com.example.helloworld.controller.HelloWorldController; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + + +@SpringBootTest +class HelloWorldApplicationTests { + + @Autowired + private HelloWorldController helloWorldController; + + @Test + void contextLoads() { + // to ensure that controller is getting created inside the application context + assertNotNull(helloWorldController); + } + +} diff --git a/src/test/java/com/example/helloworld/controller/HelloWorldControllerTest.java b/src/test/java/com/example/helloworld/controller/HelloWorldControllerTest.java new file mode 100644 index 00000000..cc924234 --- /dev/null +++ b/src/test/java/com/example/helloworld/controller/HelloWorldControllerTest.java @@ -0,0 +1,27 @@ +package com.example.helloworld.controller; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.web.servlet.MockMvc; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@SpringBootTest +@AutoConfigureMockMvc +public class HelloWorldControllerTest { + + @Autowired + private MockMvc mockMvc; + + @Test + public void shouldReturnExpectedMessage() throws Exception { + + mockMvc.perform(get("/hello")) + .andExpect(status().isOk()) + .andExpect(content().string("Hello, World!")); + } +}