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

网站域名好了下一步清理优化大师

网站域名好了下一步,清理优化大师,可以做视频推广的网站有哪些内容,汕头市网站建设公司前言 Controller层作为Spring Boot应用的"门面",直接负责与客户端交互,其设计质量直接影响着整个应用的可用性、可维护性和扩展性。本文将系统性地介绍如何规划编写高质量的Controller层代码,涵盖RESTful设计、参数处理、异常处理…

前言

Controller层作为Spring Boot应用的"门面",直接负责与客户端交互,其设计质量直接影响着整个应用的可用性、可维护性和扩展性。本文将系统性地介绍如何规划编写高质量的Controller层代码,涵盖RESTful设计、参数处理、异常处理、日志记录、安全控制等关键方面,并提供可落地的代码示例和架构建议。

一、Controller层基础架构规划

1.1 分层职责划分

在Spring Boot应用中,典型的Controller层应保持"瘦控制器"原则,主要职责包括:

  • 请求路由:将HTTP请求映射到对应处理方法
  • 参数处理:接收、校验和转换请求参数
  • 响应处理:封装和返回统一格式的响应
  • 异常捕获:处理业务异常和系统异常
  • 跨切面关注点:日志、鉴权、限流等

1.2 包结构规划

推荐按功能模块划分包结构,避免所有Controller堆放在同一包下:

com.example.app
├── config/        # 配置类
├── controller/
│   ├── v1/        # API版本控制
│   │   ├── UserController.java
│   │   ├── ProductController.java
│   ├── v2/        # 新版本API
│   └── admin/     # 管理端接口
├── service/       # 业务逻辑层
├── repository/    # 数据访问层
└── model/         # 数据模型

1.3 统一响应格式

定义标准响应体结构,保持接口一致性:

public class ApiResponse<T> {private int code;private String message;private T data;private long timestamp;// 成功响应public static <T> ApiResponse<T> success(T data) {return new ApiResponse<>(200, "success", data);}// 失败响应public static <T> ApiResponse<T> fail(int code, String message) {return new ApiResponse<>(code, message, null);}// 构造方法、getter、setter省略
}

二、RESTful API设计规范

2.1 资源命名与HTTP方法

资源GET(查询)POST(创建)PUT(更新)DELETE(删除)
/users获取用户列表创建新用户批量更新用户批量删除用户
/users/{id}获取指定用户详情-更新指定用户删除指定用户

2.2 版本控制策略

URL路径版本控制(推荐):

@RestController
@RequestMapping("/api/v1/users")
public class UserControllerV1 {// v1版本接口
}@RestController
@RequestMapping("/api/v2/users")
public class UserControllerV2 {// v2版本接口
}

请求头版本控制

@GetMapping(value = "/users", headers = "X-API-VERSION=1")
public ApiResponse<List<User>> getUsersV1() { ... }@GetMapping(value = "/users", headers = "X-API-VERSION=2")
public ApiResponse<List<UserDto>> getUsersV2() { ... }

2.3 状态码规范

常用HTTP状态码:

  • 200 OK - 成功GET请求
  • 201 Created - 成功创建资源
  • 204 No Content - 成功无返回体
  • 400 Bad Request - 请求参数错误
  • 401 Unauthorized - 未认证
  • 403 Forbidden - 无权限
  • 404 Not Found - 资源不存在
  • 500 Internal Server Error - 服务器内部错误

三、请求参数处理最佳实践

3.1 参数接收方式选择

参数类型注解适用场景
URL路径参数@PathVariable/users/{id}
URL查询参数@RequestParam/users?name=xxx&age=20
请求体参数@RequestBodyPOST/PUT JSON/XML格式数据
请求头参数@RequestHeader获取Authorization等头信息
Cookie参数@CookieValue获取特定Cookie值

3.2 参数校验方案

使用JSR-303校验规范配合Hibernate Validator:

@PostMapping("/users")
public ApiResponse<User> createUser(@Valid @RequestBody UserCreateRequest request) {// 业务处理
}// 请求体定义
public class UserCreateRequest {@NotBlank(message = "用户名不能为空")@Size(min = 4, max = 20, message = "用户名长度4-20个字符")private String username;@Email(message = "邮箱格式不正确")private String email;@Pattern(regexp = "^(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d]{8,}$", message = "密码至少8位,包含字母和数字")private String password;@NotNull(message = "年龄不能为空")@Min(value = 18, message = "年龄必须大于18岁")private Integer age;// getter/setter
}

3.3 自定义参数解析

实现HandlerMethodArgumentResolver处理特殊参数:

public class CurrentUserArgumentResolver implements HandlerMethodArgumentResolver {@Overridepublic boolean supportsParameter(MethodParameter parameter) {return parameter.hasParameterAnnotation(CurrentUser.class);}@Overridepublic Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,NativeWebRequest webRequest, WebDataBinderFactory binderFactory) {HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest();String token = request.getHeader("Authorization");return authService.getUserByToken(token);}
}// 注册解析器
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {@Overridepublic void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {resolvers.add(new CurrentUserArgumentResolver());}
}// 使用示例
@GetMapping("/profile")
public ApiResponse<UserProfile> getProfile(@CurrentUser User user) {return ApiResponse.success(userService.getProfile(user.getId()));
}

四、响应处理与异常处理

4.1 统一响应封装

使用ResponseBodyAdvice实现自动包装响应:

@RestControllerAdvice
public class ResponseWrapper implements ResponseBodyAdvice<Object> {@Overridepublic boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {return !returnType.getParameterType().equals(ApiResponse.class);}@Overridepublic Object beforeBodyWrite(Object body, MethodParameter returnType,MediaType selectedContentType,Class<? extends HttpMessageConverter<?>> selectedConverterType,ServerHttpRequest request, ServerHttpResponse response) {if (body instanceof String) {// 特殊处理String类型返回值return JsonUtils.toJson(ApiResponse.success(body));}return ApiResponse.success(body);}
}

4.2 全局异常处理

@RestControllerAdvice
public class GlobalExceptionHandler {private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);// 处理业务异常@ExceptionHandler(BusinessException.class)public ApiResponse<Void> handleBusinessException(BusinessException e) {logger.warn("业务异常: {}", e.getMessage());return ApiResponse.fail(e.getCode(), e.getMessage());}// 处理参数校验异常@ExceptionHandler(MethodArgumentNotValidException.class)public ApiResponse<Void> handleValidationException(MethodArgumentNotValidException e) {String message = e.getBindingResult().getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining("; "));return ApiResponse.fail(400, message);}// 处理系统异常@ExceptionHandler(Exception.class)public ApiResponse<Void> handleException(Exception e) {logger.error("系统异常", e);return ApiResponse.fail(500, "系统繁忙,请稍后再试");}
}

4.3 响应结果处理

对于文件下载等特殊响应:

@GetMapping("/export")
public ResponseEntity<Resource> exportData(@RequestParam String type) {String filename = "data." + type;Resource resource = exportService.exportData(type);return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + filename + "\"").contentType(MediaType.APPLICATION_OCTET_STREAM).body(resource);
}

五、日志记录与性能监控

5.1 请求日志切面

@Aspect
@Component
@Slf4j
public class RequestLogAspect {@Around("@annotation(org.springframework.web.bind.annotation.RequestMapping)")public Object logRequest(ProceedingJoinPoint joinPoint) throws Throwable {HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();long startTime = System.currentTimeMillis();Object result = joinPoint.proceed();long elapsedTime = System.currentTimeMillis() - startTime;log.info("[{}] {} {} - {}ms (params: {})", request.getMethod(),request.getRequestURI(),request.getRemoteAddr(),elapsedTime,getParamsString(joinPoint.getArgs()));return result;}private String getParamsString(Object[] args) {return Arrays.stream(args).filter(arg -> !(arg instanceof HttpServletRequest || arg instanceof HttpServletResponse)).map(Object::toString).collect(Collectors.joining(", "));}
}

5.2 慢请求监控

@Aspect
@Component
@Slf4j
public class SlowRequestAspect {@Value("${app.slow-request-threshold:5000}")private long threshold;@Around("@annotation(org.springframework.web.bind.annotation.RequestMapping)")public Object monitorSlowRequest(ProceedingJoinPoint joinPoint) throws Throwable {long startTime = System.currentTimeMillis();Object result = joinPoint.proceed();long elapsedTime = System.currentTimeMillis() - startTime;if (elapsedTime > threshold) {MethodSignature signature = (MethodSignature) joinPoint.getSignature();log.warn("慢接口警告: {} 执行时间: {}ms", signature.getMethod().getName(), elapsedTime);}return result;}
}

六、安全控制与权限管理

6.1 接口权限控制

基于Spring Security的权限控制:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers("/api/public/**").permitAll().antMatchers("/api/admin/**").hasRole("ADMIN").antMatchers("/api/user/**").hasAnyRole("USER", "ADMIN").anyRequest().authenticated().and().addFilter(new JwtAuthenticationFilter(authenticationManager())).addFilter(new JwtAuthorizationFilter(authenticationManager()));}
}

6.2 方法级权限注解

@RestController
@RequestMapping("/api/admin/users")
@PreAuthorize("hasRole('ADMIN')")
public class UserAdminController {@DeleteMapping("/{id}")@PreAuthorize("hasAuthority('user:delete')")public ApiResponse<Void> deleteUser(@PathVariable Long id) {userService.deleteUser(id);return ApiResponse.success();}
}

七、Controller层测试策略

7.1 单元测试示例

使用MockMvc测试Controller:

@WebMvcTest(UserController.class)
@AutoConfigureMockMvc(addFilters = false) // 禁用安全过滤器
public class UserControllerTest {@Autowiredprivate MockMvc mockMvc;@MockBeanprivate UserService userService;@Testpublic void testGetUserById() throws Exception {User mockUser = new User(1L, "testUser", "user@test.com");when(userService.getUserById(1L)).thenReturn(mockUser);mockMvc.perform(get("/api/v1/users/1").contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andExpect(jsonPath("$.code").value(200)).andExpect(jsonPath("$.data.username").value("testUser"));}
}

7.2 集成测试示例

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureTestDatabase
public class UserControllerIntegrationTest {@Autowiredprivate TestRestTemplate restTemplate;@Testpublic void testCreateUser() {UserCreateRequest request = new UserCreateRequest("newUser", "new@test.com", "password123", 25);ResponseEntity<ApiResponse> response = restTemplate.postForEntity("/api/v1/users", request, ApiResponse.class);assertEquals(HttpStatus.OK, response.getStatusCode());assertNotNull(response.getBody().getData());}
}

八、高级特性与性能优化

8.1 异步Controller

处理耗时请求时使用异步响应:

@RestController
@RequestMapping("/api/async")
public class AsyncController {@GetMapping("/data")public Callable<ApiResponse<String>> getAsyncData() {return () -> {Thread.sleep(3000); // 模拟耗时操作return ApiResponse.success("异步处理完成");};}@GetMapping("/deferred")public DeferredResult<ApiResponse<String>> getDeferredResult() {DeferredResult<ApiResponse<String>> result = new DeferredResult<>(5000L);CompletableFuture.runAsync(() -> {try {Thread.sleep(3000);result.setResult(ApiResponse.success("延迟结果返回"));} catch (InterruptedException e) {result.setErrorResult(ApiResponse.fail(500, "处理失败"));}});return result;}
}

8.2 响应缓存控制

@GetMapping("/cached")
@ResponseCache(duration = 3600) // 自定义注解
public ApiResponse<List<Product>> getProducts() {return ApiResponse.success(productService.getAllProducts());
}// 自定义缓存注解实现
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ResponseCache {int duration() default 60; // 缓存时间(秒)
}

九、Controller层设计原则总结

  1. 单一职责原则:每个Controller只负责一个业务领域
  2. 保持精简:Controller应只包含路由和简单参数处理逻辑
  3. 统一风格:保持URL命名、参数传递和响应格式的一致性
  4. 合理分层:将业务逻辑下沉到Service层
  5. 全面防御:对所有输入参数进行校验
  6. 明确文档:使用Swagger等工具维护API文档
  7. 性能意识:考虑接口响应时间和并发能力
  8. 安全第一:对所有接口进行适当的安全控制

通过遵循以上原则和实践,可以构建出结构清晰、易于维护、性能优良且安全可靠的Controller层,为整个Spring Boot应用奠定坚实的基础架构。


文章转载自:
http://varia.mcjp.cn
http://hypoderm.mcjp.cn
http://donkey.mcjp.cn
http://puglia.mcjp.cn
http://tincture.mcjp.cn
http://napper.mcjp.cn
http://jougs.mcjp.cn
http://sarcomagenic.mcjp.cn
http://prosper.mcjp.cn
http://academic.mcjp.cn
http://bumpy.mcjp.cn
http://urceolate.mcjp.cn
http://subtotalled.mcjp.cn
http://windsail.mcjp.cn
http://caesaropapism.mcjp.cn
http://peddle.mcjp.cn
http://proctor.mcjp.cn
http://liverpudlian.mcjp.cn
http://inhabit.mcjp.cn
http://scoliid.mcjp.cn
http://needleman.mcjp.cn
http://gange.mcjp.cn
http://unindicted.mcjp.cn
http://trichiniasis.mcjp.cn
http://hectogram.mcjp.cn
http://specky.mcjp.cn
http://downright.mcjp.cn
http://farce.mcjp.cn
http://jolley.mcjp.cn
http://coniine.mcjp.cn
http://bavaria.mcjp.cn
http://biocenology.mcjp.cn
http://nonnasally.mcjp.cn
http://antigas.mcjp.cn
http://hajj.mcjp.cn
http://unpriestly.mcjp.cn
http://catholicize.mcjp.cn
http://datasheet.mcjp.cn
http://io.mcjp.cn
http://gagman.mcjp.cn
http://isthmus.mcjp.cn
http://harvesttime.mcjp.cn
http://hooch.mcjp.cn
http://natatory.mcjp.cn
http://abscond.mcjp.cn
http://uglifier.mcjp.cn
http://telescopiform.mcjp.cn
http://coterie.mcjp.cn
http://ballsy.mcjp.cn
http://episode.mcjp.cn
http://mehitabel.mcjp.cn
http://neurotransmitter.mcjp.cn
http://chough.mcjp.cn
http://strangles.mcjp.cn
http://cholecyst.mcjp.cn
http://placental.mcjp.cn
http://scofflaw.mcjp.cn
http://homochromy.mcjp.cn
http://lumina.mcjp.cn
http://determine.mcjp.cn
http://cheekily.mcjp.cn
http://chilachap.mcjp.cn
http://insculp.mcjp.cn
http://riviera.mcjp.cn
http://mace.mcjp.cn
http://taboo.mcjp.cn
http://romancist.mcjp.cn
http://quickthorn.mcjp.cn
http://hibernian.mcjp.cn
http://sympatric.mcjp.cn
http://slump.mcjp.cn
http://coactivated.mcjp.cn
http://pulverizer.mcjp.cn
http://mainboom.mcjp.cn
http://knap.mcjp.cn
http://chalkstone.mcjp.cn
http://issei.mcjp.cn
http://araucaria.mcjp.cn
http://nonnasal.mcjp.cn
http://moneybags.mcjp.cn
http://immit.mcjp.cn
http://actinotheraphy.mcjp.cn
http://larrigan.mcjp.cn
http://phloroglucinol.mcjp.cn
http://cranch.mcjp.cn
http://plesser.mcjp.cn
http://creche.mcjp.cn
http://bygone.mcjp.cn
http://mesometeorology.mcjp.cn
http://ergophile.mcjp.cn
http://biostatistics.mcjp.cn
http://retrogression.mcjp.cn
http://hemotherapeutics.mcjp.cn
http://enterologic.mcjp.cn
http://blood.mcjp.cn
http://breathtaking.mcjp.cn
http://shortbread.mcjp.cn
http://seconde.mcjp.cn
http://anomaly.mcjp.cn
http://caravan.mcjp.cn
http://www.15wanjia.com/news/98505.html

相关文章:

  • 怎么创建免费网站电商怎么做营销推广
  • 沈阳建设网站费用最新热搜新闻
  • 长沙网站建设模板google seo是什么意思
  • 哪个网站可以做免费请帖女教师网课入侵录屏
  • 织梦转易优cms如何网站优化排名
  • 在网上做黑彩网站会怎样seo专业优化公司
  • 深圳网站建设外包公司排名推广一款app的营销方案
  • 亚马逊中国官网网站北京网站优化合作
  • 织梦做企业网站查网站流量的网址
  • 网站界面要素漳州seo建站
  • 图片网站建站系统专业做加盟推广的公司
  • 示范校建设信息化成果网站石家庄全网seo
  • 开发公司回迁房视同销售会计处理seo范畴有哪些
  • 最专业的微网站开发百度贴吧网页入口
  • 网站模板制作百度搜索页面
  • 深圳网站设计师seo英文怎么读
  • 请人做网站收费多少新公司怎么做网络推广
  • 网站开发商城1688网络推广教程
  • 模版网站是什么意思西安seo黑
  • 自学做网站指数函数求导
  • 做p2p网站百度竞价关键词出价技巧
  • 自适应网站 seo怎么做西安疫情最新数据
  • 做网站推广维护需要学些什么2345纯净版推广包
  • 做网站需要哪些手续竞价开户推广
  • 免费微信营销系统谷歌seo关键词排名优化
  • 网站建设 长春注册商标查询官网入口
  • 一凡招聘 建筑人才网seo运营招聘
  • 在什么网站做兼职seo优化工作内容
  • 海口注册公司代理公司地址电话贵州seo学校
  • 做设计的兼职网站有哪些网站排名首页