Springboot2.0如何实现自定义异常并返回JSON格式的响应?

2026-06-09 07:253阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计533个文字,预计阅读时间需要3分钟。

Springboot2.0如何实现自定义异常并返回JSON格式的响应?

这篇文章简要介绍了Spring Boot 2.0处理自定义异常并返回JSON的过程。以下是通过示例代码展示的简要内容,适合初学者或工作者参考。

Springboot2.0如何实现自定义异常并返回JSON格式的响应?

1. 编写自定义异常

javapublic class CustomException extends RuntimeException { public CustomException(String message) { super(message); }}

2. 定义异常处理类

java@ControllerAdvicepublic class GlobalExceptionHandler { @ExceptionHandler(CustomException.class) @ResponseBody public ResponseEntity handleCustomException(CustomException ex) { return new ResponseEntity(new ErrorInfo(500, ex.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR); }}

3. 创建ErrorInfo类

javapublic class ErrorInfo { private int status; private String message;

public ErrorInfo(int status, String message) { this.status=status; this.message=message; }

// getters and setters}

以上内容仅为简要介绍,对于更深入的学习,建议参考相关书籍或资料。有需要的伙伴可以参考下+1。

这篇文章主要介绍了Springboot2.0处理自定义异常并返回json,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

1. 编写自定义异常类

package cn.jfjb.crud.exception; /** * @author john * @date 2019/11/24 - 9:48 */ public class UserNotExistException extends RuntimeException { public UserNotExistException() { super("用户不存在"); } }

2. 处理自测试异常

package cn.jfjb.crud.handler; import cn.jfjb.crud.exception.UserNotExistException; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; import java.util.HashMap; import java.util.Map; /** * @author john * @date 2019/11/24 - 10:43 */ @ControllerAdvice public class MyExceptionHandler { @ResponseBody @ExceptionHandler(UserNotExistException.class) public Map<String, Object> handleException(Exception e) { Map<String, Object> map = new HashMap<>(); map.put("code", "user.notexist"); map.put("message", e.getMessage()); return map; } }

3. 配置application.yml文件(不配置的话无法获取exception)

server: error: include-exception: true

4. 编写测试

package cn.jfjb.crud.controller; import cn.jfjb.crud.exception.UserNotExistException; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; /** * @author john * @date 2019/11/22 - 19:38 */ @Controller public class HelloController { @RequestMapping({"/testException"}) public String testException(@RequestParam("user") String user) { if (user != "aaa") { throw new UserNotExistException(); } return "index"; } }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

本文共计533个文字,预计阅读时间需要3分钟。

Springboot2.0如何实现自定义异常并返回JSON格式的响应?

这篇文章简要介绍了Spring Boot 2.0处理自定义异常并返回JSON的过程。以下是通过示例代码展示的简要内容,适合初学者或工作者参考。

Springboot2.0如何实现自定义异常并返回JSON格式的响应?

1. 编写自定义异常

javapublic class CustomException extends RuntimeException { public CustomException(String message) { super(message); }}

2. 定义异常处理类

java@ControllerAdvicepublic class GlobalExceptionHandler { @ExceptionHandler(CustomException.class) @ResponseBody public ResponseEntity handleCustomException(CustomException ex) { return new ResponseEntity(new ErrorInfo(500, ex.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR); }}

3. 创建ErrorInfo类

javapublic class ErrorInfo { private int status; private String message;

public ErrorInfo(int status, String message) { this.status=status; this.message=message; }

// getters and setters}

以上内容仅为简要介绍,对于更深入的学习,建议参考相关书籍或资料。有需要的伙伴可以参考下+1。

这篇文章主要介绍了Springboot2.0处理自定义异常并返回json,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

1. 编写自定义异常类

package cn.jfjb.crud.exception; /** * @author john * @date 2019/11/24 - 9:48 */ public class UserNotExistException extends RuntimeException { public UserNotExistException() { super("用户不存在"); } }

2. 处理自测试异常

package cn.jfjb.crud.handler; import cn.jfjb.crud.exception.UserNotExistException; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; import java.util.HashMap; import java.util.Map; /** * @author john * @date 2019/11/24 - 10:43 */ @ControllerAdvice public class MyExceptionHandler { @ResponseBody @ExceptionHandler(UserNotExistException.class) public Map<String, Object> handleException(Exception e) { Map<String, Object> map = new HashMap<>(); map.put("code", "user.notexist"); map.put("message", e.getMessage()); return map; } }

3. 配置application.yml文件(不配置的话无法获取exception)

server: error: include-exception: true

4. 编写测试

package cn.jfjb.crud.controller; import cn.jfjb.crud.exception.UserNotExistException; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; /** * @author john * @date 2019/11/22 - 19:38 */ @Controller public class HelloController { @RequestMapping({"/testException"}) public String testException(@RequestParam("user") String user) { if (user != "aaa") { throw new UserNotExistException(); } return "index"; } }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。