SpringBoot學(xué)習(xí)

一、swagger測試接口

  • 了解

隨著互聯(lián)網(wǎng)技術(shù)的發(fā)展,現(xiàn)在的網(wǎng)站架構(gòu)基本都由原來的后端渲染,變成了:前端渲染、先后端分離的形態(tài),而且前端技術(shù)和后端技術(shù)在各自的道路上越走越遠(yuǎn)。
前端和后端的唯一聯(lián)系,變成了API接口;API文檔變成了前后端開發(fā)人員聯(lián)系的紐帶,變得越來越重要,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);
    }
}
  • 參數(shù)配置
#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=http://www.itdecent.cn/u/ec5ebfec0565
swagger.contact.email=623878638@qq.com
swagger.base-package=com.soft1721.jianyue.api.controller
swagger.base-path=/**
swagger.exclude-path=/error, /ops/**

二、文件上傳

  • 實(shí)現(xiàn)將本地圖片上傳到服務(wù)器指定路徑的基礎(chǔ)功能
    新建一個(gè)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配置上傳參數(shù)
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", "請選擇一個(gè)文件");
            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 - 文件上傳狀態(tài)</h1>
<div th:if="${message}">
    <h2 th:text="${message}"/>
</div>
</body>
</html>

注:如果要把上傳的路徑跟著項(xiàng)目,如在static目錄下的upload文件夾,則需要更改UploadController代碼。

@PostMapping("/upload")
    public String singleFileUpload(@RequestParam("file") MultipartFile srcFile,
                                   RedirectAttributes redirectAttributes) {
        if (srcFile.isEmpty()) {
            redirectAttributes.addFlashAttribute("message", "請選擇一個(gè)文件");
            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";
    }
    ```

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容