当前位置: 首页 > news >正文

【业务功能篇54】Springboot项目常用工具类:HTTP状态码/客户端request

 状态码常量类

/*** 返回状态码**/
public class HttpStatus {/*** 操作成功*/public static final int SUCCESS = 200;/*** 对象创建成功*/public static final int CREATED = 201;/*** 请求已经被接受*/public static final int ACCEPTED = 202;/*** 操作已经执行成功,但是没有返回数据*/public static final int NO_CONTENT = 204;/*** 资源已被移除*/public static final int MOVED_PERM = 301;/*** 重定向*/public static final int SEE_OTHER = 303;/*** 资源没有被修改*/public static final int NOT_MODIFIED = 304;/*** 参数列表错误(缺少,格式不匹配)*/public static final int BAD_REQUEST = 400;/*** 未授权*/public static final int UNAUTHORIZED = 401;/*** 访问受限,授权过期*/public static final int FORBIDDEN = 403;/*** 资源,服务未找到*/public static final int NOT_FOUND = 404;/*** 不允许的http方法*/public static final int BAD_METHOD = 405;/*** 资源冲突,或者资源被锁*/public static final int CONFLICT = 409;/*** 不支持的数据,媒体类型*/public static final int UNSUPPORTED_TYPE = 415;/*** 系统内部错误*/public static final int ERROR = 500;/*** 接口未实现*/public static final int NOT_IMPLEMENTED = 501;
}

客户端工具类  获取request\response

/*** 客户端工具类**/
public class ServletUtils {public static ServletRequestAttributes getRequestAttributes(){RequestAttributes attributes = RequestContextHolder.getRequestAttributes();return (ServletRequestAttributes) attributes;}/*** 获取request*/public static HttpServletRequest getRequest(){return getRequestAttributes().getRequest();}/*** 获取session*/public static HttpSession getSession(){return getRequest().getSession();}/*** 获取response*/public static HttpServletResponse getResponse(){return getRequestAttributes().getResponse();}/*** 获取String参数*/public static String getParameter(String name){return getRequest().getParameter(name);}/*** 获取Integer参数*/public static Integer getParameterToInt(String name){return Integer.parseInt(getRequest().getParameter(name));}/*** 将字符串渲染到客户端** @param response 渲染对象* @param string 待渲染的字符串* @return null*/public static String renderString(HttpServletResponse response, String string){try{response.setStatus(200);response.setContentType("application/json");response.setCharacterEncoding("utf-8");response.getWriter().print(string);}catch (IOException e){e.printStackTrace();}return null;}}

 

http://www.lryc.cn/news/99057.html

相关文章:

  • Fine Logic
  • Neo4j图数据基本操作
  • 前端JavaScript面试100问(中)
  • Docker 安全及日志管理与https部署
  • 2.3 HLSL常用函数
  • 互联网的发展
  • STM32 CAN通讯实验程序
  • Python代码片段之Django静态文件URL的配置
  • 基于飞桨paddle的极简方案构建手写数字识别模型测试代码
  • soft ip与hard ip
  • MyBatisPlus从入门到精通-2
  • AI面试官:Asp.Net 中使用Log4Net (一)
  • Selenium自动化元素定位方式与浏览器测试脚本
  • 人机交互与人机混合智能的区别
  • 【项目】轻量级HTTP服务器
  • sketch如何在线打开?有没有什么软件可以辅助
  • CSS Flex 笔记
  • Markdown常用标签及其用途-有示例
  • 25.1 Knife4j-Swagger的增强插件
  • 用flask run代替flask run --debug
  • python_day14_综合案例
  • 【算法题】2779. 数组的最大美丽值
  • 文件上传之PHP
  • 人脸检测实战-insightface
  • Linux工具【1】(编辑器vim、编译器gcc与g++)
  • 基于Java+SpringBoot+vue前后端分离古典舞在线交流平台设计实现
  • MQ - 闲聊MQ一二事儿 (Kafka、RocketMQ 、Pulsar )
  • Qt中的 QIODevice类(包含:随机访问、顺序访问设备)
  • 【JavaScript 07】函数声明 地位平等 函数提升 属性方法 作用域 参数 arguments对象 闭包 IIFE立即调用函数表达式 eval命令
  • MyBatis源码分析_ResultSetHandler(7)