美文网首页
12 Spring Boot学习

12 Spring Boot学习

作者: 孙嘉炜 | 来源:发表于2019-04-18 09:19 被阅读0次

    一、swagger测试接口

    • 了解

    随着互联网技术的发展,现在的网站架构基本都由原来的后端渲染,变成了:前端渲染、先后端分离的形态,而且前端技术和后端技术在各自的道路上越走越远。
    前端和后端的唯一联系,变成了API接口;API文档变成了前后端开发人员联系的纽带,变得越来越重要,swagger就是一款让你更好的书写API文档的框架。

    • 在pom里添加依赖
       <dependency>
          <groupId>com.spring4all</groupId>
          <artifactId>swagger-spring-boot-starter</artifactId>
          <version>1.8.0.RELEASE</version>
       </dependency>
    
    
    • 在Appolication里添加@EnableSwagger2Doc注解
    import com.spring4all.swagger.EnableSwagger2Doc;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    @EnableSwagger2Doc
    @SpringBootApplication
    public class SpringBootWebsocketApplication {
        public static void main(String[] args) {
            SpringApplication.run(SpringBootWebsocketApplication.class, args);
        }
    }
    
    
    • 参数配置
    #swagger配置
    swagger.enabled=true
    swagger.title=jianyue  api project
    swagger.description=Starter for swagger 2.x
    swagger.license=Apache License, Version 2.0
    swagger.licenseUrl=https://www.apache.org/licenses/LICENSE-2.0.html
    swagger.termsOfServiceUrl=https://github.com/1702343123/spring-study
    swagger.contact.name=Wj
    swagger.contact.url=https://www.jianshu.com/u/ec5ebfec0565
    swagger.contact.email=623878638@qq.com
    swagger.base-package=com.soft1721.jianyue.api.controller
    swagger.base-path=/**
    swagger.exclude-path=/error, /ops/**
    
    

    二、文件上传

    • 实现将本地图片上传到服务器指定路径的基础功能
      新建一个module,命名为file-upload,勾选web、Thymeleaf依赖
      pom.xml:
           <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-thymeleaf</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
    
    • application.properties配置上传参数
    spring.servlet.multipart.max-file-size=100MB
    spring.servlet.multipart.max-request-size=100MB
    
    
    • 编写UploadController,映射上传请求
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.multipart.MultipartFile;
    import org.springframework.web.servlet.mvc.support.RedirectAttributes;
    import java.io.IOException;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    @Controller
    public class UploadController {
        private static String UPLOADED_FOLDER = "E:/temp/";
        @GetMapping("/")
        public String index() {
            return "upload";
        }
        @PostMapping("/upload")
        public String singleFileUpload(@RequestParam("file") MultipartFile file,
                                       RedirectAttributes redirectAttributes) {
            if (file.isEmpty()) {
                redirectAttributes.addFlashAttribute("message", "请选择一个文件");
                return "redirect:upload_status";
            }
            try {
                byte[] bytes = file.getBytes();
                Path path = Paths.get(UPLOADED_FOLDER + file.getOriginalFilename());
                Files.write(path, bytes);
                redirectAttributes.addFlashAttribute("message",
                        "文件成功上传!" + file.getOriginalFilename());
            } catch (IOException e) {
                e.printStackTrace();
            }
            return "redirect:/upload_status";
        }
        @GetMapping("/upload_status")
        public String uploadStatus() {
            return "upload_status";
        }
    }
    
    
    • upload.html
    <html lang="en">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1,user-scalable=no">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <body>
    <h1>Spring Boot 文件上传示例</h1>
    <form method="POST" action="/upload" enctype="multipart/form-data">
        <input type="file" name="file"/>
        <br>
        <br>
        <input type="submit" value="提交"/>
    </form>
    </body>
    </html>
    
    
    • upload_status.html
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <body>
    <h1>Spring Boot - 文件上传状态</h1>
    <div th:if="${message}">
        <h2 th:text="${message}"/>
    </div>
    </body>
    </html>
    
    

    注:如果要把上传的路径跟着项目,如在static目录下的upload文件夹,则需要更改UploadController代码。

    @PostMapping("/upload")
        public String singleFileUpload(@RequestParam("file") MultipartFile srcFile,
                                       RedirectAttributes redirectAttributes) {
            if (srcFile.isEmpty()) {
                redirectAttributes.addFlashAttribute("message", "请选择一个文件");
                return "redirect:upload_status";
            }
            try {
                File destFile = new File(ResourceUtils.getURL("classpath:").getPath());
                if (!destFile.exists()) {
                    destFile = new File("");
                }
                System.out.println("file path:" + destFile.getAbsolutePath());
                File upload = new File(destFile.getAbsolutePath(), "static/");
                if (!upload.exists()) {
                    upload.mkdirs();
                }
                System.out.println("upload url:" + upload.getAbsolutePath());
                Path path = Paths.get(upload.getAbsolutePath() + "/" + srcFile.getOriginalFilename());
                byte[] bytes = srcFile.getBytes();
                Files.write(path, bytes);
                redirectAttributes.addFlashAttribute("message",
                        "文件成功上传!" + srcFile.getOriginalFilename());
            } catch (IOException e) {
                e.printStackTrace();
            }
            return "redirect:/upload_status";
        }
        ```
    
    

    相关文章

      网友评论

          本文标题:12 Spring Boot学习

          本文链接:https://www.haomeiwen.com/subject/rdzawqtx.html