账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    springboot怎么捕获TemplateInputException异常
    • 2020-01-01 00:00
    • 11
    34
    0

    使用@ControllerAdvice注解时捕获不到,如何解决?

    1
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 11 元积分
        全部回答
    • 0
    • 漠名流眼泪 普通会员 1楼

      在Spring Boot中,你可以使用TemplateInputException异常的catch块来捕获这个异常。以下是一个例子:

      ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;

      import java.io.IOException; import java.util.Map;

      @RestController public class MyController {

      @Autowired
      private MyService myService;
      
      @GetMapping("/my-template")
      public String handleTemplateInputException(@RequestParam("templateInput") Map<String, String> templateInput) {
          try {
              return myService.handleTemplateInput(templateInput);
          } catch (TemplateInputException e) {
              e.printStackTrace();
              return "Error: " + e.getMessage();
          }
      }
      

      } ```

      在这个例子中,我们首先通过@Autowired注解将MyService注入到MyController中。然后,我们在handleTemplateInputException方法中处理TemplateInputException异常。如果出现异常,我们将异常信息打印到控制台,并返回一个错误消息。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部