Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to handle custom exceptions and return json in Springboot2.0

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article Xiaobian for you to introduce in detail "how to handle custom exceptions and return json in Springboot2.0", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to handle custom exceptions and return json in Springboot2.0" can help you solve your doubts.

1. Write custom exception classes

Package cn.jfjb.crud.exception;public class UserNotExistException extends RuntimeException {public UserNotExistException () {super ("user does not exist");}}

two。 Handling self-test exceptions

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;@ControllerAdvicepublic class MyExceptionHandler {@ ResponseBody @ ExceptionHandler (UserNotExistException.class) public Map handleException (Exception e) {Map map = new HashMap (); map.put ("code", "user.notexist") Map.put ("message", e.getMessage ()); return map;}}

3. Configure application.yml file (exception cannot be obtained without configuration)

Server: error: include-exception: true

4. Write tests

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;@Controllerpublic class HelloController {@ RequestMapping ({"/ testException"}) public String testException (@ RequestParam ("user") String user) {if (user! = "aaa") {throw new UserNotExistException ();} return "index";}}

After reading this, the article "how to handle custom exceptions and return json in Springboot2.0" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it yourself. If you want to know more about related articles, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report