补充接口统一返回和统一异常返回

master
chenjf 2 years ago
parent 182b10d055
commit 851d04e639

@ -1,20 +1,19 @@
package org.ils.file.controller; package org.ils.file.controller;
import cn.xuyanwu.spring.file.storage.FileInfo;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.ils.file.service.impl.IlsFileDetailServiceImpl; import org.ils.file.service.impl.IlsFileDetailServiceImpl;
import org.ils.file.utils.Result;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
/** /**
@ -37,17 +36,28 @@ public class FileDetailController {
*/ */
@ApiOperation("上传文件-返回fileInfo") @ApiOperation("上传文件-返回fileInfo")
@RequestMapping(value = "upload", method = RequestMethod.POST) @RequestMapping(value = "upload", method = RequestMethod.POST)
public FileInfo upload(MultipartFile file) { public Result upload(MultipartFile file) throws InterruptedException {
return ilsFileDetailService.upload(file); return Result.ok(ilsFileDetailService.upload(file));
} }
/**
*
*/
@ApiOperation("上传文件-返回fileInfo")
@RequestMapping(value = "uploadInputStream", method = RequestMethod.POST)
public Result uploadInputStream(InputStream inputStream) {
return Result.ok(ilsFileDetailService.uploadInputStream(inputStream));
}
/** /**
* url * url
*/ */
@ApiOperation("上传文件-返回url") @ApiOperation("上传文件-返回url")
@RequestMapping(value = "uploadUrl", method = RequestMethod.POST) @RequestMapping(value = "uploadUrl", method = RequestMethod.POST)
public String uploadUrl(MultipartFile file) { public Result uploadUrl(MultipartFile file) {
return ilsFileDetailService.uploadUrl(file); return Result.ok(ilsFileDetailService.uploadUrl(file));
} }
/** /**
@ -57,8 +67,8 @@ public class FileDetailController {
*/ */
@ApiOperation("上传图片-缩略图") @ApiOperation("上传图片-缩略图")
@RequestMapping(value = "uploadImage", method = RequestMethod.POST) @RequestMapping(value = "uploadImage", method = RequestMethod.POST)
public FileInfo uploadImage(MultipartFile file) { public Result uploadImage(MultipartFile file) {
return ilsFileDetailService.uploadImage(file); return Result.ok(ilsFileDetailService.uploadImage(file));
} }
/** /**
@ -66,8 +76,8 @@ public class FileDetailController {
*/ */
@ApiOperation("上传文件到指定阿里云存储平台,成功返回文件信息") @ApiOperation("上传文件到指定阿里云存储平台,成功返回文件信息")
@RequestMapping(value = "uploadPlatform", method = RequestMethod.POST) @RequestMapping(value = "uploadPlatform", method = RequestMethod.POST)
public FileInfo uploadPlatform(MultipartFile file, String platform) { public Result uploadPlatform(MultipartFile file, String platform) {
return ilsFileDetailService.uploadPlatform(file, platform); return Result.ok(ilsFileDetailService.uploadPlatform(file, platform));
} }
/** /**
@ -84,8 +94,8 @@ public class FileDetailController {
*/ */
@ApiOperation("根据url和文件后缀上传文件") @ApiOperation("根据url和文件后缀上传文件")
@RequestMapping(value = "uploadByUrl", method = RequestMethod.POST) @RequestMapping(value = "uploadByUrl", method = RequestMethod.POST)
public FileInfo uploadByUrl(String url, String contentType, HttpServletResponse response) throws IOException { public Result uploadByUrl(String url, String contentType, HttpServletResponse response) throws IOException {
return ilsFileDetailService.uploadByUrl(url, contentType, response); return Result.ok(ilsFileDetailService.uploadByUrl(url, contentType, response));
} }
/** /**
@ -93,8 +103,8 @@ public class FileDetailController {
*/ */
@ApiOperation("根据base64内容和文件后缀上传文件") @ApiOperation("根据base64内容和文件后缀上传文件")
@RequestMapping(value = "uploadByBase64", method = RequestMethod.POST) @RequestMapping(value = "uploadByBase64", method = RequestMethod.POST)
public FileInfo uploadByBase64(String base64, String contentType, HttpServletResponse response) throws IOException { public Result uploadByBase64(String base64, String contentType, HttpServletResponse response) throws IOException {
return ilsFileDetailService.uploadByBase64(base64, contentType, response); return Result.ok(ilsFileDetailService.uploadByBase64(base64, contentType, response));
} }
} }

@ -0,0 +1,21 @@
package org.ils.file.exception;
import org.ils.file.utils.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
/**
*
* @author chenjf
*/
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(value = Exception.class)
@ResponseBody
public Result handleCustomException(HttpServletRequest request, Exception e) {
return Result.error(e.getMessage());
}
}

@ -6,6 +6,7 @@ import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
/** /**
* @author chenjf * @author chenjf
@ -17,6 +18,11 @@ public interface IlsFileDetailService {
*/ */
public FileInfo upload(MultipartFile file); public FileInfo upload(MultipartFile file);
/**
*
*/
public FileInfo uploadInputStream(InputStream inputStream);
/** /**
* url * url
*/ */

@ -1,7 +1,5 @@
package org.ils.file.service.impl; package org.ils.file.service.impl;
import cn.hutool.core.io.FileUtil;
import cn.xuyanwu.spring.file.storage.Downloader;
import cn.xuyanwu.spring.file.storage.FileInfo; import cn.xuyanwu.spring.file.storage.FileInfo;
import cn.xuyanwu.spring.file.storage.FileStorageService; import cn.xuyanwu.spring.file.storage.FileStorageService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -37,6 +35,11 @@ public class IlsFileDetailServiceImpl implements IlsFileDetailService {
return fileInfo; return fileInfo;
} }
@Override
public FileInfo uploadInputStream(InputStream inputStream) {
FileInfo fileInfo = fileStorageService.of(inputStream).upload();
return fileInfo;
}
@Override @Override
@ -56,7 +59,7 @@ public class IlsFileDetailServiceImpl implements IlsFileDetailService {
public FileInfo uploadImage(MultipartFile file) { public FileInfo uploadImage(MultipartFile file) {
FileInfo fileInfo = fileStorageService.of(file) FileInfo fileInfo = fileStorageService.of(file)
.image(img -> img.size(1000,1000)) //将图片大小调整到 1000*1000 .image(img -> img.size(1000,1000)) //将图片大小调整到 1000*1000
.thumbnail(th -> th.size(200,200)) //再生成一张 200*200 的缩略图 .thumbnail(th -> th.size(600,600)) //再生成一张 600*600 的缩略图
.upload(); .upload();
return fileInfo; return fileInfo;
} }

@ -0,0 +1,32 @@
package org.ils.file.utils;
/**
*
*/
public interface CommonConstant {
/**
*
*/
Integer HTTP_CODE_500 = 500;
/**
*
*/
Integer HTTP_CODE_200 = 200;
/**
*
*/
Integer HTTP_CODE_510 = 510;
/**
* 400
*/
Integer HTTP_CODE_400 = 400;
/**
*
*/
Integer HTTP_CODE_201 = 201;
}

@ -0,0 +1,151 @@
package org.ils.file.utils;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
/**
*
* @author chenjf
* @date 2023/07/14
* @param <T>
*/
@Data
public class Result <T> implements Serializable {
@ApiModelProperty(value = "成功标志")
private boolean success = true;
@ApiModelProperty(value = "返回处理消息")
private String message = "";
@ApiModelProperty(value = "返回代码")
private Integer code = CommonConstant.HTTP_CODE_200;
@ApiModelProperty(value = "返回数据对象")
private T result;
@ApiModelProperty(value = "时间戳")
private long timestamp = System.currentTimeMillis();
public Result() {
}
/**
*
* @param code code
* @param message
*/
public Result(Integer code, String message) {
this.code = code;
this.message = message;
}
/**
*
* @param message
*/
public Result<T> success(String message) {
this.message = message;
this.code = CommonConstant.HTTP_CODE_200;
this.success = true;
return this;
}
public static<T> Result<T> ok() {
Result<T> r = new Result<T>();
r.setSuccess(true);
r.setCode(CommonConstant.HTTP_CODE_200);
return r;
}
public static<T> Result<T> ok(String msg) {
Result<T> r = new Result<T>();
r.setSuccess(true);
r.setCode(CommonConstant.HTTP_CODE_200);
//Result OK(String msg)方法会造成兼容性问题 issues/I4IP3D
r.setResult((T) msg);
r.setMessage(msg);
return r;
}
public static<T> Result<T> ok(T data) {
Result<T> r = new Result<T>();
r.setSuccess(true);
r.setCode(CommonConstant.HTTP_CODE_200);
r.setResult(data);
return r;
}
public static<T> Result<T> OK() {
Result<T> r = new Result<T>();
r.setSuccess(true);
r.setCode(CommonConstant.HTTP_CODE_200);
return r;
}
public static<T> Result<T> OK(String msg) {
Result<T> r = new Result<T>();
r.setSuccess(true);
r.setCode(CommonConstant.HTTP_CODE_200);
r.setMessage(msg);
r.setResult((T) msg);
return r;
}
public static<T> Result<T> OK(T data) {
Result<T> r = new Result<T>();
r.setSuccess(true);
r.setCode(CommonConstant.HTTP_CODE_200);
r.setResult(data);
return r;
}
public static<T> Result<T> OK(String msg, T data) {
Result<T> r = new Result<T>();
r.setSuccess(true);
r.setCode(CommonConstant.HTTP_CODE_200);
r.setMessage(msg);
r.setResult(data);
return r;
}
public static<T> Result<T> error(String msg, T data) {
Result<T> r = new Result<T>();
r.setSuccess(false);
r.setCode(CommonConstant.HTTP_CODE_201);
r.setMessage(msg);
r.setResult(data);
return r;
}
public static<T> Result<T> error(String msg) {
return error(CommonConstant.HTTP_CODE_201, msg);
}
public static<T> Result<T> error(int code, String msg) {
Result<T> r = new Result<T>();
r.setCode(code);
r.setMessage(msg);
r.setSuccess(false);
return r;
}
public Result<T> error500(String message) {
this.message = message;
this.code = CommonConstant.HTTP_CODE_500;
this.success = false;
return this;
}
}

@ -1,5 +1,8 @@
server: server:
port: 9098 port: 9098
feign:
hystrix:
enabled: true
spring: spring:
mvc: mvc:
pathmatch: pathmatch:
@ -9,7 +12,7 @@ spring:
max-file-size: 100MB max-file-size: 100MB
max-request-size: 100MB max-request-size: 100MB
datasource: datasource:
url: jdbc:mysql://127.0.0.1:3306/ils_fileStorage?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&AllowPublicKeyRetrieval=True&useSSL=false url: jdbc:mysql://127.0.0.1:3306/ils_fileStorage?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root username: root
password: 123456 password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver driver-class-name: com.mysql.cj.jdbc.Driver

Loading…
Cancel
Save