欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring Boot 功能整合的實現(xiàn)

 更新時間:2021年05月27日 10:39:47   作者:Jaxson Wang  
Spring Boot生態(tài)豐富,集成也不算困難。本文簡單的介紹下功能整合的步驟,最后提供一個具體的實現(xiàn)例子,學習Spring Boot的同學可以參考下

前言

如果根據(jù)之前做的 Nest.js 后端項目功能為標準的話,那么 Spring Boot 項目需要幾種功能進行整合,好在生態(tài)豐富,集成也不算困難。所以打算根據(jù)之前的項目使用 Spring Boot 重寫個新的項目:

  • Restful API CRUD 功能實現(xiàn)
  • 數(shù)據(jù)庫對象關系映射功能持久化支持
  • OpenAPI 文檔支持
  • 參數(shù)校驗判斷業(yè)務
  • redis 緩存
  • ...

數(shù)據(jù)庫持久化支持

目前數(shù)據(jù)庫持久化主要是 Spring Boot Jpa 和 Spring Boot Mybatis 。如果看過 JPA 的業(yè)務過程會發(fā)現(xiàn)和 Nodejs 中的 TypeORM 及其相似。Mybatis 主要可以靈活調試動態(tài) Sql 。不管怎么說根據(jù)自己項目業(yè)務需求選定其中功能吧。

安裝 MyBatis 教程可以官方文檔查閱:mybatis-spring-boot-autoconfigure

Swagger 文檔支持

集成 Swagger UI 文檔支持也非常簡單,生態(tài)中的 springfox 做的不錯,添加依賴:

<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-boot-starter</artifactId>
  <version>3.0.0</version>
</dependency>

這里需要指定版本,不指定拉取依賴會報錯。

然后在啟動方法添加注解:

@EnableOpenApi
public class YasuoApplication {
	public static void main(String[] args) {
    // ...
	}
}

然后在 Controller 類上添加標識:

@Api(value = "global", tags = "全局接口")
@RestController
@RequestMapping("/")
public class AppController {
}

在然后在方法里添加詳細信息:

@Api(value = "global", tags = "全局接口")
@RestController
@RequestMapping("/")
public class AppController {
    UserService userService;

    @ApiOperation(value = "用戶登錄", notes = "系統(tǒng)用戶登錄")
    @PostMapping("login")
    public JSONObject login(@RequestParam("username") String username, @RequestParam("password") String password) {
        System.out.println(username);
        System.out.println(password);
        JSONObject info = new JSONObject();
        return info;
    }
}

啟動項目訪問:http://localhost:8080/swagger-ui 即可訪問。值得注意是如果你在 application 添加 server.servlet.contextPath 選項的時候記得添加對應的字段。

參數(shù)校驗 JSR303

從 springboot-2.3 開始,校驗包被獨立成了一個 starter 組件:

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-validation</artifactId>
</dependency>

比如在 DTO 類里:

package com.iiong.yasuo.dto;

import lombok.Data;

import javax.validation.constraints.NotEmpty;

/**
 * Author: Jaxson
 * Description: 請求用戶登錄參數(shù)
 * Date: 2021-05-26
 */
@Data
public class UserLoginRequestDTO {
    @NotEmpty(message = "登錄名稱不得為空!")
    private String username;

    @NotEmpty(message = "登錄密碼不得為空!")
    private String password;
}

內(nèi)置的校驗注解可以查看官方文檔,然后進行參數(shù)校驗:

@ApiOperation(value = "用戶登錄", notes = "系統(tǒng)用戶登錄")
@PostMapping("login")
public RestfulModel<UserLoginResponseDTO> login(@RequestBody @Validated UserLoginRequestDTO userLoginRequestDTO) {
    System.out.println(userLoginRequestDTO);
    UserLoginResponseDTO userLoginResponseDTO = new UserLoginResponseDTO();
    userLoginResponseDTO.setId(1013401346173L);
    userLoginResponseDTO.setLoginName("112233");
    userLoginResponseDTO.setName("系統(tǒng)管理員");
    userLoginResponseDTO.setToken("test");
    return new RestfulModel<>(0, "登錄成功!", userLoginResponseDTO);
}

不過默認返回的異常信息并不是很友好,需要再次簡化,所以需要做個全局異常處理。如果需要可以使用 @RestControllerAdvice 注解來表示全局處理類:

/**
 * Author: Jaxson
 * Description: 全局異常處理類
 * Date: 2021-05-26
 */
@ControllerAdvice
public class ExceptionHandlerConfig {

    /**
     * 統(tǒng)一處理參數(shù)校驗異常
     * @param bindException 捕捉到的異常
     * @return 返回數(shù)據(jù)
     */
    @ExceptionHandler(value = BindException.class)
    @ResponseBody
    public RestfulModel<Object> validExceptionHandler(BindException bindException) {
        String exceptionMsg = bindException.getBindingResult().getAllErrors().get(0).getDefaultMessage();
        return new RestfulModel<>(1000, exceptionMsg, null);
    }
}

當然這里面還可以處理一些系統(tǒng)級別的異常,自己拋出即可。

跨域解決

解決跨域問題也很簡單,只需要實現(xiàn)接口 WebMvcConfigurer 重寫方法即可:

/**
 * Author: Jaxson
 * Description: 運行跨域
 * Date: 2021-05-26
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry corsRegistry) {
        corsRegistry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedHeaders(CorsConfiguration.ALL)
                .allowedMethods(CorsConfiguration.ALL)
                .allowCredentials(true)
                .maxAge(3600); // 1小時內(nèi)不需要再預檢(發(fā)OPTIONS請求)
    }
}

整合MongoDB實現(xiàn)文件上傳下載刪除

引入pom依賴

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>

配置yml

spring:
  data:
    mongodb:
      host: *.*.*.*
      username: ***
      password: ***
      database: ***
      port: 27017
 # 設置文件上傳的大小限制
  servlet:
    multipart:
      max-file-size: 10MB
      max-request-size: 50MB

上傳下載刪除

/**
 * @author Mr.Horse
 * @version 1.0
 * @description: MongoDB的文件上傳、下載、刪除等基本操作(集合HuTool工具庫)
 * @date 2021/4/29 9:53
 */
@Validated
@Controller
@RequestMapping("/mongo")
public class MongoUploadController {

    private static Logger logger = LoggerFactory.getLogger(MongoUploadController.class);

    @Autowired
    private GridFsTemplate gridFsTemplate;

    @Autowired
    private MongoTemplate mongoTemplate;

    private static final List<String> CONTENT_TYPES = Arrays.asList("image/gif", "image/jpeg", "image/jpg", "image/png");

    /**
     * MongoDB文件上傳(圖片上傳)
     *
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public ResponseEntity<String> fileUpload(@RequestParam("file") MultipartFile file) {
        try {
            // 校驗文件信息(文件類型,文件內(nèi)容)
            String originalFilename = file.getOriginalFilename();
            if (StrUtil.isBlank(originalFilename)) {
                return ResponseEntity.badRequest().body("參數(shù)錯誤");
            }
            String contentType = file.getContentType();
            if (!CONTENT_TYPES.contains(contentType)) {
                return ResponseEntity.badRequest().body("文件類型錯誤");
            }
            InputStream inputStream = file.getInputStream();
            BufferedImage bufferedImage = ImageIO.read(inputStream);
            if (ObjectUtil.isEmpty(bufferedImage)) {
                return ResponseEntity.badRequest().body("文件內(nèi)容錯誤");
            }

            // 文件重命名
            String suffix = FileNameUtil.getSuffix(originalFilename);
            String fileName = IdUtil.simpleUUID().concat(".").concat(suffix);

            // 文件上傳,返回ObjectId
            ObjectId objectId = gridFsTemplate.store(inputStream, fileName, contentType);
            return StrUtil.isBlank(String.valueOf(objectId)) ? ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("文件上傳失敗") : ResponseEntity.ok(String.valueOf(objectId));
        } catch (IOException e) {
            return ResponseEntity.badRequest().body("文件上傳異常");
        }
    }


    /**
     * 根據(jù)ObjectId讀取文件并寫入響應流,頁面進行進行相關操作,可以進行文件的下載和展示
     *
     * @param objectId
     */
    @GetMapping("/read")
    public void queryFileByObjectId(@RequestParam("objectId") @NotBlank(message = "ObjectId不能為空") String objectId, HttpServletResponse response) {
        // 根據(jù)objectId查詢文件
        GridFSFile file = gridFsTemplate.findOne(new Query(Criteria.where("_id").is(objectId)));
        // 創(chuàng)建一個文件桶
        GridFSBucket gridFsBucket = GridFSBuckets.create(mongoTemplate.getDb());
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            if (ObjectUtil.isNotNull(file)) {
                // 打開下載流對象
                GridFSDownloadStream fileStream = gridFsBucket.openDownloadStream(file.getObjectId());
                // 創(chuàng)建girdFsResource,傳入下載流對象,獲取流對象
                GridFsResource gridFsResource = new GridFsResource(file, fileStream);
                // 寫入輸出流
                inputStream = gridFsResource.getInputStream();
                outputStream = response.getOutputStream();
                byte[] bytes = new byte[1024];
                if (inputStream.read(bytes) != -1) {
                    outputStream.write(bytes);
                }
            }
        } catch (IOException e) {
            logger.error("文件讀取異常: {}", e.getMessage());
        } finally {
            IoUtil.close(outputStream);
            IoUtil.close(inputStream);
        }
    }

    /**
     * 根據(jù)ObjectId刪除文件
     *
     * @param objectId
     * @return
     */
    @DeleteMapping("/remove")
    public ResponseEntity<String> removeFileByObjectId(@RequestParam("objectId") @NotBlank(message = "ObjectId不能為空") String objectId) {
        gridFsTemplate.delete(new Query(Criteria.where("_id").is(objectId)));
        return ResponseEntity.ok("刪除成功");
    }

}

如果需要實現(xiàn)在瀏覽器頁面下載此資源的功能,可結合js進行操作(文件類型根據(jù)具體業(yè)務需求而定)。主要實現(xiàn)代碼如下所示:

    downloadNotes(noteId) {
      axios({
        url: this.BASE_API + '/admin/mongo/file/query/' + noteId,
        method: 'get',
        responseType: 'arraybuffer',
        params: { type: 'download' }
      }).then(res => {
        // type類型可以設置為文本類型,這里是pdf類型
        const pdfUrl = window.URL.createObjectURL(new Blob([res.data], { type: `application/pdf` }))
        const fname = noteId // 下載文件的名字
        const link = document.createElement('a')
        link.href = pdfUrl
        link.setAttribute('download', fname)
        document.body.appendChild(link)
        link.click()
        URL.revokeObjectURL(pdfUrl) // 釋放URL 對象
      })
    }

以上就是Spring Boot 功能整合的實現(xiàn)的詳細內(nèi)容,更多關于Spring Boot 功能整合的資料請關注腳本之家其它相關文章!

相關文章

  • Map與JavaBean相互轉換的工具類?

    Map與JavaBean相互轉換的工具類?

    這篇文章主要介紹了Map與JavaBean相互轉換的工具類,在做導入的時候,遇到了需要將map對象轉化?成javabean的問題,也就是說,不清楚javabean的內(nèi)部字段排列,只知道m(xù)ap的?key代表javabean的字段名,value代表值,需要的朋友可以參考下
    2022-02-02
  • idea一鍵部署SpringBoot項目jar包到服務器的實現(xiàn)

    idea一鍵部署SpringBoot項目jar包到服務器的實現(xiàn)

    我們在開發(fā)環(huán)境部署項目一般通過idea將項目打包成jar包,然后連接linux服務器,將jar手動上傳到服務中,本文就來詳細的介紹一下步驟,感興趣的可以了解一下
    2023-12-12
  • SpringBoot打包成Docker鏡像的幾種實現(xiàn)方式

    SpringBoot打包成Docker鏡像的幾種實現(xiàn)方式

    Spring Boot是一個用于構建獨立的、可執(zhí)行的Spring應用程序的框架,結合使用Spring Boot和Docker,可以方便地將應用程序部署到不同的環(huán)境中本文,主要介紹了SpringBoot打包成Docker鏡像的幾種實現(xiàn)方式,感興趣的可以了解一下
    2024-01-01
  • Mybatis Plus ActiveRecord模式的具體使用

    Mybatis Plus ActiveRecord模式的具體使用

    ActiveRecord 是一種設計模式,它是一種在軟件開發(fā)中用于管理關系數(shù)據(jù)庫的模式,本文主要介紹了Mybatis Plus ActiveRecord模式的具體使用,具有一定的參考價值,感興趣的可以了解一下
    2024-07-07
  • java抓取12306信息實現(xiàn)火車余票查詢示例

    java抓取12306信息實現(xiàn)火車余票查詢示例

    這篇文章主要介紹了java抓取12306信息實現(xiàn)火車余票查詢示例,需要的朋友可以參考下
    2014-04-04
  • java中創(chuàng)建、寫入文件的5種方式

    java中創(chuàng)建、寫入文件的5種方式

    這篇文章主要介紹了java中創(chuàng)建、寫入文件的5種方式,幫助大家更好的理解學習Java io的相關知識,感興趣的朋友可以了解下
    2020-08-08
  • 淺談Java8的特性之Optional類

    淺談Java8的特性之Optional類

    這篇文章主要介紹了淺談Java8的特性之Optional類,Optional類是 Java 8 引入的一個很有趣的特性,Optional 類是一個可以為null的容器對象,它主要解決的問題是臭名昭著的空指針異常,需要的朋友可以參考下
    2023-08-08
  • SpringBoot同一接口多個實現(xiàn)類配置的實例詳解

    SpringBoot同一接口多個實現(xiàn)類配置的實例詳解

    這篇文章主要介紹了SpringBoot同一接口多個實現(xiàn)類配置的實例詳解,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-11-11
  • 高斯混合模型與EM算法圖文詳解

    高斯混合模型與EM算法圖文詳解

    高斯模型就是用高斯概率密度函數(shù)(正態(tài)分布曲線)精確地量化事物,將一個事物分解為若干的基于高斯概率密度函數(shù)(正態(tài)分布曲線)形成的模型
    2021-08-08
  • spring-boot 如何實現(xiàn)單次執(zhí)行程序

    spring-boot 如何實現(xiàn)單次執(zhí)行程序

    這篇文章主要介紹了spring-boot 實現(xiàn)單次執(zhí)行程序方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09

最新評論