or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

configuration.mdcontrollers.mdembedded-server.mderror-handling.mdhttp-clients.mdindex.mdjson-processing.mdstatic-resources.mdtesting.md
tile.json

tessl/maven-org-springframework-boot--spring-boot-starter-web

Starter for building web, including RESTful, applications using Spring MVC with embedded Tomcat server.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/org.springframework.boot/spring-boot-starter-web@3.5.x

To install, run

npx @tessl/cli install tessl/maven-org-springframework-boot--spring-boot-starter-web@3.5.0

index.mddocs/

Spring Boot Starter Web

Spring Boot Starter Web is a comprehensive dependency starter that provides all necessary components for building web applications using Spring MVC framework. It includes embedded Tomcat server, JSON processing capabilities, and auto-configuration for RESTful services and traditional web applications.

Package Information

  • Package Name: spring-boot-starter-web
  • Package Type: maven
  • Language: Java
  • Group ID: org.springframework.boot
  • Artifact ID: spring-boot-starter-web
  • Installation: Add dependency to Maven/Gradle build file

Maven

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

Gradle

implementation 'org.springframework.boot:spring-boot-starter-web:3.5.3'

Core Imports

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.ResponseEntity;

Basic Usage

@SpringBootApplication
public class WebApplication {
    public static void main(String[] args) {
        SpringApplication.run(WebApplication.class, args);
    }
}

@RestController
@RequestMapping("/api")
public class UserController {
    
    @GetMapping("/users/{id}")
    public ResponseEntity<User> getUser(@PathVariable Long id) {
        User user = userService.findById(id);
        return ResponseEntity.ok(user);
    }
    
    @PostMapping("/users")
    public ResponseEntity<User> createUser(@RequestBody User user) {
        User created = userService.save(user);
        return ResponseEntity.status(201).body(created);
    }
}

Architecture

Spring Boot Starter Web is built around several key components:

  • Auto-Configuration: Automatically configures Spring MVC, embedded server, and related components
  • Embedded Server: Tomcat server by default (Jetty/Undertow alternatives available)
  • Spring MVC Framework: Full MVC capabilities with annotation-driven development
  • Content Negotiation: Automatic JSON/XML serialization based on request headers
  • Error Handling: Built-in error pages and API error responses
  • Static Resource Handling: Serves static content with caching and versioning support

Capabilities

Controller Development

Create RESTful web services and traditional MVC controllers using annotations.

@RestController
@RequestMapping("/api")
public class ApiController {
    @GetMapping("/resource/{id}")
    public ResponseEntity<Resource> getResource(@PathVariable Long id);
    
    @PostMapping("/resource")
    public ResponseEntity<Resource> createResource(@RequestBody Resource resource);
}

Controller Development

Configuration Properties

Configure web application behavior through application properties.

// Core server configuration
server.port=8080
server.servlet.context-path=/myapp

// MVC configuration  
spring.mvc.static-path-pattern=/**
spring.mvc.async.request-timeout=30s

// Multipart file uploads
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=50MB

Configuration Properties

Error Handling

Built-in error handling with customizable error pages and API responses.

@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(ResourceNotFoundException.class)
    public ResponseEntity<ErrorResponse> handleNotFound(ResourceNotFoundException ex);
    
    @ExceptionHandler(ValidationException.class) 
    public ResponseEntity<ErrorResponse> handleValidation(ValidationException ex);
}

Error Handling

Static Resources

Serve static content like CSS, JavaScript, and images with built-in caching.

// Default static resource locations
// classpath:/META-INF/resources/
// classpath:/resources/
// classpath:/static/
// classpath:/public/

// Configuration
spring.web.resources.static-locations=classpath:/custom-static/
spring.web.resources.cache.period=3600

Static Resources

JSON Processing

Automatic JSON serialization and deserialization using Jackson library.

@Autowired
private ObjectMapper objectMapper;

// Automatic JSON conversion for request/response bodies
@PostMapping("/users")
public ResponseEntity<User> createUser(@RequestBody User user) {
    return ResponseEntity.ok(user);
}

JSON Processing

Bean Validation

Built-in support for JSR-303/349/380 Bean Validation with automatic validation of request bodies.

// Automatic validation with @Valid annotation
@PostMapping("/users")
public ResponseEntity<User> createUser(@Valid @RequestBody User user, BindingResult result) {
    if (result.hasErrors()) {
        return ResponseEntity.badRequest().body("Validation failed");
    }
    return ResponseEntity.ok(user);
}

// Validation annotations on model classes
public class User {
    @NotBlank(message = "Name is required")
    @Size(min = 2, max = 50)
    private String name;
    
    @Email(message = "Invalid email format")
    private String email;
}

Web Testing

Comprehensive testing support for web controllers and integration testing.

@WebMvcTest(UserController.class)
class UserControllerTest {
    @Autowired
    private MockMvc mockMvc;
    
    @MockBean
    private UserService userService;
    
    @Test
    void shouldGetUser() throws Exception {
        mockMvc.perform(get("/users/1"))
                .andExpect(status().isOk());
    }
}

Web Testing

HTTP Clients

Auto-configured RestTemplate and RestClient for making HTTP requests to external services.

@Autowired
private RestTemplateBuilder restTemplateBuilder;

// Create configured RestTemplate
RestTemplate restTemplate = restTemplateBuilder
        .rootUri("https://api.example.com")
        .setConnectTimeout(Duration.ofSeconds(5))
        .build();

// Make HTTP requests
User user = restTemplate.getForObject("/users/{id}", User.class, 1L);

HTTP Clients

Embedded Server

Configure and customize the embedded Tomcat server.

@Bean
public WebServerFactoryCustomizer<TomcatServletWebServerFactory> tomcatCustomizer() {
    return factory -> {
        factory.setPort(9090);
        factory.setContextPath("/app");
    };
}

Embedded Server

Core Types

// HTTP response wrapper
public class ResponseEntity<T> {
    public static <T> ResponseEntity<T> ok(T body);
    public static <T> ResponseEntity<T> status(HttpStatus status);
    public static ResponseEntity<?> noContent();
}

// HTTP status codes
public enum HttpStatus {
    OK(200), CREATED(201), NO_CONTENT(204),
    BAD_REQUEST(400), NOT_FOUND(404), INTERNAL_SERVER_ERROR(500);
}

// Request/Response body binding
public interface HttpMessageConverter<T> {
    boolean canRead(Class<?> clazz, MediaType mediaType);
    boolean canWrite(Class<?> clazz, MediaType mediaType);
    T read(Class<? extends T> clazz, HttpInputMessage inputMessage);
    void write(T t, MediaType contentType, HttpOutputMessage outputMessage);
}