注解详解系列 - @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注解。如果有任何问题或需要进一步的帮助,请随时告诉我。

相关推荐
Boilermaker19923 小时前
[Java 并发编程] Synchronized 锁升级
java·开发语言
Cherry的跨界思维3 小时前
28、AI测试环境搭建与全栈工具实战:从本地到云平台的完整指南
java·人工智能·vue3·ai测试·ai全栈·测试全栈·ai测试全栈
alonewolf_994 小时前
JDK17新特性全面解析:从语法革新到模块化革命
java·开发语言·jvm·jdk
一嘴一个橘子4 小时前
spring-aop 的 基础使用(啥是增强类、切点、切面)- 2
java
sheji34164 小时前
【开题答辩全过程】以 中医药文化科普系统为例,包含答辩的问题和答案
java
恋爱绝缘体14 小时前
2020重学C++重构你的C++知识体系
java·开发语言·c++·算法·junit
xiaolyuh1235 小时前
Spring 框架 核心架构设计 深度详解
spring·设计模式·spring 设计模式
wszy18095 小时前
新文章标签:让用户一眼发现最新内容
java·python·harmonyos
wszy18095 小时前
顶部标题栏的设计与实现:让用户知道自己在哪
java·python·react native·harmonyos
程序员小假6 小时前
我们来说一下无锁队列 Disruptor 的原理
java·后端