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

相关推荐
悟空码字12 分钟前
SpringBoot深度整合高德地图,构建高性能位置服务
java·springboot·高德地图·编程技术·后端开发
千金裘换酒20 分钟前
栈和队列定义及常用语法 LeetCode
java·开发语言
0x5342 分钟前
JAVA|智能无人机平台(二)
java·开发语言·无人机
JH30731 小时前
SpringBoot自定义启动banner:给项目加个专属“开机画面”
java·spring boot·后端
假女吖☌1 小时前
限流算法-redis实现与java实现
java·redis·算法
lixin5565561 小时前
基于迁移学习的图像风格增强器
java·人工智能·pytorch·python·深度学习·语言模型
面汤放盐1 小时前
企业权限--系统性方案探究
java·开发语言
what丶k1 小时前
深度解析Redis LRU与LFU算法:区别、实现与选型
java·redis·后端·缓存
悟能不能悟1 小时前
java Date转换为string
java·开发语言
菜宾2 小时前
java-redis面试题
java·开发语言·redis