(办公)springmvc->controller的统一异常层,返回json

时间:2023-03-08 21:53:31
(办公)springmvc->controller的统一异常层,返回json

controller里面写的代码,很多时候,没有写try{}catch(Exceiption ex){},结果就是系统出错,就算是接口,参数正确也会返回404,这个是不应该的.

下面是代码,以后参考

package com.lianrong.manager.controller.;
import org.apache.zookeeper.proto.ErrorResponse;
import org.springframework.beans.ConversionNotSupportedException;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.EnableWebMvc; import javax.servlet.http.HttpServletRequest;
import java.io.IOException; @ControllerAdvice
@EnableWebMvc
public class RestExceptionHandler { @ExceptionHandler(Exception.class)
@ResponseBody
public String handleException(){
return "Exception Deal!";
}
//运行时异常
@ExceptionHandler(RuntimeException.class)
@ResponseBody
public ServiceResult runtimeExceptionHandler(RuntimeException runtimeException) {
return new ServiceResult(false,"","1000运行时异常");
} //空指针异常
@ExceptionHandler(NullPointerException.class)
@ResponseBody
public ServiceResult nullPointerExceptionHandler(NullPointerException ex) {
ex.printStackTrace();
return new ServiceResult(false,"","1001空指针异常");
}
//类型转换异常
@ExceptionHandler(ClassCastException.class)
@ResponseBody
public ServiceResult classCastExceptionHandler(ClassCastException ex) {
ex.printStackTrace();
return new ServiceResult(false,"","1002类型转换异常");
} //IO异常
@ExceptionHandler(IOException.class)
@ResponseBody
public ServiceResult iOExceptionHandler(IOException ex) {
ex.printStackTrace();
return new ServiceResult(false,"","1003IO异常");
}
//未知方法异常
@ExceptionHandler(NoSuchMethodException.class)
@ResponseBody
public ServiceResult noSuchMethodExceptionHandler(NoSuchMethodException ex) {
ex.printStackTrace();
return new ServiceResult(false,"","1004未知方法异常");
} //数组越界异常
@ExceptionHandler(IndexOutOfBoundsException.class)
@ResponseBody
public ServiceResult indexOutOfBoundsExceptionHandler(IndexOutOfBoundsException ex) {
ex.printStackTrace();
return new ServiceResult(false,"","1005数组越界异常");
}
//400错误
@ExceptionHandler({HttpMessageNotReadableException.class})
@ResponseBody
public ServiceResult requestNotReadable(HttpMessageNotReadableException ex){
System.out.println("400..requestNotReadable");
ex.printStackTrace();
return new ServiceResult(false,"","400..requestNotReadable");
}
//400错误
@ExceptionHandler({TypeMismatchException.class})
@ResponseBody
public ServiceResult requestTypeMismatch(TypeMismatchException ex){
System.out.println("400..TypeMismatchException");
ex.printStackTrace();
return new ServiceResult(false,"","400..TypeMismatchException");
}
//400错误
@ExceptionHandler({MissingServletRequestParameterException.class})
@ResponseBody
public ServiceResult requestMissingServletRequest(MissingServletRequestParameterException ex){
System.out.println("400..MissingServletRequest");
ex.printStackTrace();
return new ServiceResult(false,"","400..MissingServletRequest");
}
//405错误
@ExceptionHandler({HttpRequestMethodNotSupportedException.class})
@ResponseBody
public ServiceResult request405(){
System.out.println("405...");
return new ServiceResult(false,"","");
}
//406错误
@ExceptionHandler({HttpMediaTypeNotAcceptableException.class})
@ResponseBody
public ServiceResult request406(){
System.out.println("404...");
return new ServiceResult(false,"","");
}
//500错误
@ExceptionHandler({ConversionNotSupportedException.class, HttpMessageNotWritableException.class})
@ResponseBody
public ServiceResult server500(RuntimeException runtimeException){
System.out.println("500...");
return new ServiceResult(false,"","");
} }

    注意事项,先干掉默认的SimpleMappingExceptionResolver

springmvc 默认的异常,dispatcher-servlet.xml
<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="defaultErrorView" value="error"/>
<property name="defaultStatusCode" value=""/>
<property name="statusCodes">
<props>
<prop key=""></prop>
<prop key="error"></prop>
</props>
</property>
<property name="exceptionMappings">
<props>
<prop key="java.sql.SQLException">error</prop>
<prop key="org.springframework.web.bind.ServletRequestBindingException">error</prop>
<prop key="java.lang.IllegalArgumentException">error</prop>
<prop key="org.springframework.validation.BindException">error</prop>
<prop key="java.lang.ClassNotFoundException">error</prop>
</props>
</property>
<property name="warnLogCategory" value="com.lianrong.manager">
</property>
</bean>