在前面代碼基礎上進行改造;
1.SpringBoot常用注解
@SpringBootApplication :指定SpringBoot項目啟動的入口,是一個復合注解,由@Configuration、@EnableAutoConfiguration、@ComponentScan三個注解。
@Configuration:表示將該類作用springboot配置文件類。
@EnableAutoConfiguration:表示程序啟動時,自動加載springboot默認的配置。
@ComponentScan:表示程序啟動是,自動掃描當前包及子包下所有類。
@SpringBootConfiguration:說明這是一個配置文件類,就像xml配置文件,而現在是用java配置文件。
@Value:通過這個注解可以來讀取.properties或.yml中配置的屬性。
@Bean:這個注解是方法級別上的注解,主要添加在 @Configuration 或
@SpringBootConfiguration 注解的類,有時也可以添加在 。
@Component 注解的類。它的作用是定義一個Bean。
2.攔截器
先定義攔截器:
import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * 攔截器 * @author XIHONGLEI * @date 2018-11-02 */ public class ApiInterceptor implements HandlerInterceptor { /** * 請求之前訪問 */ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { System.out.println("請求之前攔截..."); return true; } /** * 請求時訪問 */ @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) { System.out.println("請求中攔截..."); } /** * 請求完成后訪問 */ @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { System.out.println("請求完成后攔截..."); } }
再將自定義攔截器添加注冊進系統
import com.hello.filter.ApiInterceptor; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringBootConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; /** * 配置類 * @author XIHONGLEI * @date 2018-10-31 */ @SpringBootConfiguration public class WebConfig extends WebMvcConfigurationSupport { @Override protected void addInterceptors(InterceptorRegistry registry) { super.addInterceptors(registry); // 將 ApiInterceptor 攔截器類添加進去 registry.addInterceptor(new ApiInterceptor()); } }
看效果:
3.異常處理
創建異常處理類->加入@Aspect、@Component 注解->對請求鏈接進行攔截->發生異常之后的異常處理
import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; /** * 異常攔截處理 * * @author XIHONGLEI * @date 2018-11-02 */ @Aspect @Component public class WebExceptionAspect { //凡是注解了RequestMapping的方法都被攔截 @Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)") private void webPointcut() { System.out.println("請求被攔截..."); } /** * 攔截web層異常,記錄異常日志,並返回友好信息到前端 * 目前只攔截Exception,是否要攔截Error需再做考慮 * @param e 異常對象 */ @AfterThrowing(pointcut = "webPointcut()", throwing = "e") public void handleThrowing(Exception e) { System.out.println("出現異常:"); e.printStackTrace(); // 輸出錯誤提示到瀏覽器 writeContent("您請求的鏈接出現異常,請重試!"); } /** * 將內容輸出到瀏覽器 * @param content 輸出內容 */ private void writeContent(String content) { HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse(); response.reset(); response.setCharacterEncoding("UTF-8"); response.setHeader("Content-Type", "text/plain;charset=UTF-8"); response.setHeader("icop-content-type", "exception"); PrintWriter writer = null; try { writer = response.getWriter(); } catch (IOException e) { e.printStackTrace(); } writer.print(content); writer.flush(); writer.close(); } }