参考自 4.Web开发进阶.pptx
动态资源拜访
默认将动态资源放到src/main/resources/static目录下,这种状况间接通过ip:port/动态资源名称即可拜访(默认做了映射)
也能够在application.properties中通过spring.web.resources.static-locations
进行设置
例如,spring.web.resources.static-locations=/images/**
,则必须在原门路前加上images
编译后resources上面的文件,都会放到target/classes下
文件上传
传文件时,(对前端来说)表单的enctype属性,必须由默认的application/x-www-form-urlencoded,改为multipart/form-data,这样编码方式就会变动
spring.servlet.multipart.max-file-size=10MB">spring.servlet.multipart.max-file-size=10MB
src/main/java/com/example/helloworld/controller/FileUploadController.java
package com.example.helloworld.controller;import org.springframework.web.bind.annotation.*;import org.springframework.web.multipart.MultipartFile;import javax.servlet.http.HttpServletRequest;import java.io.File;import java.io.IOException;import java.util.Date;@RestControllerpublic class FileUploadController { @PostMapping("/upload") // 下面这行等价于 @RequestMapping(value = "/upload", method = RequestMethod.POST) public String up(String nickname, MultipartFile photo, HttpServletRequest request) throws IOException { System.out.println(nickname); // 获取图片的原始名称 System.out.println(photo.getOriginalFilename()); // 取文件类型 System.out.println(photo.getContentType()); // HttpServletRequest 获取web服务器的门路(可动静获取) String path = request.getServletContext().getRealPath("/upload/"); System.out.println(path); saveFile(photo, path); return "上传胜利"; } // public void saveFile(MultipartFile photo, String path) throws IOException { // 判断存储的目录是否存在,如果不存在则创立 File dir = new File(path); if (!dir.exists()) { // 创立目录 dir.mkdir(); } // 调试时能够把这个门路写死 File file = new File(path + photo.getOriginalFilename()); photo.transferTo(file); }}
拦截器
就是一个中间件..
和gin的middleware齐全一样
先定义一个一般的java类,个别以Interceptor结尾,代表是一个拦截器。须要继承零碎的拦截器类,能够重写父类里的办法
父类根本没做啥事,空的办法实现
上面重写其中的办法
src/main/java/com/example/helloworld/interceptor/LoginInterceptor.java
package com.example.helloworld.interceptor;import org.springframework.web.servlet.HandlerInterceptor;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class LoginInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("LoginInterceptor拦挡!!!"); return true; }}
src/main/java/com/example/helloworld/config/WebConfig.java
配置只拦挡/user
package com.example.helloworld.config;import com.example.helloworld.interceptor.LoginInterceptor;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.InterceptorRegistry;import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;@Configurationpublic class WebConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/user/**"); }}
5.构建RESTful服务.pptx
Spring Boot实现RESTful API
PUT和PATCH咋准确辨别? ...前几年用过PUT,而PATCH我印象里素来没用过。。
没啥太大意思,还有主张一律用POST的
src/main/java/com/example/helloworld/controller/UserController.java:
package com.example.helloworld.controller;import com.example.helloworld.entity.User;import io.swagger.annotations.Api;import io.swagger.annotations.ApiOperation;import org.springframework.web.bind.annotation.*;@RestControllerpublic class UserController { @ApiOperation("获取用户") @GetMapping("/user/{id}") public String getUserById(@PathVariable int id) { // 想拿到门路上的动静参数,须要加@PathVariable 注解 System.out.println(id); return "依据ID获取用户信息"; } @PostMapping("/user") public String save(User user) { return "增加用户"; } @PutMapping("/user") public String update(User user) { return "更新用户"; } @DeleteMapping("/user/{id}") public String deleteById(@PathVariable int id) { System.out.println(id); return "依据ID删除用户"; }}
应用Swagger生成Web API文档
pom.xml:
<!-- 增加swagger2相干性能 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <!-- 增加swagger-ui相干性能 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency>
除了增加依赖,还须要加一个配置类
src/main/java/com/example/helloworld/config/SwaggerConfig.java:
package com.example.helloworld.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import springfox.documentation.builders.ApiInfoBuilder;import springfox.documentation.builders.PathSelectors;import springfox.documentation.builders.RequestHandlerSelectors;import springfox.documentation.service.ApiInfo;import springfox.documentation.spi.DocumentationType;import springfox.documentation.spring.web.plugins.Docket;import springfox.documentation.swagger2.annotations.EnableSwagger2;@Configuration // 通知Spring容器,这个类是一个配置类(SB的配置类,都须要加上@Configuration这个注解)@EnableSwagger2 // 启用Swagger2性能public class SwaggerConfig { /** * 配置Swagger2相干的bean */ @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) // 调用了一个自定义办法(改改题目啥的~) .select() .apis(RequestHandlerSelectors.basePackage("com"))// com包下所有API都交给Swagger2治理 .paths(PathSelectors.any()).build(); } /** * 此处次要是API文档页面显示信息 */ private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("爽哥提醒:演示我的项目API") // 题目 .description("爽哥提醒:演示我的项目") // 形容 .version("1.0") // 版本 .build(); }}
能够拜访http://localhost:8080/swagger-ui.html
读的我的项目里的控制器,开展能够看到每个控制器里都有什么办法
能够通过如下注解,在页面增加正文
例如@ApiOperation("获取用户")
还能够在页面间接进行调试
本文由mdnice多平台公布