SpringBoot啟動遇到的異常問題及解決方案
SpringBoot啟動遇到異常
1、 問題
SpringBoot本身需要引入自身的一個parent,但是pom里面一般都已經(jīng)存在了一個parent,這時就不能在引入springBoot的parent
解決方案:
<dependency> ? ? ? ? ? ? <groupId>org.springframework.boot</groupId> ? ? ? ? ? ? <artifactId>spring-boot-starter-web</artifactId> ? ? ? ? ? ? <version>1.3.3.RELEASE</version> ? ? ? ? </dependency> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>org.springframework.boot</groupId> ? ? ? ? ? ? <artifactId>spring-boot-dependencies</artifactId> ? ? ? ? ? ? <version>1.5.6.RELEASE</version> ? ? ? ? ? ? <type>pom</type> ? ? ? ? ? ? <scope>import</scope> ? ? ? ? </dependency>
2、異常
Exception in thread "main" java.lang.IllegalArgumentException: Cannot instantiate interface org.springframework.context.ApplicationListener : org.springframework.boot.logging.ClasspathLoggingApplicationListener
。。。。。。
Caused by: java.lang.NoClassDefFoundError: org/springframework/context/event/GenericApplicationListener
解決方案:
這個問題可能是由于Spring的版本低導致,升級spring版本。親測到4.2.5.RELEASE可以
<org.springframework.version>4.2.5.RELEASE</org.springframework.version>
3、異常
Exception in thread "main" java.lang.IllegalArgumentException: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. Either remove Logback or the competing implementation (class org.slf4j.impl.Log4jLoggerFactory loaded from file:/D:/maven/repository/org/slf4j/slf4j-log4j12/1.7.5/slf4j-log4j12-1.7.5.jar). If you are using WebLogic you will need to add 'org.slf4j' to prefer-application-packages in WEB-INF/weblogic.xml Object of class [org.slf4j.impl.Log4jLoggerFactory] must be an instance of class ch.qos.logback.classic.LoggerContext
at org.springframework.util.Assert.isInstanceOf(Assert.java:346)
at org.springframework.boot.logging.logback.LogbackLoggingSystem.getLoggerContext(LogbackLoggingSystem.java:221)
解決方案:
這個異常是由于打印日志的jar沖突導致,SpringBoot本身有打印日志的功能,如果跟本地的沖突,就需要去掉,如下
? ?<dependency> ? ? ? ? ? ? <groupId>org.springframework.boot</groupId> ? ? ? ? ? ? <artifactId>spring-boot-starter-web</artifactId> ? ? ? ? ? ? <version>1.3.3.RELEASE</version> ? ? ? ? ? ? <exclusions> ? ? ? ? ? ? ? ? <exclusion> ? ? ? ? ? ? ? ? ? ? <groupId>org.springframework.boot</groupId> ? ? ? ? ? ? ? ? ? ? <artifactId>spring-boot-starter-logging</artifactId> ? ? ? ? ? ? ? ? </exclusion> ? ? ? ? ? ? </exclusions> ? ? ? ? </dependency>
4、異常
Cannot instantiate factory class: org.springframework.boot.autoconfigure.AutoConfigurationImportFilter
Caused by: java.lang.IllegalAccessException: Class org.springframework.core.io.support.SpringFactoriesLoader can not access a member of class org.springframework.boot.autoconfigure.condition.OnClassCondition with modifiers ""
解決方案:
這種可以檢查org.springframework.boot的版本,可能是版本不兼容,我這里一開始設置的1.5.6.RELEASE,一直出異常,后來SpringBoot的版本改成全部改成1.3.3.RELEASE 把Spring的版本改成4.2.5.RELEASE,通過
5、 異常
SpringBoot啟動
xport.annotation.AnnotationMBeanExporter -Unregistering JMX-exposed beans on shutdown ,tomcat也沒有運行
可能原因1:
看看是否屏蔽了,檢查 <artifactId>spring-boot-starter-web</artifactId> 這個下面是否屏蔽了spring-boot-starter-tomcat,
如果有,請去掉
<exclusion> ? ? ? ? ? ? ? ? ? ? <groupId>org.springframework.boot</groupId> ? ? ? ? ? ? ? ? ? ? <artifactId>spring-boot-starter-tomcat</artifactId> ? ? ? ? ? ? ? ? </exclusion>
可能原因2:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>
將<scope>provided</scope>注釋掉
6、異常
Caused by: java.lang.NoClassDefFoundError: org/springframework/beans/factory/ObjectProvider
Caused by: java.lang.ClassNotFoundException: org.springframework.beans.factory.ObjectProvider
解決方案:
這個異常最蛋疼,搞了半天是SpringBoot的版本不兼容,切換了一個版本到1.3.3.RELEASE 這里就好了
? ? ? ? <dependency> ? ? ? ? ? ? <groupId>org.springframework.boot</groupId> ? ? ? ? ? ? <artifactId>spring-boot-starter-web</artifactId> ? ? ? ? ? ? <version>1.3.3.RELEASE</version> ? ? ? ? ? ? <exclusions> ? ? ? ? ? ? ? ? <exclusion> ? ? ? ? ? ? ? ? ? ? <groupId>org.springframework.boot</groupId> ? ? ? ? ? ? ? ? ? ? <artifactId>spring-boot-starter-logging</artifactId> ? ? ? ? ? ? ? ? </exclusion> ? ? ? ? ? ? </exclusions> ? ? ? ? </dependency>
建議把<groupId>org.springframework.boot</groupId>這下下面的版本都改成1.3.3.RELEASE
7、異常
java.lang.NoSuchMethodError: org.springframework.expression.spel.SpelParserConfiguration.<init>(Lorg/springframework/expression/spel/SpelCompilerMode;Ljava/lang/ClassLoader
解決方案:
缺少jar
? ? <dependency> ? ? ? ? ? ? ? ? <groupId>org.springframework</groupId> ? ? ? ? ? ? ? ? <artifactId>spring-expression</artifactId> ? ? ? ? ? ? ? ? <version>4.2.5.RELEASE</version> ? ? ? ? ? ? </dependency>
SpringBoot優(yōu)雅的處理異常
SpringBoot 統(tǒng)一異常處理
像這種統(tǒng)一異常的文章博客有許多,但是每個人使用都有自己的心得,我來總結一下自己使用的心得
統(tǒng)一異常,顧名思義,就是統(tǒng)一管理項目中會方法的異常,然后進行一個處理,Spring發(fā)生錯誤后,底層會去請求一個/error的地址,拋出對應的異常到頁面上,對客戶或者開發(fā)來說都不是特別的友好
使用統(tǒng)一異常處理的話,可以返回自定義的異常數(shù)據(jù),閱讀性提高,優(yōu)雅的處理異常
使用異常
使用異常的方式很簡單,Spring提供了兩個注解:@ControllerAdvice和@ExceptionHandler
@ControllerAdvice:控制器增強,使@ExceptionHandler、@InitBinder、@ModelAttribute注解的方法應用到所有的 @RequestMapping注解的方法@ExceptionHandler:異常處理器,此注解的作用是當出現(xiàn)其定義的異常時進行處理的方法
創(chuàng)建統(tǒng)一異常類
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.TypeMismatchException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.web.AbstractErrorController;
import org.springframework.boot.autoconfigure.web.ErrorAttributes;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.servlet.NoHandlerFoundException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;
import java.util.Map;
/**
* 全局異常處理
* 一般情況下,方法都有異常處理機制,但不能排除有個別異常沒有處理,導致返回到前臺,因此在這里做一個異常攔截,統(tǒng)一處理那些未被處理過的異常
*
* @author ${author} on ${date}
*/
@ControllerAdvice
@Controller
@RequestMapping
public class GlobalExceptionHandler extends AbstractErrorController {
private static final Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class);
public GlobalExceptionHandler(ErrorAttributes errorAttributes) {
super(errorAttributes);
}
@Value("${server.error.path:${error.path:/error}}")
private static String errorPath = "/error";
/**
* sql異常
*
* @param req
* @param rsp
* @param ex
* @return
* @throws Exception
*/
@ResponseBody
@ExceptionHandler(SQLException.class)
public Result<String> sqlException(HttpServletRequest req, HttpServletResponse rsp, Exception ex) {
LOGGER.error("!!! request uri:{} from {} server exception:{}", req.getRequestURI(), RequestUtil.getIpAddress(req), ex == null ? null : ex);
return ResponseMsgUtil.builderResponse(1002, ex == null ? null : ex.getMessage(), null);
}
/**
* 500錯誤.
*
* @param req
* @param rsp
* @param ex
* @return
* @throws Exception
*/
@ResponseBody
@ResponseStatus(code = HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(Exception.class)
public Result<String> serverError(HttpServletRequest req, HttpServletResponse rsp, Exception ex) throws Exception {
LOGGER.error("!!! request uri:{} from {} server exception:{}", req.getRequestURI(), RequestUtil.getIpAddress(req), ex == null ? null : ex);
return ResponseMsgUtil.builderResponse(1002, ex == null ? null : ex.getMessage(), null);
}
/**
* 404的攔截.
*
* @param request
* @param response
* @param ex
* @return
* @throws Exception
*/
@ResponseBody
@ResponseStatus(code = HttpStatus.NOT_FOUND)
@ExceptionHandler(NoHandlerFoundException.class)
public Result<String> notFound(HttpServletRequest request, HttpServletResponse response, Exception ex) throws Exception {
LOGGER.error("!!! request uri:{} from {} not found exception:{}", request.getRequestURI(), RequestUtil.getIpAddress(request), ex);
return ResponseMsgUtil.builderResponse(404, ex == null ? null : ex.getMessage(), null);
}
@ExceptionHandler(MissingServletRequestParameterException.class)
@ResponseBody
public Result<String> paramException(MissingServletRequestParameterException ex) {
LOGGER.error("缺少請求參數(shù):{}", ex.getMessage());
return ResponseMsgUtil.builderResponse(99999, "缺少參數(shù):" + ex.getParameterName(), null);
}
//參數(shù)類型不匹配
//getPropertyName()獲取數(shù)據(jù)類型不匹配參數(shù)名稱
//getRequiredType()實際要求客戶端傳遞的數(shù)據(jù)類型
@ExceptionHandler(TypeMismatchException.class)
@ResponseBody
public Result<String> requestTypeMismatch(TypeMismatchException ex) {
LOGGER.error("參數(shù)類型有誤:{}", ex.getMessage());
return ResponseMsgUtil.builderResponse(99999, "參數(shù)類型不匹配,參數(shù)" + ex.getPropertyName() + "類型應該為" + ex.getRequiredType(), null);
}
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
@ResponseBody
public Result<String> requestMethod(HttpRequestMethodNotSupportedException ex) {
LOGGER.error("請求方式有誤:{}", ex.getMethod());
return ResponseMsgUtil.builderResponse(99999, "請求方式有誤:" + ex.getMethod(), null);
}
@ExceptionHandler(MultipartException.class)
@ResponseBody
public Result<String> fileSizeLimit(MultipartException m) {
LOGGER.error("超過文件上傳大小限制");
return ResponseMsgUtil.builderResponse(99999, "超過文件大小限制,最大10MB", null);
}
/**
* 重寫/error請求, ${server.error.path:${error.path:/error}} IDEA報紅無需處理,作用是獲取spring底層錯誤攔截
*
* @param request
* @param response
* @return
* @throws Exception
*/
@ResponseBody
@RequestMapping(value = "${server.error.path:${error.path:/error}}")
public Result<String> handleErrors(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpStatus status = getStatus(request);
if (status == HttpStatus.NOT_FOUND) {
throw new NoHandlerFoundException(request.getMethod(), request.getRequestURL().toString(), new HttpHeaders());
}
Map<String, Object> body = getErrorAttributes(request, true);
return ResponseMsgUtil.builderResponse(Integer.parseInt(body.get("status").toString()), body.get("message").toString(), null);
}
@Override
public String getErrorPath() {
return errorPath;
}
}
從上面可以看出來,我定義了sql異常,500異常,404異常該做的事情,通過@ExceptionHandler注解來攔截程序中的異常,比如執(zhí)行SQL時,拋出了異常,就會被統(tǒng)一異常給攔截,然后返回我們想要返回的數(shù)據(jù)
@ResponseStatus注解可加可不加,就是對響應碼進行攔截,如代碼上,對404響應碼進行了攔截
最下面的handleErrors方法,是對Spring底層訪問/error的時候進行了一次攔截,獲取當前請求碼,如果是404.拋出404的異常
優(yōu)化處理異常怎么能沒有自定義返回數(shù)據(jù)呢
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
/**
* 返回數(shù)據(jù)結果集合
*/
public class Result<T> {
private Integer code;
private String resMsg;
private T data;
public Result() {
}
public Integer getCode() {
return this.code;
}
public void setCode(Integer resCode) {
this.code = resCode;
}
public String getResMsg() {
return this.resMsg;
}
public void setResMsg(String resMsg) {
this.resMsg = resMsg;
}
public T getData() {
return this.data;
}
public void setData(T data) {
this.data = data;
}
public String toJson() {
return this.data == null ? JSON.toJSONString(this) : this.toJson(SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteMapNullValue);
}
public String toJson(SerializerFeature... features) {
return features == null ? this.toJson() : JSON.toJSONString(this, features);
}
public String toString() {
return "Result{code=" + this.code + ", resMsg='" + this.resMsg + '\'' + ", data=" + this.data + '}';
}
}
/**
* 返回數(shù)據(jù)結果集合
* @author RuXuanWo on 2019/02/21
*/
public class ResponseMsgUtil {
public ResponseMsgUtil() {
}
public static <T> Result<T> builderResponse(int code, String msg, T data) {
Result<T> res = new Result();
res.setCode(code);
res.setResMsg(msg);
res.setData(data);
return res;
}
public static <T> Result<T> success(String msg) {
return builderResponse(0, msg, null);
}
public static <T> Result<T> success(String msg, T data) {
return builderResponse(0, msg, data);
}
public static <T> Result<T> success(T data) {
return builderResponse(0, "Success", data);
}
public static <T> Result<T> success() {
return builderResponse(0, "Success", null);
}
public static <T> Result<T> failure() {
return builderResponse(1, "Failure", null);
}
public static <T> Result<T> failure(String msg) {
return builderResponse(1, msg, null);
}
public static <T> Result<T> failure(T date) {
return builderResponse(-1, "Failure", date);
}
public static <T> Result<T> illegalRequest() {
return builderResponse(1008, "Illegal request", (T) null);
}
public static <T> Result<T> exception() {
return builderResponse(1002, "request exception", (T) null);
}
public static <T> Result<T> paramsEmpty() {
return builderResponse(1009, "the input parameter is null", (T) null);
}
}
測試
將這些準備都做好以后,項目跑起來,訪問一個接口,故意不傳某個必填項,就會被統(tǒng)一異常攔截,如下
{
?? ?code: 1002,
?? ?data: null,
?? ?msg: "Required String parameter 'id' is not present"
}以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Map按單個或多個Value排序當Value相同時按Key排序
Map可以先按照value進行排序,然后按照key進行排序。 或者先按照key進行排序,然后按照value進行排序,這樣操作都行,這篇文章主要介紹了Map按單個或多個Value排序,當Value相同時按Key排序,需要的朋友可以參考下2023-02-02
win10 java(jdk安裝)環(huán)境變量配置和相關問題
這篇文章主要介紹了win10java(jdk安裝)環(huán)境變量配置和相關問題解決,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-12-12
使用java基礎類實現(xiàn)zip壓縮和zip解壓工具類分享
使用java基礎類寫的一個簡單的zip壓縮解壓工具類,實現(xiàn)了指定目錄壓縮到和該目錄同名的zip文件和將zip文件解壓到指定的目錄的功能2014-03-03
最詳細的Java循環(huán)結構解析之for循環(huán)教程(適合小白)
:循環(huán)結構是指在程序中需要反復執(zhí)行某個功能而設置的一種程序結構,下面這篇文章主要給大家介紹了關于Java循環(huán)結構解析之for循環(huán)的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2021-09-09

