最近在做项目时需要对异常进行全局统一处理,主要是一些分类入库以及记录日志等,因为项目是基于Springboot的,所以去网络上找了一些博客文档,然后再结合项目本身的一些特殊需求做了些许改造,现在记录下来便于以后查看。
package com.glxcn.common.exception;
/**
* Created by WF on 2019/4/28.
*/
import com.glxcn.common.utils.UserAgentUtil;
import com.glxcn.module.api.andiro.service.GongLueService;
import com.glxcn.module.o7xz.seo.service.SeoService;
import com.glxcn.mybatis.mapper.TO7xzAppMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
@Controller
class MainsiteErrorControllerimplements ErrorController {
@Autowired
private SeoServiceseoService;
@Autowired
private GongLueServicegongLueService;;
@RequestMapping("/error")
public StringhandleError(HttpServletRequest request,Model model){
System.out.println("进入异常方法");
String userAgent = request.getHeader("User-Agent");
String os = UserAgentUtil.getOS(userAgent);
//获取statusCode:401,404,500
Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
if(statusCode ==401){
return "/401";
}else if(statusCode ==404){
if (os.equals("Android")){
System.out.println("安卓异常成功");
//keword,描述等
model.addAttribute("keyd",seoService.selectByTitle("软件下载_软件下载中心_手机游戏下载-免费电脑软件下载就上07下载站"));
//8个APP
model.addAttribute("appinfo",gongLueService.selectByDown(8));
//5条游戏攻略
model.addAttribute("recommendList",gongLueService.findByNewsCount());
//4个软件教程
model.addAttribute("fourlist",gongLueService.selectAllFive(4));
return "/www/android/404";
}
else if (os.equals("iPad")||os.equals("iPhone")){
//keword,描述等
model.addAttribute("keyd",seoService.selectByTitle("软件下载_软件下载中心_手机游戏下载-免费电脑软件下载就上07下载站"));
//8个APP
model.addAttribute("appinfo",gongLueService.selectByDown(8));
//5条游戏攻略
model.addAttribute("recommendList",gongLueService.findByNewsCount());
//4个软件教程
model.addAttribute("fourlist",gongLueService.selectAllFive(4));
return "/www/ios/404";
}else{
return "/404";
}
}else if(statusCode ==403){
return "/403";
}else{
return "/500";
}
}
@Override
public StringgetErrorPath() {
return "/error";
}
}