AUTO-CONFIG
If so, you probably do NOT want Tomcat but instead want embedded Jetty. Spring Boot handles that for you.
If so, there are a few beans that must always be added to your application context. Spring Boot adds them for you.
1 https://start.spring.io/
2 Chooose Grade || Maven
3 Dependency < Spring Web
4 Generate
package com.example.springboot;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
// Ready to handle Web request
// @RestController combines @Controller and @ResponseBody - When called returns data not view
@RestController
public class HelloController {
// Maps / to index() method
@GetMapping("/")
public String index() {
return "Greetings from Spring Boot!";
}
}
package com.example.springboot;
import java.util.Arrays;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
/*
@SpringBootApplication Adds
- @Configuration : Tags class as source of bean def. for application context
- @EnableAutoConfiguration : Add beans based on classpath settings
- @ComponentScan : Look for components in package to find controller
*/
@SpringBootApplication
public class Application {
public static void main(String[] args) {
//Launches App. (no web.xml)
SpringApplication.run(Application.class, args);
}
//Retrieves all the beans that were created by your application and runs them on start up
@Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
return args -> {
System.out.println("Let's inspect the beans provided by Spring Boot:");
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
System.out.println(beanName);
}
};
}
}
# Gradle
./gradlew bootRun
# Maven
./mvnw spring-boot:run
# Check Server
$ curl localhost:8080
Greetings from Spring Boot!
Add unit test dependency to Gradle/Maven
package com.example.springboot;
import static org.hamcrest.Matchers.equalTo;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
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.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
@SpringBootTest
@AutoConfigureMockMvc
public class HelloControllerTest {
//MockMvc - import from SpringTest. Sends HTTPRequest into DispatcherServlet and make assertions about result.
@Autowired
private MockMvc mvc;
@Test
public void getHello() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string(equalTo("Greetings from Spring Boot!")));
}
}
Spring Boot provides - HTTP endpoints or with JMX. Auditing, health, and metrics gathering management services with actuator module.
Actuator : manufacturing term that refers to a mechanical device for moving or controlling something. Actuators can generate a large amount of motion from a small change.
# Startup server and check
$ curl localhost:8080/actuator/health
{"status":"UP"}
https://spring.io/guides/gs/spring-boot/
https://docs.spring.io/spring-boot/docs/2.5.0/reference/htmlsingle/#getting-started