注解详解系列 - @ResponseStatus

注解简介

在今天的每日一注解中,我们将探讨@ResponseStatus注解。@ResponseStatus是Spring框架中的一个注解,用于为控制器方法指定HTTP响应状态码和理由短语。


注解定义

@ResponseStatus注解用于标记控制器方法或异常类,以指示HTTP响应的状态码和理由短语。以下是一个基本的示例:

java 复制代码
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {

    @GetMapping("/notfound")
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public String notFound() {
        return "Resource not found";
    }
}

在这个示例中,当访问/notfound路径时,Spring会返回404状态码,并在响应体中包含"Resource not found"消息。


注解详解

@ResponseStatus注解可以用来为控制器方法或自定义异常类指定HTTP响应状态码和理由短语。这在需要返回特定HTTP状态码时非常有用。

  • value: 指定HTTP状态码。
  • reason: 指定理由短语(可选)。

使用场景

@ResponseStatus广泛用于Spring MVC应用程序中,用于设置HTTP响应状态码。例如,处理资源未找到、权限不足、服务器内部错误等情况。


示例代码

以下是一个使用@ResponseStatus注解的代码示例,展示了如何处理自定义异常并返回特定的HTTP状态码:

java 复制代码
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {

    @GetMapping("/resource")
    public String getResource() {
        if (resourceNotFound()) {
            throw new ResourceNotFoundException();
        }
        return "Resource content";
    }

    private boolean resourceNotFound() {
        // 模拟资源未找到的情况
        return true;
    }

    @ExceptionHandler(ResourceNotFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public String handleResourceNotFound(ResourceNotFoundException e) {
        return e.getMessage();
    }
}

@ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "Resource Not Found")
class ResourceNotFoundException extends RuntimeException {
    public ResourceNotFoundException() {
        super("Resource not found");
    }
}

在这个示例中,当getResource方法检测到资源未找到时,会抛出ResourceNotFoundException异常,并返回404状态码和相应的错误消息。


常见问题

问题:如何在返回响应时设置自定义理由短语?

解决方案 :可以在@ResponseStatus注解中使用reason属性设置自定义理由短语。

java 复制代码
@ResponseStatus(value = HttpStatus.FORBIDDEN, reason = "Access Denied")
public class AccessDeniedException extends RuntimeException {
    public AccessDeniedException() {
        super("Access denied");
    }
}

问题:如何处理HTTP状态码的全局异常?

解决方案 :可以使用@ControllerAdvice@ExceptionHandler注解全局处理特定异常,并返回相应的HTTP状态码。

java 复制代码
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(AccessDeniedException.class)
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public String handleAccessDeniedException(AccessDeniedException e) {
        return e.getMessage();
    }
}

小结

通过今天的学习,我们了解了@ResponseStatus的基本用法和应用场景。明天我们将探讨另一个重要的Spring注解------@ControllerAdvice


相关链接

希望这个示例能帮助你更好地理解和应用@ResponseStatus注解。如果有任何问题或需要进一步的帮助,请随时告诉我。

相关推荐
zhz52144 小时前
Spring Boot + Redis 缓存性能优化实战:从5秒到毫秒级的性能提升
java·spring boot·redis·缓存·vue
lifallen4 小时前
Hadoop MapOutputBuffer:Map高性能核心揭秘
java·大数据·数据结构·hadoop·算法·apache
E_ICEBLUE4 小时前
如何使用 OCR 提取扫描件 PDF 的文本(Python 实现)
java·数据库·pdf
一 乐4 小时前
美食分享|基于Springboot和vue的地方美食分享网站系统设计与实现(源码+数据库+文档)
java·vue.js·spring boot·论文·毕设·美食·地方美食分享网站系统
MuMuMu#5 小时前
Spring Cloud Gateway基础复习
java·运维·学习
波波烤鸭5 小时前
深入理解 Gateway 网关:原理、源码解析与最佳实践
java·spring·gateway
FenceRain5 小时前
EasyExcel 实现国际化导入导出
java·spring boot·easyexcel
Leslie_Lei5 小时前
【Eclipse】eclipse打开git拉取的项目
java·git·eclipse
li35745 小时前
Spring Boot 中 StringRedisTemplate 与 RedisTemplate 的区别与使用陷阱(附 getBean 为何报错
java·spring boot·后端
AD钙奶-lalala5 小时前
RabbitMQ的核心使用示例
java·rabbitmq·java-rabbitmq