赞
踩
前言
本文适合具有springboot的基础的同学。
Java 17或更高版本
Gradle 7.5+或Maven 3.5+
META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
SpringMVC 5.2 以后 允许我们使用函数式的方式,定义Web的请求处理流程。
Web请求处理的方式:
场景:User RESTful - CRUD
● GET /user/1 获取1号用户
● POST /user 请求体携带JSON,新增一个用户
● DELETE /user/1 删除1号用户
● RouterFunction - 路由函数
● RequestPredicate - 请求谓词
● ServerRequest - 请求
● ServerResponse -响应
##3、 代码示例
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.MediaType; import org.springframework.web.servlet.function.RequestPredicate; import org.springframework.web.servlet.function.RouterFunction; import org.springframework.web.servlet.function.ServerResponse; import static org.springframework.web.servlet.function.RequestPredicates.accept; import static org.springframework.web.servlet.function.RouterFunctions.route; @Configuration(proxyBeanMethods = false) public class MyRoutingConfiguration { private static final RequestPredicate ACCEPT_JSON = accept(MediaType.APPLICATION_JSON); @Bean public RouterFunction<ServerResponse> routerFunction(MyUserHandler userHandler) { return route() .GET("/{user}", ACCEPT_JSON, userHandler::getUser) .POST("/", ACCEPT_JSON, userHandler::addUser) .DELETE("/{user}", ACCEPT_JSON, userHandler::deleteUser) .build(); } }
import org.springframework.stereotype.Component; import org.springframework.web.servlet.function.ServerRequest; import org.springframework.web.servlet.function.ServerResponse; @Component public class MyUserHandler { public ServerResponse getUser(ServerRequest request) { ... return ServerResponse.ok().build(); } public ServerResponse addUser(ServerRequest request) { ... return ServerResponse.ok().build(); } public ServerResponse deleteUser(ServerRequest request) { ... return ServerResponse.ok().build(); } }
详见
RFC 7807: https://www.rfc-editor.org/rfc/rfc7807
就是会对一部分错误信息进行处理后再返回
@Configuration(proxyBeanMethods = false)
//需要我们再配置文件中配置过这个属性 spring.mvc.problemdetails.enabled=true
@ConditionalOnProperty(prefix = "spring.mvc.problemdetails", name = "enabled", havingValue = "true")
static class ProblemDetailsErrorHandlingConfiguration {
@Bean
@ConditionalOnMissingBean(ResponseEntityExceptionHandler.class)
ProblemDetailsExceptionHandler problemDetailsExceptionHandler() {
return new ProblemDetailsExceptionHandler();
}
}
@ExceptionHandler({ HttpRequestMethodNotSupportedException.class, //请求方式不支持 HttpMediaTypeNotSupportedException.class, HttpMediaTypeNotAcceptableException.class, MissingPathVariableException.class, MissingServletRequestParameterException.class, MissingServletRequestPartException.class, ServletRequestBindingException.class, MethodArgumentNotValidException.class, NoHandlerFoundException.class, AsyncRequestTimeoutException.class, ErrorResponseException.class, ConversionNotSupportedException.class, TypeMismatchException.class, HttpMessageNotReadableException.class, HttpMessageNotWritableException.class, BindException.class })
{
"timestamp": "2023-04-18T11:13:05.515+00:00",
"status": 405,
"error": "Method Not Allowed",
"trace": "org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' is not supported\r\n\tat org.springframework.web.servlejava.base/java.lang.Thread.run(Thread.java:833)\r\n",
"message": "Method 'POST' is not supported.",
"path": "/users"
}
spring.mvc.problemdetails.enabled=true
开启后 会使用新的MediaType
Content-Type: application/problem+json+ 额外扩展返回
并且返回信息也会变化
{
"type": "about:blank",
"title": "Method Not Allowed",
"status": 405,
"detail": "Method 'POST' is not supported.",
"instance": "/users"
}
主要是因为该请求异常被 HttpRequestMethodNotSupportedException拦截了
https://www.graalvm.org/
GraalVM是一个高性能的JDK,旨在加速用Java和其他JVM语言编写的应用程序的执行,同时还提供JavaScript、Python和许多其他流行语言的运行时。
GraalVM提供了两种运行Java应用程序的方式:
https://visualstudio.microsoft.com/zh-hans/free-developer-offers/
安装 native-image 依赖:
gu install native-image
gu install --file native-image-installable-svm-java17-windows-amd64-22.3.2.jar
native-image
第一步: 创建项目
使用mvn clean package进行打包
确认jar包是否可以执行java -jar xxx.jar
可能需要给 MANIFEST.MF添加 Main-Class: 你的主类
#从入口开始,编译整个jar
native-image -cp springboot3-aot-1.0-SNAPSHOT.jar com.springboot3.MainApplication -o qidongchengxu
#编译某个类【必须有main入口方法,否则无法编译】
native-image -cp .\classes org.example.App
<build>
<plugins>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
可能提示如下各种错误,无法构建原生镜像,需要配置环境变量;
● 提示其他找不到出现cl.exe找不到错误
● 出现乱码
● 提示no include path set
● 提示fatal error LNK1104: cannot open file ‘LIBCMT.lib’
● 提示 LINK : fatal error LNK1104: cannot open file ‘kernel32.lib’
需要修改三个环境变量:Path、INCLUDE、lib
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.33.31629\bin\Hostx64\x64
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.33.31629\include;C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\shared;C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\ucrt;C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\um;C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\winrt
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.33.31629\lib\x64;C:\Program Files (x86)\Windows Kits\10\Lib\10.0.19041.0\um\x64;C:\Program Files (x86)\Windows Kits\10\Lib\10.0.19041.0\ucrt\x64
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。