首页 > 其他分享 >SpringBoot官方笔记4Web

SpringBoot官方笔记4Web

时间:2023-07-17 22:57:29浏览次数:42  
标签:web SpringBoot Spring 4Web userId 笔记 springframework import public

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

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

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:

  • Redis

  • JDBC

  • Hazelcast

  • MongoDB

参考资料:

https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#web

标签:web,SpringBoot,Spring,4Web,userId,笔记,springframework,import,public
From: https://www.cnblogs.com/df888/p/17561522.html

相关文章

  • SpringBoot官方笔记3核心
    SpringApplicationBydefault, INFO loggingmessagesareshown,includingsomerelevantstartupdetails,suchastheuserthatlaunchedtheapplication.LazyInitializationWhenlazyinitializationisenabled,beansarecreatedastheyareneededratherth......
  • SpringBoot官方笔记7IO
    CachingSpringBootauto-configuresthecacheinfrastructureaslongascachingsupportisenabledbyusingthe @EnableCaching annotation.importorg.springframework.cache.annotation.Cacheable;importorg.springframework.stereotype.Component;@Component......
  • SpringBoot官方笔记6消息
    TheSpringFrameworkprovidesextensivesupportforintegratingwithmessagingsystems,fromsimplifieduseoftheJMSAPIusing JmsTemplate toacompleteinfrastructuretoreceivemessagesasynchronously.SpringAMQPprovidesasimilarfeaturesetforth......
  • SpringBoot官方笔记5Data
    SpringBootintegrateswithanumberofdatatechnologies,bothSQLandNoSQL.SQLDatabasesSpringData providesanadditionalleveloffunctionality:creating Repository implementationsdirectlyfrominterfacesandusingconventionstogeneratequeries......
  • SpringBoot官方笔记8其他
    ContainerImagesFROMeclipse-temurin:17-jreasbuilderWORKDIRapplicationARGJAR_FILE=target/*.jarCOPY${JAR_FILE}application.jarRUNjava-Djarmode=layertools-jarapplication.jarextractFROMeclipse-temurin:17-jreWORKDIRapplicationCOPY--from=......
  • 决策单调性优化DP 学习笔记 & P4767 [IOI2000] 邮局 题解
    0.题面题目描述高速公路旁边有一些村庄。高速公路表示为整数轴,每个村庄的位置用单个整数坐标标识。没有两个在同样地方的村庄。两个位置之间的距离是其整数坐标差的绝对值。邮局将建在一些,但不一定是所有的村庄中。为了建立邮局,应选择他们建造的位置,使每个村庄与其最近的邮局......
  • 组合数学学习笔记
    组合数学学习笔记组合数学常用公式基本公式排列:\[A_{n}^r=\frac{n!}{(n-r)!}\]组合:\[C_{n}^r=\frac{n!}{r!(n-r)!}\\\dbinom{n}{r}=\frac{n!}{r!(n-r)!}\]组合公式杨辉恒等式......
  • 第一篇博客 练习typora笔记
    学习MarkDown字体helloworld!helloworld!helloworld!helloworld! 引用 乐交诤友不交损友 分割线 图片  超链接点击跳转到百度 列表ABC 无序列表ABC 列表姓名性別年齡張三男18 代碼publicvoid......
  • springboot下使用rabbitMQ之开发配置方式(一)
    springboot下使用rabbitMQ之开发配置方式(一)距离上次发布博客已经小一年了,这次...嗯,没错,我又回来啦.........
  • C++笔记(2)——函数
    六.函数6.1函数基础一个典型的函数(function)定义包括:返回类型(returntype)、函数名字,由0或多个形参(parameter)组成的列表以及函数体。我们通过调用运算符来执行函数,形式为"()"。函数调用完成两项工作:一是用实参初始化函数对应的形参,二是将控制权转移给被调用函数。此时,主调......