苍穹外卖中的模块总结

devtools/2025/2/24 14:16:26/

本文总结苍穹外卖项目中可复用的通用设计

sky-common

在这里插入图片描述
constant存放常量类,包括消息常量,状态常量
context是上下文对象,封装了threadlocal

package com.sky.context;public class BaseContext {public static ThreadLocal<Long> threadLocal = new ThreadLocal<>();public static void setCurrentId(Long id) {threadLocal.set(id);}public static Long getCurrentId() {return threadLocal.get();}public static void removeCurrentId() {threadLocal.remove();}}

enumeration存放枚举类
exception存放自定义异常类,用于抛出自定义异常

/*** 业务异常*/
public class BaseException extends RuntimeException {public BaseException() {}public BaseException(String msg) {super(msg);}}
/*** 账号被锁定异常*/
public class AccountLockedException extends BaseException {public AccountLockedException() {}public AccountLockedException(String msg) {super(msg);}}

properties用于配置工具类的属性,@ConfigurationProperties(prefix = “sky.alioss”)读取application.yml中以sky.alioss为前缀的具体值

@Component
@ConfigurationProperties(prefix = "sky.alioss")
@Data
public class AliOssProperties {private String endpoint;private String accessKeyId;private String accessKeySecret;private String bucketName;
}

utils封装具体的工具类
result封装返回给前端的响应类
json封装对象映射器(基于jackson将Java对象转为json,或者将json转为Java对象),定制 JSON 与 Java 对象之间的序列化(对象转JSON)和反序列化(JSON转对象)规则

sky-pojo

在这里插入图片描述

entity封装实体类,dto封装前端向后端传递数据的对象,通常是entity中某个类的部分数据,vo封装后端向前端返回数据的对象,按照前端需要的数据格式进行设计

sky-server

在这里插入图片描述
annotationaspect负责springboot的aop切面编程
config用于注册Bean

WebMvcConfiguration

/*** 配置类,注册web层相关组件*/
@Configuration
@Slf4j
public class WebMvcConfiguration extends WebMvcConfigurationSupport {@Autowiredprivate JwtTokenAdminInterceptor jwtTokenAdminInterceptor;@Autowiredprivate JwtTokenUserInterceptor jwtTokenUserInterceptor;/*** 注册自定义拦截器** @param registry*/protected void addInterceptors(InterceptorRegistry registry) {log.info("开始注册自定义拦截器...");registry.addInterceptor(jwtTokenAdminInterceptor).addPathPatterns("/admin/**").excludePathPatterns("/admin/employee/login");registry.addInterceptor(jwtTokenUserInterceptor).addPathPatterns("/user/**").excludePathPatterns("/user/user/login").excludePathPatterns("/user/shop/status");}/*** 通过knife4j生成接口文档* @return*/@Beanpublic Docket docket1() {ApiInfo apiInfo = new ApiInfoBuilder().title("苍穹外卖项目接口文档").version("2.0").description("苍穹外卖项目接口文档").build();Docket docket = new Docket(DocumentationType.SWAGGER_2).groupName("管理端接口").apiInfo(apiInfo).select().apis(RequestHandlerSelectors.basePackage("com.sky.controller.admin")).paths(PathSelectors.any()).build();return docket;}@Beanpublic Docket docket2() {ApiInfo apiInfo = new ApiInfoBuilder().title("苍穹外卖项目接口文档").version("2.0").description("苍穹外卖项目接口文档").build();Docket docket = new Docket(DocumentationType.SWAGGER_2).groupName("用户端接口").apiInfo(apiInfo).select().apis(RequestHandlerSelectors.basePackage("com.sky.controller.user")).paths(PathSelectors.any()).build();return docket;}/*** 设置静态资源映射* @param registry*/protected void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");}/*** 扩展spring mvc 的消息转换器* @param converters*/@Overrideprotected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {log.info("扩展消息转换器");//创建一个消息转换器对象MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();//为消息转换器设置一个对象转换器,对象转换器可将java对象序列化converter.setObjectMapper(new JacksonObjectMapper());//将消息转换器添加到convertersconverters.add(0,converter);}}

z

public class OssConfiguration {@Bean@ConditionalOnMissingBeanpublic AliOssUtil aliOssUtil(AliOssProperties aliOssProperties) {log.info("开始创建阿里云上传文件工具类对象",aliOssProperties);return new AliOssUtil(aliOssProperties.getEndpoint(),aliOssProperties.getAccessKeyId(),aliOssProperties.getAccessKeySecret(),aliOssProperties.getBucketName());}
}

这里用到前面common中提到的AliOssProperties来创建AliOssUtil类
handle用于定义全局异常处理器,处理各个地方抛出的异常

/*** 全局异常处理器,处理项目中抛出的业务异常*/
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {/*** 捕获业务异常* @param ex* @return*/@ExceptionHandlerpublic Result exceptionHandler(BaseException ex){log.error("异常信息:{}", ex.getMessage());return Result.error(ex.getMessage());}@ExceptionHandlerpublic Result exceptionHandler(SQLIntegrityConstraintViolationException ex){String msg=ex.getMessage();if(msg.contains("Duplicate entry")){String[] msgs=msg.split(" ");String username=msgs[2];return Result.error(username+MessageConstant.ALREADY_EXISTS);}else{return Result.error(MessageConstant.UNKNOWN_ERROR);}}}

@RestControllerAdvice:表示这是一个全局异常处理类,会拦截所有 @RestController 或 @Controller 抛出的异常,并将处理结果以 JSON 格式返回给前端。
@ExceptionHandler :是 Spring 框架中用于集中处理异常的核心注解,它的核心作用是捕获并处理控制器(Controller)中抛出的特定异常,返回统一的错误响应。
interceptor用于定义拦截器
task定义定时任务类,与websocket配合使用


http://www.ppmy.cn/devtools/161376.html

相关文章

Automa 浏览器自动化编排 实现自动化浏览器操作

在日常的浏览器使用过程中&#xff0c;我们常常会遇到一些重复繁琐的任务&#xff0c;比如反复填写网页表单、从网页抓取数据、定时截图等&#xff0c;这些工作不仅耗费时间和精力&#xff0c;还容易出错。今天要给大家介绍的Automa&#xff0c;就是一款专门用来解决这类问题的…

ctf网络安全题库 ctf网络安全大赛答案

此题解仅为部分题解&#xff0c;包括&#xff1a; 【RE】&#xff1a;①Reverse_Checkin ②SimplePE ③EzGame 【Web】①f12 ②ezrunner 【Crypto】①MD5 ②password ③看我回旋踢 ④摩丝 【Misc】①爆爆爆爆 ②凯撒大帝的三个秘密 ③你才是职业选手 一、 Re ① Reverse Chec…

基于Flask的租房信息可视化系统的设计与实现

【Flask】基于Flask的租房信息可视化系统的设计与实现&#xff08;完整系统源码开发笔记详细部署教程&#xff09;✅ 目录 一、项目简介二、项目界面展示三、项目视频展示 一、项目简介 随着互联网的快速发展&#xff0c;租房市场日益繁荣&#xff0c;信息量急剧增加&#xff…

数据结构与算法再探(七)查找-排序

查找 一、二分查找 二分查找是一种高效的查找算法&#xff0c;适用于在已排序的数组或列表中查找特定元素。它通过将搜索范围逐步减半来快速定位目标元素。理解二分查找的“不变量”和选择左开右闭区间的方式是掌握这个算法的关键。 二分查找关键点 不变量 在二分查找中&a…

MATLAB进阶之路:数据导入与处理

在MATLAB的学习旅程中,我们已经初步了解了它的基础操作。如今,我们将沿着这条充满惊喜的道路,迈向下一个重要的站点——数据导入与处理。这部分内容就像是为MATLAB注入了强大的能量,使其能够从现实的数据世界中汲取信息,然后像一位智慧的魔法师一样,巧妙地处理这些数据,…

会话对象 Cookie 四、Cookie的路径

1.Cookie的path属性 Cookie还有一个path属性&#xff0c;可以通过Cookie#setPath(String)方法来设置。你可以使用HttpWatch查看响应中的Set-Cookie中是否存在路径。下面是通过Chrome查看Cookie信息。 也就是说&#xff0c;就算你不设置Cookie的path&#xff0c;Cookie也是有路…

文心一言大模型的“三级跳”:从收费到免费再到开源,一场AI生态的重构实验

2025年2月&#xff0c;百度文心大模型接连抛出两枚“重磅炸弹”&#xff1a;4月1日起全面免费&#xff0c;6月30日正式开源文心大模型4.5系列。这一系列动作不仅颠覆了李彦宏此前坚持的“闭源优势论”13&#xff0c;更标志着中国AI大模型竞争进入了一个全新的阶段——从技术壁垒…

MapReduce 第二部:深入分析与实践

在第一部分中&#xff0c;我们了解了MapReduce的基本概念和如何使用Python2编写MapReduce程序进行简单的单词计数。今天&#xff0c;我们将深入探讨如何使用MapReduce处理更复杂的数据源&#xff0c;比如HDFS中的CSV文件&#xff0c;并将结果输出到HDFS。通过更复杂的实践案例&…