ZetCode

Spring Boot WebApplicationType

最后修改时间:2023年7月20日

Spring Boot WebApplicationType 教程介绍了 Spring Boot 应用程序中各种类型的 Web 应用程序。该示例演示了如何设置 WebApplicationType。

Spring Boot 是一个流行的应用程序框架,用于在 Java、Kotlin 或 Groovy 中创建企业应用程序。

WebApplicationType

WebApplicationType 是 Web 应用程序可能类型的枚举。 有三个可能的值

Spring Boot 示例

在以下应用程序中,我们定义了 Spring Boot 应用程序的 Web 应用程序类型。

build.gradle 
...
src
├── main
│   ├── java
│   │   └── com
│   │       └── zetcode
│   │           └── Application.java
│   └── resources
└── test
    └── java

这是 Spring Boot 应用程序的项目结构。

build.gradle
plugins {
    id 'java'
    id 'org.springframework.boot' version '3.1.1'
    id 'io.spring.dependency-management' version '1.1.0'
}

group = 'com.zetcode'
version = '0.0.1-SNAPSHOT'

java {
    sourceCompatibility = '17'
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-webflux'
    implementation 'org.springframework.boot:spring-boot-starter-web'
}

test {
    useJUnitPlatform()
}

build.gradle 文件中,我们有经典 servlet 和响应式 Web 应用程序的依赖项。

com/zetcode/Application.java
package com.zetcode;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;

import static org.springframework.web.reactive.function.BodyInserters.fromValue;
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
import static org.springframework.web.reactive.function.server.ServerResponse.ok;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {

        new SpringApplicationBuilder(Application.class)
                .web(WebApplicationType.SERVLET)
                .run(args);
    }
}

@RestController
class MyController {

    @GetMapping("/")
    public String hello() {

        return "Home page";
    }
}

@Configuration
class MyRoutes {

    @Bean
    RouterFunction<ServerResponse> about() {
        return route(GET("/about"), request -> ok().body(fromValue("About page")));
    }
}

@Component
class MyRunner implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {

        System.out.println("Hello there!");
    }
}

Application 中,我们定义了 Spring Boot 应用程序并设置了一个经典 Web 接口、一个响应式路由和一个命令行运行器。

new SpringApplicationBuilder(Application.class)
    .web(WebApplicationType.SERVLET)
    .run(args);

我们使用 SpringApplicationBuilder 定义 Web 应用程序类型。 对于 WebApplicationType.SERVLET,响应式路由不可用。

$ ./gradlew bootRun

我们启动应用程序。

$ curl localhost:8080/
Home page

经典的 servlet 接口处于活动状态。

在本文中,我们使用了 Spring Boot WebApplicationType

作者

我的名字是 Jan Bodnar,我是一个充满激情的程序员,拥有丰富的编程经验。 自 2007 年以来,我一直在撰写编程文章。 迄今为止,我撰写了 1,400 多篇文章和 8 本电子书。 我拥有超过十年的编程教学经验。

列出 所有 Spring Boot 教程