今天集成了swagger自动生成模板和注册功能的店铺logo上传,都是以前做过的功能;
swagger自动生成步骤;
1.新建模板类,把swagger配置类的代码放进去;
2.自动生成的路径选好即可;
1.新建模板类,把swagger配置类的代码放进去;
package cn.itsource.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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* @ClassName: Swagger2
* @description: 系统API接口文档配置类
* @author ${author}
* @since ${date}
* @Version 1.1.0.1
*/
@Configuration
@EnableSwagger2
public class Swagger2 {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//对外暴露服务的包,以controller的方式暴露,所以就是controller的包.
.apis(RequestHandlerSelectors.basePackage("cn.itsource.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("系统服务API")
.description("平台服务接口文档说明")
.contact(new Contact("colin", "", "wujiangbo@itsource.cn"))
.version("1.0")
.build();
}
}
2.自动生成的路径选好即可;
//swagger的输出配置
focList.add(new FileOutConfig("/templates/swagger2.java.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
//合并好的内容输出到哪儿?
return rb.getString("OutputDir")+ "/cn/itsource/config/swagger2.java";
}
});
阿里云oss上传步骤;
1.后端代码参考官网,把模板扒下来用;
2.前端记得给name;后端接参数的时候用name的值接即可;
3.后端业务完成后拼接参数,把路径返回给前端展示;
1.后端代码参考官网,把模板扒下来用;
@Override
public JSONResult getFileOss(MultipartFile file) {
//文件上传接口(上传到OSS中的bucket)
// Endpoint:地域节点
String endpoint = "oss-cn-chengdu.aliyuncs.com";
// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
String accessKeyId = "这里填id";
String accessKeySecret = "这里填密钥";
// 填写Bucket名称,例如examplebucket。
String bucketName = "0217blog";
//获取上传文件的后缀
String originalFilename = file.getOriginalFilename();//.jpg.jng
int i = originalFilename.lastIndexOf(".");
String fileType = originalFilename.substring(i);
// 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。
//
String fileName = UUID.randomUUID().toString().replace("-", "") + fileType;
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try {
InputStream inputStream = file.getInputStream();
// 创建PutObject请求。
ossClient.putObject(bucketName, fileName, inputStream);
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (Exception ce) {
System.out.println("上传文件OSS异常:" + ce.getMessage());
return JSONResult.error(ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
//拼接URL地址返回给前端
//https://blog0217.oss-cn-chengdu.aliyuncs.com/77786f3d6f31445bad462e5464085de8.jpg
String url = "https://" + bucketName + "." + endpoint + "/" + fileName;
return JSONResult.success(url);
2.前端记得给name;后端接参数的时候用name的值接即可;
@RequestMapping("/file") public JSONResult fileOss(@RequestParam("fileName") MultipartFile file){ return iFileService.getFileOss(file); }
<el-upload class="upload-demo" action="http://localhost:1020/hrm/file/file/file" name="fileName" :on-preview="handlePreview" :on-remove="handleRemove" :on-success="handleSuccess" :file-list="fileList" list-type="picture"> <el-button size="small" type="primary">点击上传</el-button> <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div> </el-upload>这里的name要与后端接口的接收参数保持一致;
3.后端业务完成后拼接参数,把路径返回给前端展示;
//拼接URL地址返回给前端 //https://blog0217.oss-cn-chengdu.aliyuncs.com/77786f3d6f31445bad462e5464085de8.jpg
String url = "https://" + bucketName + "." + endpoint + "/" + fileName;
return JSONResult.success(url);