溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

SpringBoot2異常處理與web原生組件注入的方法

發布時間:2022-03-22 13:35:12 來源:億速云 閱讀:102 作者:iii 欄目:開發技術

SpringBoot2異常處理與Web原生組件注入的方法

引言

在Spring Boot 2.x中,異常處理和Web原生組件的注入是開發Web應用程序時經常遇到的兩個重要主題。Spring Boot提供了多種機制來處理異常,并且允許開發者輕松地將Servlet、Filter、Listener等Web原生組件注入到Spring容器中。本文將詳細介紹如何在Spring Boot 2.x中進行異常處理以及如何注入Web原生組件。

一、Spring Boot 2.x中的異常處理

1.1 默認異常處理機制

Spring Boot默認提供了一個BasicErrorController來處理應用程序中的異常。當應用程序拋出未捕獲的異常時,Spring Boot會自動將請求重定向到/error路徑,并返回一個包含錯誤信息的JSON響應或HTML頁面。

1.1.1 自定義錯誤頁面

要自定義錯誤頁面,可以在src/main/resources/templates目錄下創建一個error.html文件。Spring Boot會自動使用這個文件來渲染錯誤頁面。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Error</title>
</head>
<body>
    <h1>Error Page</h1>
    <p>Status: [[${status}]]</p>
    <p>Error: [[${error}]]</p>
    <p>Message: [[${message}]]</p>
</body>
</html>

1.1.2 自定義錯誤JSON響應

要自定義錯誤JSON響應,可以通過實現ErrorAttributes接口或繼承DefaultErrorAttributes類來定制錯誤信息的格式。

import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.WebRequest;

import java.util.Map;

@Component
public class CustomErrorAttributes extends DefaultErrorAttributes {

    @Override
    public Map<String, Object> getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) {
        Map<String, Object> errorAttributes = super.getErrorAttributes(webRequest, includeStackTrace);
        errorAttributes.put("customAttribute", "This is a custom error attribute");
        return errorAttributes;
    }
}

1.2 使用@ControllerAdvice進行全局異常處理

@ControllerAdvice注解允許我們定義一個全局的異常處理類,用于處理應用程序中所有控制器拋出的異常。

1.2.1 定義全局異常處理類

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResponseEntity<String> handleException(Exception ex) {
        return new ResponseEntity<>("An error occurred: " + ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
    }

    @ExceptionHandler(IllegalArgumentException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ResponseEntity<String> handleIllegalArgumentException(IllegalArgumentException ex) {
        return new ResponseEntity<>("Invalid argument: " + ex.getMessage(), HttpStatus.BAD_REQUEST);
    }
}

1.2.2 處理特定控制器的異常

@ControllerAdvice還可以與@ExceptionHandler結合使用,處理特定控制器拋出的異常。

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;

@ControllerAdvice(assignableTypes = {MyController.class})
public class MyControllerExceptionHandler {

    @ExceptionHandler(MyCustomException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ResponseEntity<String> handleMyCustomException(MyCustomException ex) {
        return new ResponseEntity<>("Custom exception: " + ex.getMessage(), HttpStatus.BAD_REQUEST);
    }
}

1.3 使用@ExceptionHandler進行局部異常處理

@ExceptionHandler注解可以用于控制器內部,處理該控制器拋出的特定異常。

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {

    @ExceptionHandler(IllegalArgumentException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ResponseEntity<String> handleIllegalArgumentException(IllegalArgumentException ex) {
        return new ResponseEntity<>("Invalid argument: " + ex.getMessage(), HttpStatus.BAD_REQUEST);
    }

    @GetMapping("/test")
    public String test() {
        throw new IllegalArgumentException("Test exception");
    }
}

1.4 使用@ResponseStatus自定義HTTP狀態碼

@ResponseStatus注解可以用于自定義異常類,指定當該異常拋出時返回的HTTP狀態碼。

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "Resource not found")
public class ResourceNotFoundException extends RuntimeException {
    public ResourceNotFoundException(String message) {
        super(message);
    }
}

1.5 使用ErrorController自定義錯誤處理

通過實現ErrorController接口,可以完全自定義錯誤處理邏輯。

import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
public class MyErrorController implements ErrorController {

    @RequestMapping("/error")
    public String handleError(HttpServletRequest request) {
        // 自定義錯誤處理邏輯
        return "error";
    }

    @Override
    public String getErrorPath() {
        return "/error";
    }
}

二、Web原生組件的注入

Spring Boot允許開發者將Servlet、Filter、Listener等Web原生組件注入到Spring容器中,從而在Spring Boot應用程序中使用這些組件。

2.1 注入Servlet

2.1.1 使用@WebServlet注解

可以通過@WebServlet注解將Servlet注冊到Spring Boot應用程序中。

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "myServlet", urlPatterns = "/myServlet")
public class MyServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.getWriter().write("Hello from MyServlet");
    }
}

2.1.2 使用ServletRegistrationBean

可以通過ServletRegistrationBean將Servlet注冊到Spring Boot應用程序中。

import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ServletConfig {

    @Bean
    public ServletRegistrationBean<MyServlet> myServletRegistrationBean() {
        return new ServletRegistrationBean<>(new MyServlet(), "/myServlet");
    }
}

2.2 注入Filter

2.2.1 使用@WebFilter注解

可以通過@WebFilter注解將Filter注冊到Spring Boot應用程序中。

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

@WebFilter(urlPatterns = "/*")
public class MyFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // 初始化邏輯
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        // 過濾邏輯
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {
        // 銷毀邏輯
    }
}

2.2.2 使用FilterRegistrationBean

可以通過FilterRegistrationBean將Filter注冊到Spring Boot應用程序中。

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FilterConfig {

    @Bean
    public FilterRegistrationBean<MyFilter> myFilterRegistrationBean() {
        FilterRegistrationBean<MyFilter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(new MyFilter());
        registrationBean.addUrlPatterns("/*");
        return registrationBean;
    }
}

2.3 注入Listener

2.3.1 使用@WebListener注解

可以通過@WebListener注解將Listener注冊到Spring Boot應用程序中。

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@WebListener
public class MyListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        // 上下文初始化邏輯
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        // 上下文銷毀邏輯
    }
}

2.3.2 使用ServletListenerRegistrationBean

可以通過ServletListenerRegistrationBean將Listener注冊到Spring Boot應用程序中。

import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ListenerConfig {

    @Bean
    public ServletListenerRegistrationBean<MyListener> myListenerRegistrationBean() {
        return new ServletListenerRegistrationBean<>(new MyListener());
    }
}

2.4 注入ServletContextInitializer

ServletContextInitializer接口允許開發者在Servlet上下文初始化時執行自定義邏輯。

import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.stereotype.Component;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;

@Component
public class MyServletContextInitializer implements ServletContextInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        // 自定義初始化邏輯
    }
}

2.5 注入ServletContextListener

ServletContextListener接口允許開發者在Servlet上下文初始化和銷毀時執行自定義邏輯。

import org.springframework.stereotype.Component;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

@Component
public class MyServletContextListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        // 上下文初始化邏輯
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        // 上下文銷毀邏輯
    }
}

三、總結

Spring Boot 2.x提供了多種機制來處理異常,并且允許開發者輕松地將Servlet、Filter、Listener等Web原生組件注入到Spring容器中。通過合理地使用這些機制,開發者可以構建出更加健壯和靈活的Web應用程序。

在異常處理方面,Spring Boot提供了默認的異常處理機制,開發者可以通過自定義錯誤頁面、錯誤JSON響應、全局異常處理、局部異常處理等方式來滿足不同的需求。

在Web原生組件的注入方面,Spring Boot支持通過注解和編程式配置兩種方式來注冊Servlet、Filter、Listener等組件。開發者可以根據具體需求選擇合適的方式來注入這些組件。

通過本文的介紹,相信讀者已經對Spring Boot 2.x中的異常處理和Web原生組件注入有了更深入的了解。希望這些知識能夠幫助開發者在實際項目中更好地應用Spring Boot,構建出高質量的Web應用程序。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女