统一功能(2异常)

 @RequestMapping("/r1")
    public String returnString(){
        return "asdfghjkl";
    }

如果在RequestMapping中返回的类型是String但是统一返回结果是Result类就会报错。

 原因:源码中需要Result类型但是传入的是String类型 


统一异常处理:

比如在写入图书的时候会抛出异常,还有很多其他情况会抛出 异常,把异常抛给上方(调用方),不希望没有捕获异常,导致外部知道内部发生了什么异常。不让客户端感知。 不修改代码对异常进行捕获。

package com.syx.book.config;

import com.syx.book.Model.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@ResponseBody
public class ExceptionAdvice {
    @ExceptionHandler
    public Result handlerException(Exception e){
        return Result.fail("内部错误请联系管理员");
        
    }
}

我们通过@ControllerAdvice @ResponseBody @ExceptionHandler

这几个注解来进行异常统一返回,不把错误信息暴露给前端。

我们返回的是一个数据,如果不加@ResponseBody的话就会去寻找默认的视图,没找到就会报错。

package com.syx.book.Controller;

import com.syx.book.Model.Result;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("test")
public class TestController {
    @RequestMapping("t1")
    public Result datareturn(){
        Integer a=10/0;
        String s="asdfghjkl";
        return Result.success(s);
    }

}
package com.syx.book.config;

import com.syx.book.Model.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
@Slf4j
@ControllerAdvice
@ResponseBody
public class ExceptionAdvice {
    @ExceptionHandler
    public Result handlerException(Exception e){
        log.error("发生异常"+e);
        return Result.fail("内部错误请联系管理员");

    }
}

 在这里如果出现异常就会统一返回内部错误信息,并且在后端打日志。由于我们在这里发生异常没有错误信息,我们可以通过打日志来观察错误信息。

异常优先子类捕获,子类没有捕获父类捕获。

有多个捕获方法应该使用哪一个?

package com.syx.book.config;

import com.syx.book.Model.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
@Slf4j
@ControllerAdvice
@ResponseBody
public class ExceptionAdvice {
    @ExceptionHandler
    public Result handlerException(Exception e){
        log.error("发生异常"+e);
        return Result.fail("内部错误请联系管理员");

    }

    @ExceptionHandler
    public Result handlerException(NullPointerException e){
        log.error("异常打印是+"+e);
        return Result.fail("内部错误请联系管理员");

        
    }
}

在这里我们会通过异常的深度来匹配异常。 


在统一返回和统一异常都加上了@ControllerAdvice:当发生什么事情,通知做下面controllerAdvice的逻辑。

在这里@ControllerAdvice里面包含了@Component,是一个复合注解。


如何修改返回内容为字符串?:

 

将produces修改为application/json,返回的格式会发生变化,会自动转化成json格式。(后端统一返回会出现问题,由于后端对字符串的处理),返回json情况下前端可以直接通过点xxx的形式获取比如data.records。

(用到统一返回格式最好要加上application/json,不然无法拿到数据)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值