Spring Boot Web
Spring Boot Web is the foundation for building web applications and RESTful services in Spring Boot. It provides an embedded servlet container (Tomcat, Jetty, or Undertow) and simplifies web development by eliminating the need for XML configuration and reducing boilerplate code.
Key Features
Built-in support for Tomcat, Jetty, and Undertow servlet containers, allowing you to run applications as standalone JAR files.
Simple annotation-based approach to create REST endpoints with @RestController and @RequestMapping annotations.
Automatic configuration of Spring MVC components, message converters, and view resolvers based on classpath dependencies.
Automatic serving of static resources from classpath locations like /static, /public, /resources, and /META-INF/resources.
Use Cases
- RESTful APIs: Building REST services for mobile and web applications
- Web Applications: Creating traditional web applications with server-side rendering
- Microservices: Developing lightweight microservices with embedded containers
- API Gateways: Building API gateway services that route and transform requests
- Backend Services: Creating backend services for single-page applications
Example Usage
A simple REST controller example:
@RestController
@RequestMapping("/api/users")
public class UserController {
@GetMapping("/{id}")
public ResponseEntity<User> getUser(@PathVariable Long id) {
User user = userService.findById(id);
return ResponseEntity.ok(user);
}
@PostMapping
public ResponseEntity<User> createUser(@RequestBody User user) {
User saved = userService.save(user);
return ResponseEntity.status(HttpStatus.CREATED).body(saved);
}
}
Dependencies
Add the following dependency to your pom.xml or build.gradle:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Configuration
Customize server settings in application.properties:
server.port=8080
server.servlet.context-path=/api
spring.mvc.pathmatch.matching-strategy=ant_path_matcher