How Spring Boot uses the Jetty container

How Spring Boot uses the Jetty container

Jetty is a lightweight Java web container for web applications of all sizes. In Spring Boot, we can use Jetty as a web container to handle HTTP requests and responses. This article will introduce how Spring Boot uses the Jetty container, including how to configure the Jetty container, how to handle HTTP requests and responses, and provide corresponding code examples.

insert image description here

Add Jetty dependency

Before using Jetty as the Spring Boot web container, we need to add the Jetty dependency to the project. The following dependencies can be added to the Maven or Gradle build tools:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <!-- 排除默认的 Tomcat 依赖 -->
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

In this example, we've used spring-boot-starter-webthe dependency to enable Spring Boot Web functionality, exclusionsexcluded the default Tomcat dependency using the tag, and then added the Jetty dependency. This makes it possible to use Jetty as the web container.

Configure the Jetty container

When Spring Boot uses Jetty as the web container, you application.propertiescan application.ymlconfigure the Jetty container in or . Here is an example:

server:
  port: 8080
  jetty:
    acceptors: 2
    selectors: 4
    max-http-post-size: 1048576
    request-header-size: 8192

In this example, we have used jetty.acceptorsthe attribute to set the number of Jetty acceptors, jetty.selectorsthe attribute to set the number of Jetty selectors, jetty.max-http-post-sizethe attribute to set the maximum size of the HTTP POST request, and jetty.request-header-sizethe attribute to set the maximum size of the request header. Through these property configurations, we can flexibly control the behavior of the Jetty container.

Handle HTTP requests and responses

In Spring Boot, we can use @Controller, @RestController, @RequestMappingand other annotations to handle HTTP requests and responses. Here is an example:

@RestController
@RequestMapping("/api")
public class ApiController {
    
    

    @GetMapping("/hello")
    public String hello() {
    
    
        return "Hello, world!";
    }

}

In this example, we annotated the ApiController class with @RestControllerannotations and @RequestMappingset the request path with annotations. A hello() method is defined in the class to handle GET requests. When the user sends a /api/hello request, Spring Boot will automatically call this method and return the "Hello, world!" string.

Render the view using a templating engine

In addition to handling HTTP requests and responses, Spring Boot also supports the use of templating engines to render views. Commonly used template engines include Thymeleaf, Freemarker, Velocity, etc. Here's an example of a view rendered with Thymeleaf:

@Controller
public class ViewController {
    
    

    @GetMapping("/index")
    public String index(Model model) {
    
    
        model.addAttribute("message", "Hello, world!");
        return "index";
    }

}

In this example, we use @Controllerannotations annotate the ViewController class and define an index() method in the class to handle GET requests. In this method, we use Modelthe parameter to pass the data, and then return a view name. In this example, the view name is "index", which means that the template file named "index.html" will be rendered using the Thymeleaf template engine. In template files, you can use Thymeleaf's syntax to dynamically generate HTML content. Here is an example of a simple Thymeleaf template:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Thymeleaf Example</title>
</head>
<body>
    <h1 th:text="${message}"></h1>
</body>
</html>

In this example, we use Thymeleaf's th:textproperty to output the "Hello, world!" string. When the view is rendered, Thymeleaf will automatically fill the value of messagethe parameter th:textinto the attribute, generating the final HTML content.

Complete sample code

The following is a complete Spring Boot sample code using the Jetty container:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
public class Application {
    
    

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

}

@RestController
@RequestMapping("/api")
public class ApiController {
    
    

    @GetMapping("/hello")
    public String hello() {
    
    
        return "Hello, world!";
    }

}

@Controller
public class ViewController {
    
    

    @GetMapping("/index")
    public String index(Model model) {
    
    
        model.addAttribute("message", "Hello, world!");
        return "index";
    }

}

In this example, we created a Spring Boot application called "Application" and defined an ApiController and a ViewController class for handling HTTP requests and responses, respectively. At the same time, we also added a Jetty dependency to use the Jetty container as a web container to handle HTTP requests and responses.

Summarize

In this article, we introduced how Spring Boot uses the Jetty container, including how to add Jetty dependencies, how to configure the Jetty container, how to handle HTTP requests and responses, and provide corresponding code examples. Using Jetty as a Spring Boot web container can improve the performance and stability of web applications, while also giving you the flexibility to control the behavior of the container. Hope this article helps you.

Guess you like

Origin blog.csdn.net/it_xushixiong/article/details/131303883