首页 > 其他分享 >spring mvc环境之异常错误码的统一返回(十五)

spring mvc环境之异常错误码的统一返回(十五)

时间:2022-12-14 17:23:50浏览次数:55  
标签:return String spring request 错误码 json mvc import ModelAndView

1.根据不同的请求方式,返回页面或json数据

  1).创建统一权限异常处理类,所有的权限异常走一个端口

  2).根据请求方式不同返回不同数据,页面请求返回403未授权页面,ajax请求返回json数据

 

package com.cc8w.exception;


import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
public class MyExceptionAdvice {

    @ExceptionHandler(Exception.class)
    public ModelAndView showInfoE(Exception e, HttpServletRequest request){
        if (isAjaxRequest(request))
        {
            ModelAndView modelAndView=new ModelAndView(new MappingJackson2JsonView());
            modelAndView.addObject(AjaxResult.authError("错误1"));
            return modelAndView;
            //return new ModelAndView(AjaxResult.authError("您没有权限查看该数据").toString());
        }
        else {
            return new ModelAndView("error1");
        }

    }

    /**
     * 是否是Ajax异步请求
     * @param request
     */
    public static boolean isAjaxRequest(HttpServletRequest request) {
        String accept = request.getHeader("accept");
        if (accept != null && accept.indexOf("application/json") != -1) {
            return true;
        }

        String xRequestedWith = request.getHeader("X-Requested-With");
        if (xRequestedWith != null && xRequestedWith.indexOf("XMLHttpRequest") != -1) {
            return true;
        }

        String uri = request.getRequestURI();
        if (isContainStrs(uri, ".json", ".xml")) {
            return true;
        }

        String ajax = request.getParameter("__ajax");
        if (isContainStrs(ajax, "json", "xml")) {
            return true;
        }
        return false;
    }

    public static boolean isContainStrs(String str, String... strs) {
        if (str != null && strs != null) {
            for (String s : strs) {
                if (str.equalsIgnoreCase(s.trim())) {
                    return true;
                }
            }
        }
        return false;
    }

}

其中:AjaxResult.authError是系统自定义的json通用返回对象,包含code、msg、data等通用字段自己可以自定义设置。

主要涉及到ModelAndView、MappingJackson2JsonView  通过传入的类型来控制是页面ORjson字符串

 

2.返回统一的数据结果

 https://blog.csdn.net/zhangzengxiu/article/details/121622103

 

 

 

 

 

 

转:

https://blog.csdn.net/butioy_org/article/details/78718405

https://blog.csdn.net/s1040342522/article/details/123545878

https://www.cnblogs.com/chenzhubing/p/11438902.html

https://blog.csdn.net/weixin_56644618/article/details/127580941

 

标签:return,String,spring,request,错误码,json,mvc,import,ModelAndView
From: https://www.cnblogs.com/fps2tao/p/16982700.html

相关文章

  • spring学习-1.使用Maven构建spring项目
    1.新建一个Maven项目​​​​​​项目的结构图​​​​2.配置pom.xml,引入项目需要的依赖,这里引入了spring,junit<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns......
  • Maven构建spring整合mybatis的项目
    1.使用Maven构建java项目,修改pom.xml文件,添加所需的依赖jar包<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:......
  • Spring Cloud Config介绍
    市场上的开源的配置中心有很多,如奇虎360的QConf、淘宝的Diamond、百度的Disconf、携程的Apollo都可解决上述提到的问题,同样地SpringCloud提供的配置中心则是SpringCloud......
  • 基于Springboot+Mybatis+mysql+vue宠物领养网站1
    @目录一、系统介绍二、功能展示1.主页(普通用户)2.登陆、注册(普通用户)3.宠物大全(普通用户)4.宠物详情(申请领养、点赞、评论)(普通用户)5.我的申请(普通用户)6.个人信息(普通用户......
  • 基于jsp+sevlet+spring+mysql实现校园疫情防控管理信息系统
    @目录一、系统介绍二、采用技术及开发环境1.开发技术2.开发环境二、功能展示1.学校疫情统计2.各情况统计3.各学院学生情况统计4.各学院教师统计5.最新时报6.后台管理四、代......
  • SpringMVC中HelloWorld实现(三)
    我机器的开发环境为:开发工具:EclipseForJavaEE;数据库:MySql5.5.35;运行环境:TomCatV7.0;JDK:JDK1.7.0_45;项目工程为:DynamicWebProject; 一、项目依赖的jar包:[ht......
  • SpringBoot入门系列:Spring Security 和 Angular JS(1)
    SpringSecurity和AngularJS系列,是一文的解析。受保护的单页面应用程序这一节我们要展示将SpringSecurity、SpringBoot和AngularJS放在一起使用的一些不错的特性,它......
  • SpringBoot入门系列:Spring Security 和 Angular JS(2)
    添加动态的内容目前我们已经有了一个应用程序,硬编码了一句问候语在里面。这对学习如何把这些凑到一起很有帮助,不过实际上我们期望的是来自于后台服务器的内容,因此我们可以创......
  • SpringBoot入门系列: JPA mysql(五)
    一,准备工作,建立spring-boot-sample-mysql工程1、http://start.spring.io/    A、Artifact中输入spring-boot-sample-mysql    B、勾选Web下的web    C、勾选......
  • SpringBoot入门:Hello World(一)
       跟随SpringBoot的文档(http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-documentation)学习,前后几章关联才调通代码,煞是痛苦,在......