Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >SpringBoot官方笔记4Web

SpringBoot官方笔记4Web

作者头像
dongfanger
发布于 2023-07-20 12:09:47
发布于 2023-07-20 12:09:47
27700
代码可运行
举报
文章被收录于专栏:dongfangerdongfanger
运行总次数:0
代码可运行

Most web applications use the spring-boot-starter-web module to get up and running quickly. You can also choose to build reactive web applications by using the spring-boot-starter-webflux module.

Servlet Web Applications

Spring Web MVC Framework

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import java.util.List;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/users")
public class MyRestController {

    private final UserRepository userRepository;

    private final CustomerRepository customerRepository;

    public MyRestController(UserRepository userRepository, CustomerRepository customerRepository) {
        this.userRepository = userRepository;
        this.customerRepository = customerRepository;
    }

    @GetMapping("/{userId}")
    public User getUser(@PathVariable Long userId) {
        return this.userRepository.findById(userId).get();
    }

    @GetMapping("/{userId}/customers")
    public List<Customer> getUserCustomers(@PathVariable Long userId) {
        return this.userRepository.findById(userId).map(this.customerRepository::findByUser).get();
    }

    @DeleteMapping("/{userId}")
    public void deleteUser(@PathVariable Long userId) {
        this.userRepository.deleteById(userId);
    }

}

Static Content

By default, Spring Boot serves static content from a directory called /static (or /public or /resources or /META-INF/resources) in the classpath or from the root of the ServletContext.

Error Handling

By default, Spring Boot provides an /error mapping that handles all errors in a sensible way, and it is registered as a “global” error page in the servlet container.

CORS Support

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration(proxyBeanMethods = false)
public class MyCorsConfiguration {

    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {

            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/api/**");
            }

        };
    }

}

By default, the embedded server listens for HTTP requests on port 8080.

Reactive Web Applications

Spring WebFlux is the new reactive web framework introduced in Spring Framework 5.0. Unlike Spring MVC, it does not require the servlet API, is fully asynchronous and non-blocking, and implements the Reactive Streams specification through the Reactor project.

Spring Security

Spring Boot relies on Spring Security’s content-negotiation strategy to determine whether to use httpBasic or formLogin.

The basic features you get by default in a web application are:

  • UserDetailsService (or ReactiveUserDetailsService in case of a WebFlux application) bean with in-memory store and a single user with a generated password (see SecurityProperties.User for the properties of the user).
  • Form-based login or HTTP Basic security (depending on the Accept header in the request) for the entire application (including actuator endpoints if actuator is on the classpath).
  • DefaultAuthenticationEventPublisher for publishing authentication events.

OAuth2 is a widely used authorization framework that is supported by Spring.

Spring Session

When building a servlet web application, the following stores can be auto-configured:

参考资料: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#web

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-07-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
利用 Spring WebFlux 技术打造高效非阻塞 API 的完整开发方案与实践技巧
以下是一篇关于使用Spring WebFlux构建非阻塞API的技术方案和应用实例文章:
啦啦啦191
2025/08/12
5070
利用 Spring WebFlux 技术打造高效非阻塞 API 的完整开发方案与实践技巧
SpringBoot 物理线程、虚拟线程、Webflux 性能全面对比!
国产 Star 破 10w+ 的开源项目,前端包括管理后台 + 微信小程序,后端支持单体和微服务架构。
用户1220090
2025/05/10
5370
SpringBoot 物理线程、虚拟线程、Webflux 性能全面对比!
SpringBoot+JWT完成token验证
JWT官网: https://jwt.io/ JWT(Java版)的github地址:https://github.com/jwtk/jjwt
用户1212940
2022/04/13
1K0
SpringBoot+JWT完成token验证
Spring MVC 与 Spring Webflux 性能测试
本文翻译自国外论坛 medium,原文地址:本文翻译自国外论坛 medium,原文地址:https://medium.com/deno-the-complete-reference/spring-boot-vs-spring-webflux-performance-comparison-for-hello-world-case-386da4e9c418
wayn
2023/09/12
9610
Spring MVC 与 Spring Webflux 性能测试
SpringBoot使用WebFlux响应式编程操作数据库
在之前一篇简单介绍了WebFlux响应式编程的操作,我们在来看一下下图,可以看到,在目前的Spring WebFlux还没有支持类似Mysql这样的关系型数据库,所以本文以MongoDb数据库为例。
dalaoyang
2018/09/18
1.2K0
SpringBoot使用WebFlux响应式编程操作数据库
【微服务~远程调用】整合RestTemplate、WebClient、Feign
🔎这里是【微服务~远程调用】,关注我学习微服务不迷路 👍如果对你有帮助,给博主一个免费的点赞以示鼓励 欢迎各位🔎点赞👍评论收藏⭐️ 👀专栏介绍 【微服务~远程调用】 目前主要更新微服务,一起学习一起进步。 👀本期介绍 本期主要介绍远程调用整合整合RestTemplate、WebClient、Feign 文章目录 整合RestTemplate 整合WebClient WebClient和RestTemplate 响应式IO模型 WebClient入门 API详解 整合Feign 概述 整合Feign
陶然同学
2023/02/27
1.2K0
【微服务~远程调用】整合RestTemplate、WebClient、Feign
重学SpringBoot3-Spring WebFlux简介
随着微服务架构的流行和对高并发、低延迟系统需求的增加,响应式编程逐渐成为现代应用开发的主流方式之一。Spring Boot 3 引入了对响应式编程的强大支持,其中 Spring WebFlux 是一个重要的模块。本文将介绍 Spring WebFlux 的概念、特点以及它在实际应用中的场景和优势。
CoderJia
2024/10/20
2.1K0
重学SpringBoot3-Spring WebFlux简介
微服务架构之Spring Boot(三十五)
如果您开发使用超媒体的RESTful API,Spring Boot为Spring HATEOAS提供了适用于大多数应用程序的自动配置。自动配置取代了使
用户1289394
2022/04/07
6500
Spring Boot入门
Spring Boot入门: 使用IDEA创建Spring Boot 项目: 选择 勾选Web 生成项目结构: 例子: package com.example.testboot; import or
二十三年蝉
2018/02/28
8940
Spring Boot入门
【SpringBoot】SpringBoot:简化数据库操作与API开发
在现代应用开发中,高效的数据库操作和API开发是关键组成部分。SpringBoot作为一个强大的框架,通过其简化的配置和强大的功能,显著提升了开发效率。本文将深入探讨SpringBoot如何简化数据库操作与API开发,帮助开发者更快地构建高性能的应用。
E绵绵
2025/05/25
2270
Java 高级篇之 Springboot 框架详细解析与实战应用
我将为你详细介绍Spring Boot框架,涵盖其核心概念、关键技术,并结合应用实例展示开发过程,帮助你快速掌握并应用于实际项目。
啦啦啦191
2025/06/15
5810
Java 高级篇之 Springboot 框架详细解析与实战应用
这会是下一代的 Java 程序员的技术栈吗?
前面的文章给大家介绍了 SpringBoot 的应用 web 类型推断,并且结合了 Reactive web 类型给大家分析了一下 SpringBoot 是如何进行 web 类型推断和创建内置 web 服务器的。
Java极客技术
2023/02/23
7200
这会是下一代的 Java 程序员的技术栈吗?
spring-boot-starter-data-jpa + SQLite简单例子(含全部代码)
SQLite官网:http://www.sqlite.org/ SQLite是比 Access 更优秀的文件型数据库,支持复杂的 SQL 语句,支持索引、触发器,速度很快,开源等。
程裕强
2021/11/18
5.1K0
spring-boot-starter-data-jpa + SQLite简单例子(含全部代码)
Spring Framework 历史漏洞研究
本文的主要目标是分析、总结、归纳历史上出现过的 Spring 框架漏洞,从而尝试找出其中的潜在模式,以达到温故知新的目的。当然作为一个 Java 新手,在直接分析漏洞之前,还是会先从开发者的角度去学习 Spring 中的一些核心概念,从而为后续的理解奠定基础。
evilpan
2023/04/28
1.2K0
springboot之整合JPA
1、新建一个springboot项目,选择web、data jdbc、data jpa、mysql driver。
西西嘛呦
2020/08/26
5160
springboot之整合JPA
使用Spring Boot实现博客管理系统
博客管理系统在内容创作和分享中扮演着重要角色。它能够帮助用户方便地发布、编辑、管理和分享博客文章。Spring Boot通过其简便的配置和强大的功能支持,使得开发一个高效的博客管理系统变得非常容易。本文将详细探讨如何使用Spring Boot实现一个博客管理系统,并提供具体的代码示例和应用案例。
E绵绵
2024/07/03
4040
SpringBoot使用矩阵传参
对于上面的请求url,我们只需要使用@RequestParam注解就可以将其获取,十分简单。
半月无霜
2023/03/03
4070
SpringBoot使用矩阵传参
SpringBoot Web开发精解
当在 Spring Boot 中引入 Web 模块时,SpringBoot 会帮我们自动配置 Web 相关的组件,其中 Spring MVC 便是最重要的部分。
堆栈哲学
2023/03/08
7100
SpringBoot Web开发精解
Springboot整合web相关技术
它是基于Servlet 技术实现的, 简单的来说,过滤器就是起到过滤的作用,在web项目开发中帮我们过滤一些指定的 url做一些特殊的处理
上分如喝水
2021/08/16
9060
Springboot整合web相关技术
2025 年 Java 学习笔记从入门到精通完整版教程
IDE推荐使用IntelliJ IDEA 2025.1或VS Code(需安装Extension Pack for Java)。
啦啦啦191
2025/06/27
2730
2025 年 Java 学习笔记从入门到精通完整版教程
相关推荐
利用 Spring WebFlux 技术打造高效非阻塞 API 的完整开发方案与实践技巧
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档