refactor: 添加未认证和未授权自定义异常处理,printWriter无需手动关闭

This commit is contained in:
haoxr
2022-11-15 00:24:41 +08:00
parent 8c988de05a
commit dee6fa5cc4
7 changed files with 31 additions and 27 deletions

View File

@@ -4,6 +4,7 @@ import cn.hutool.json.JSONUtil;
import com.youlai.system.common.result.Result;
import com.youlai.system.common.result.ResultCode;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@@ -23,7 +24,7 @@ public class ResponseUtils {
* @param response
* @param resultCode
*/
public static void writeErrMsg(HttpServletResponse response, ResultCode resultCode) {
public static void writeErrMsg(HttpServletResponse response, ResultCode resultCode) throws IOException {
switch (resultCode) {
case ACCESS_UNAUTHORIZED:
case TOKEN_INVALID_OR_EXPIRED:
@@ -36,15 +37,9 @@ public class ResponseUtils {
response.setStatus(HttpStatus.BAD_REQUEST.value());
break;
}
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
response.setCharacterEncoding("UTF-8");
try {
String bodyJsonStr = JSONUtil.toJsonStr(Result.failed(resultCode));
PrintWriter printWriter = response.getWriter();
printWriter.print(bodyJsonStr);
printWriter.flush();
printWriter.close();
} catch (IOException e) {
}
response.getWriter().print(JSONUtil.toJsonStr(Result.failed(resultCode)));
}