package com.ai.demos.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializeFilter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;
import org.apache.log4j.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ServletUtil {
//服务器标识
private static String hostName = "";
//响应的ContentType内容
private static final String RESPONSE_CONTENTTYPE = "application/json";
//响应编码
private static final String RESPONSE_CHARACTERENCODING = "utf-8";
//业务名称的缩写
private static final String BIZ_NAME = "";
private static Logger log = Logger.getLogger(ServletUtil.class);
static{
try {
InetAddress netAddress = InetAddress.getLocalHost();
hostName = netAddress.getHostName();
} catch (UnknownHostException e) {
log.error("netAddress.getHostName failed", e);
}
}
/**
* 生成系统异常错误报文
* @param response
*/
public static String createSysErrorResponse(HttpServletResponse response){
final String code = "INTERNAL_SERVER_ERROR";
String message = "服务器内部错误";
return createErrorResponse(500,500,code, message,response);
}
/**
* 生成参数不正确报文
* @param response
*/
public static String createParamErrorResponse(HttpServletResponse response) {
final String code = "REQUIRE_ARGUMENT";
String message = "缺少参数";
return createErrorResponse(400,400,code,message,response);
}
/**
* 生成参数不正确报文
* @param param 缺少的参数名称
* @param response
*/
public static String createParamErrorResponse(String param,HttpServletResponse response) {
final String code = "REQUIRE_ARGUMENT";
String message = "缺少参数:" + param;
return createErrorResponse(400,400,code,message,response);
}
/**
* 认证失败
* @param response
*/
public static String createAuthorizationErrorResponse(HttpServletResponse response) {
final String code = "AUTH_INVALID_TOKEN";
String message = "请求认证失败!请按规范在Header报文头中附上正确的Authorization认证属性!";
return createErrorResponse(401,401,code, message,response);
}
/**
* 授权失败
* @param response
*/
public static String createAuthorizeErrorResponse(HttpServletResponse response) {
final String code = "AUTH_DENIED";
String message = "请求失败,没有访问或操作该资源的权限!";
return createErrorResponse(403,403,code, message,response);
}
/**
* 授权失败
* @param response
*/
public static String createAuthorizeErrorResponse(HttpServletResponse response, String message) {
final String code = "AUTH_DENIED";
return createErrorResponse(403,403,code, message,response);
}
/**
* 路径不存在
* @param response
*/
public static String createNotFoundErrorResponse(HttpServletResponse response) {
final String code = "NOT_FOUND";
String message = "请求的URL路径不存在!";
return createErrorResponse(404,404,code, message,response);
}
/**
* 生成错误报文
* @param errorCode
* @param response
*/
public static String createErrorResponse(ErrorCode errorCode,
HttpServletResponse response){
return createErrorResponse(errorCode.getHttpStatus(),errorCode.getRes_code(),errorCode.getCode(),
errorCode.getMessage(),response);
}
/**
* 生成错误报文
* @param httpStatus
* @param code
* @param message
* @param response
*/
public static String createErrorResponse(Integer httpStatus, Object code,
String message, HttpServletResponse response){
code = BIZ_NAME + code;
PrintWriter printWriter = null;
String jsonString = "";
try {
response.setCharacterEncoding(RESPONSE_CHARACTERENCODING);
response.setContentType(RESPONSE_CONTENTTYPE);
response.setStatus(httpStatus);
Map<String, Object> map = new HashMap<String, Object>();
map.put("code", code);
map.put("message", message);
//map.put("request_id", requestId==null?"":requestId);
//map.put("host_id", hostName);
map.put("server_time", DateUtil.formatISO8601DateString(new Date()));
printWriter = response.getWriter();
jsonString = JSON.toJSONString(map, SerializerFeature.WriteMapNullValue);
printWriter.write(jsonString);
printWriter.flush();
} catch (Exception e) {
log.error("createResponse failed", e);
} finally {
if(null!=printWriter)printWriter.close();
}
return jsonString;
}
/**
* 生成错误报文
* @param httpStatus
* @param res_code
* @param code
* @param message
* @param response
* @return
*/
public static String createErrorResponse(Integer httpStatus,Integer res_code, Object code,
String message, HttpServletResponse response){
code = BIZ_NAME + code;
PrintWriter printWriter = null;
String jsonString = "";
try {
response.setCharacterEncoding(RESPONSE_CHARACTERENCODING);
response.setContentType(RESPONSE_CONTENTTYPE);
response.setStatus(httpStatus);
Map<String, Object> map = new HashMap<String, Object>();
map.put("code", code);
map.put("message", message);
//map.put("request_id", requestId==null?"":requestId);
//map.put("host_id", hostName);
map.put("res_code", res_code);
map.put("server_time", DateUtil.formatISO8601DateString(new Date()));
printWriter = response.getWriter();
jsonString = JSON.toJSONString(map, SerializerFeature.WriteMapNullValue);
printWriter.write(jsonString);
printWriter.flush();
} catch (Exception e) {
log.error("createResponse failed", e);
} finally {
if(null!=printWriter)printWriter.close();
}
return jsonString;
}
/**
* 生成成功报文
* @param httpCode
* @param result
* @param response
*/
public static String createSuccessResponse(Integer httpCode, Object result, HttpServletResponse response){
return createSuccessResponse(httpCode, result, SerializerFeature.WriteMapNullValue,null,response);
}
public static String createSuccessResponse(Integer httpCode,String message,Object result,HttpServletResponse response){
return createSuccessResponse(httpCode,message,result, SerializerFeature.WriteMapNullValue,null,response);
}
/**
* 生成登录传成功报文
* @param httpCode
* @param result
* @param response
*/
public static String createLoginSuccessResponse(Integer httpCode, Object result, HttpServletResponse response){
/*response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods","POST, GET, PATCH, DELETE, PUT");
response.setHeader("Access-Control-Allow-Headers",request.getHeader("Access-Control-Request-Headers"));*/
return createSuccessResponse(httpCode, result, SerializerFeature.WriteMapNullValue,null,response);
}
public static String createSuccessResponse(Integer httpCode, Object result, SerializeFilter filter, HttpServletResponse response){
return createSuccessResponse(httpCode, result, SerializerFeature.PrettyFormat,filter,response);
}
public static String createSuccessResponse(Integer httpCode, Object result, SerializerFeature serializerFeature, HttpServletResponse response){
return createSuccessResponse(httpCode, result,serializerFeature,null,response);
}
public static String createSuccessResponse(Integer httpCode, Object result, SerializerFeature serializerFeature, SerializeFilter filter, HttpServletResponse response){
PrintWriter printWriter = null;
String jsonString = "";
try {
response.setCharacterEncoding(RESPONSE_CHARACTERENCODING);
response.set
没有合适的资源?快使用搜索试试~ 我知道了~
springboot创建web项目,配置了mybatis多数据源,实现了拦截器等.zip
共249个文件
js:78个
png:35个
css:32个
0 下载量 34 浏览量
2024-08-30
09:11:59
上传
评论
收藏 4.22MB ZIP 举报
温馨提示
项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松copy复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全栈开发),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助 【资源内容】:项目具体内容可查看/点击本页面下方的*资源详情*,包含完整源码+工程文件+说明(若有)等。【若无VIP,此资源可私信获取】 【本人专注IT领域】:有任何使用问题欢迎随时与我联系,我会及时解答,第一时间为您提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【适合场景】:相关项目设计中,皆可应用在项目开发、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面中 可借鉴此优质项目实现复刻,也可基于此项目来扩展开发出更多功能 #注 1. 本资源仅用于开源学习和技术交流。不可商用等,一切后果由使用者承担 2. 部分字体及插图等来自网络,若是侵权请联系删除,本人不对所涉及的版权问题或内容负法律责任。收取的费用仅用于整理和收集资料耗费时间的酬劳 3. 积分资源不提供使用问题指导/解答
资源推荐
资源详情
资源评论
收起资源包目录
springboot创建web项目,配置了mybatis多数据源,实现了拦截器等.zip (249个子文件)
mvnw.cmd 5KB
ace.min.css 322KB
jq22.css 274KB
bootstrap.min.css 118KB
font-awesome.min.css 49KB
font-awesome-ie7.min.css 37KB
ace-rtl.min.css 36KB
ace-skins.min.css 26KB
jquery-ui-1.10.3.full.min.css 24KB
bootstrap-theme.min.css 23KB
bootstrap-editable.css 21KB
select2.css 19KB
layer.css 14KB
chosen.css 13KB
ui.jqgrid.css 12KB
dropzone.css 11KB
fullcalendar.css 11KB
datepicker.css 10KB
ace-ie.min.css 7KB
daterangepicker-bs3.css 5KB
daterangepicker.css 5KB
googleFamily.css 5KB
colorbox.css 4KB
jquery-ui-1.10.3.custom.min.css 3KB
bootstrap-timepicker.css 3KB
carousel.css 2KB
colorpicker.css 2KB
jquery.gritter.css 2KB
bootstrap-treeview.css 1KB
bootstrap-multiselect.css 710B
boxDiv.css 631B
custom.css 471B
theme.css 399B
Thumbs.db 4KB
fontawesome-webfont.eot 59KB
glyphicons-halflings-regular.eot 20KB
loading.gif 9KB
loading-0.gif 6KB
loading-2.gif 2KB
loading-1.gif 701B
.gitignore 249B
index.html 23KB
main.html 16KB
learn-resource.html 9KB
login.html 4KB
maven-wrapper.jar 46KB
ServletUtil.java 12KB
LearnController.java 7KB
LearnDataBase.java 3KB
StudentDataBase.java 3KB
ValidatorUtil.java 3KB
MyInterceptor.java 2KB
ErrorCode.java 2KB
CalculateUtil.java 2KB
LoginController.java 2KB
LearnServiceImpl.java 1KB
StringUtil.java 1KB
StudentController.java 1KB
DateUtil.java 1KB
Student.java 1KB
Course.java 1KB
Score.java 1008B
Customer.java 934B
LearnResource.java 813B
Teacher.java 809B
MyWebMvcConfigurerAdapter.java 730B
StudentService.java 696B
User.java 625B
LearnResourceMapper.java 607B
LearnService.java 455B
CourseMapper.java 430B
IndexController.java 417B
TeacherMapper.java 382B
StudentMapper.java 382B
ScoreMapper.java 368B
DemoApplicationTests.java 326B
DemoApplication.java 300B
11401.jpg 474KB
11402.jpg 351KB
11403.jpg 283KB
bbg.jpg 141KB
image-4.jpg 89KB
image-2.jpg 79KB
image-1.jpg 66KB
image-3.jpg 61KB
image-5.jpg 59KB
image-6.jpg 42KB
btn.jpg 25KB
thumb-2.jpg 9KB
thumb-4.jpg 9KB
thumb-3.jpg 9KB
profile-pic.jpg 8KB
thumb-5.jpg 8KB
thumb-1.jpg 6KB
dudu.jpg 6KB
user_n.jpg 5KB
pass_n.jpg 5KB
thumb-6.jpg 5KB
code_n.jpg 4KB
user.jpg 1KB
共 249 条
- 1
- 2
- 3
资源评论
热爱技术。
- 粉丝: 2630
- 资源: 7860
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 淘宝客项目,支持App,微信小程序,QQ小程序.zip
- 第16课作业周旭.iml
- 海风小店,商城,微信小开源程序商城服务器端.zip
- 技术资料分享uCOS-II原理3很好的技术资料.zip
- 【java毕业设计】食品安全管理系统源码(springboot+vue+mysql+说明文档+LW).zip
- 海风小店,商城,微信小开源程序商城管理后台,后台管理,VUE.zip
- 【java毕业设计】流浪动物救助平台源码(springboot+vue+mysql+说明文档+LW).zip
- 技术资料分享uCOS-II原理2很好的技术资料.zip
- 流动图书馆微信小程序引入.zip
- 【java毕业设计】某大学外卖系统源码(springboot+前端+mysql+说明文档+LW).zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功