Skip to content

Commit

Permalink
add spring boot hello world code
Browse files Browse the repository at this point in the history
  • Loading branch information
Bunty Raghani committed May 2, 2021
1 parent 5052615 commit c75065d
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 14 deletions.
18 changes: 6 additions & 12 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
53 changes: 51 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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** <br/>``` mvn spring-boot:run```


- **From jar file**
Create a jar file using '**mvn clean install**' command and then execute
<br/>```java -jar target/hello-world-1.0.1-SNAPSHOT.jar```


- **Directly from IDE**
<br/>```Right click on HelloWorldApplication.java and click on 'Run' option```
<br/><br/>

> **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.
<br/>

**Send an HTTP GET request to '/hello' endpoint using any of the two methods**

- **Browser or REST client**
<br/>```http://localhost:8080/hello```


- **cURL**
<br/>```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**
<br/>```mvn test```


- **To run a particular test class**
<br/>```mvn -Dtest=HelloWorldControllerTest test```
<br/>or
<br/>```mvn -Dtest=HelloWorldApplicationTests test```
49 changes: 49 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

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

<groupId>com.example</groupId>
<artifactId>hello-world</artifactId>
<version>1.0.1-SNAPSHOT</version>

<name>hello-world</name>
<description>A simple Spring Boot app to send hello world message to a user</description>

<properties>
<java.version>1.8</java.version>
</properties>

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

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

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

</project>
13 changes: 13 additions & 0 deletions src/main/java/com/example/helloworld/HelloWorldApplication.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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!";
}
}
2 changes: 2 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# configuring port number on which application should run
#server.port=8081
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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!"));
}
}

0 comments on commit c75065d

Please sign in to comment.