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>

(办公)springmvc->controller的统一异常层,返回json的更多相关文章

  1. springmvc全局异常后返回JSON异常数据

    转自:http://www.cnblogs.com/exmyth/p/5601288.html (1)自定义或者使用spring自带的各种异常处理器 例如spring基于注解的异常解析器Annotat ...

  2. Spring MVC全局异常后返回JSON异常数据

    问题: 当前项目是作为手机APP后台支持,使用spring mvc + mybaits + shiro进行开发.后台服务与手机端交互是发送JSON数据.如果后台发生异常,会直接返回异常页面,显示异常内 ...

  3. SpringMVC 避免IE执行AJAX时,返回JSON出现下载文件

    <?xml version="1.0" encoding="UTF-8"?> <!-- SpringMVC配置文件 --> <be ...

  4. 关于Controller层返回JSON字符串

    /** * 导入jackson包. * @param pn * @return */ @RequestMapping("/emps") @ResponseBody public M ...

  5. SpringMVC 配置.html拦截时,返回JSON数据时出现406错误解决方案

    [说明]在SpringMVC框架的使用中常常会使用@ResponseBody注解,修饰"处理器"(Controller的方法),这样在处理器在返回完毕后,就不走逻辑视图,而是将返回 ...

  6. SpringMVC避免IE执行AJAX,返回JSON出现下载文件

  7. spring入门(七)【springMVC返回json串】

    现在多数的应用为了提高交互性多使用异步刷新,即在不刷新整个页面的情况下,只刷新局部,局部刷新用得最多就是ajax,ajax和后台进行交互的数据格式使用的最多的是JSON,这里简单描述,在springm ...

  8. SpringMVC返回JSON方案

    SpringMVC已经大行其道.一般的,都是返回JSP视图.如果需要返回JSON格式,我们大都掌握了一些方法. 在ContentNegotiatingViewResolver之前,一般使用XmlVie ...

  9. springmvc返回json字符串中文乱码问题

    问题: 后台代码如下: @RequestMapping("menuTreeAjax") @ResponseBody /** * 根据parentMenuId获取菜单的树结构 * @ ...

随机推荐

  1. 直观理解神经网络最后一层全连接+Softmax

    目录 写在前面 全连接层与Softmax回顾 加权角度 模板匹配 几何角度 Softmax的作用 总结 参考 博客:blog.shinelee.me | 博客园 | CSDN 写在前面 这篇文章将从3 ...

  2. vue安装jquery和配置(不需要在页面引入直接可以使用)

    首先在命令行工具上输入 npm install jquery --save-dev 安装完成之后在build文件夹下的webpack.base.conf.js进行配置,在顶部添加:const webp ...

  3. openlayers4 入门开发系列之地图空间查询篇(附源码下载)

    前言 openlayers4 官网的 api 文档介绍地址 openlayers4 api,里面详细的介绍 openlayers4 各个类的介绍,还有就是在线例子:openlayers4 官网在线例子 ...

  4. emacs 高亮

    用途:让某个单词高亮显示 1,安装 m-x 回车,输入list-packages 在列表中找到highlight-symbol后,鼠标点击它,再点击安装 2,在.emacs中配置 ;;高亮 (requ ...

  5. C++基础——类继承中方法重载

    一.前言 在上一篇C++基础博文中讨论了C++最基本的代码重用特性——类继承,派生类可以在继承基类元素的同时,添加新的成员和方法.但是没有考虑一种情况:派生类继承下来的方法的实现细节并不一定适合派生类 ...

  6. vue 中 vue-router、transition、keep-alive 怎么结合使用?

    <transition :name="name" mode="out-in" name="fade"> <keep-ali ...

  7. http-server安装及运行

    vue项目打包后会生成一个dist目录,我们想要直接运行dist目录...除了复制静态文件到服务器nginx目录下,然后启动nginx来启动项目, 另外一个办法就是安装http-server 直接启动 ...

  8. Unsupported major.minor version 52.0解决办法

    一.错误现象:当改变了jdk版本时,在编译java时,会遇到Unsupported major.minor version错误.jdk版本和stanford parser对应关系 JDK版本和Java ...

  9. 有趣的 box-decoration-break

    这两天接触到一个很有意思的 CSS 属性 -- box-decoration-break.下面就一起去一探究竟. 因为 MDN 上关于这个属性,没有中文文档,所以一直在想一个合理贴切的中文翻译.直译一 ...

  10. 超级简单!把文档转换成网页格式(Core)

    因为需要把好多的文档放在服务器上访问,最开始是使用第3方网站www.aconvert.com,这个其实也挺方便的, 最后由于一些需求原因,最终只有依靠代码来进行,以下是简化后的代码 创建一个控制台应用 ...