ajax返回类型构造,@RestControllerAdvice构造统一返回值格式和统一异常处理

利用注解@RestControllerAdvice拦截所有的ajax请求,构造统一格式的返回值

/**

* @desc 统一的响应结构

**/

@Data

@NoArgsConstructor

@AllArgsConstructor

public class CommonResponse implements Serializable {

private Integer code;

private String message;

private T data;

public CommonResponse(Integer code, String message) {

this.code = code;

this.message = message;

}

}

配置拦截

/**

* @desc 对响应的统一拦截,把所有响应弄成同一个格式

**/

@RestControllerAdvice

public class CommonResponseDataAdvice implements ResponseBodyAdvice {

@Override

public boolean supports(MethodParameter methodParameter, Class aClass) {

//通过方法参数得到类名, 然后得到类上的annotation,

// 如果被IgnoreResponseAdvice标识就不拦截

if (methodParameter.getDeclaringClass().isAnnotationPresent(

IgnoreResponseAdvice.class

)){

return false;

}

//方法上被标注,也不拦截

if (methodParameter.getMethod().isAnnotationPresent(

IgnoreResponseAdvice.class

)){

return false;

}

return true;

}

//在写入响应之前,所以在这里构造统一格式

@Override

@Nullable

public Object beforeBodyWrite(@Nullable Object o, MethodParameter methodParameter, MediaType mediaType, Class aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {

CommonResponse response = new CommonResponse<>(0, "");

if (null == o) {

return response;

} else if (o instanceof CommonResponse) {

response = (CommonResponse) o;

} else {

response.setData(o);

}

return response;

}

}

有时候有些简单的注解,不需要统一的格式,就使用这个自定义的注解

@Target({ElementType.TYPE,ElementType.METHOD})

@Retention(RetentionPolicy.RUNTIME)

public @interface IgnoreResponseAdvice {

}

另外,同一个原理,可以进行统一异常处理

/**

* @desc 统一的异常处理 可以自己多定义几个异常类,细化,然后多建立几个方法使用@ExceptionHandler注解,处理特定的异常

**/

@RestControllerAdvice

public class GlobalExceptionAdvice {

@ExceptionHandler(value = GlobeException.class)

public CommonResponse handlerGlobeException(HttpServletRequest request, GlobeException exception){

CommonResponse response=new CommonResponse<>(-1,"");

response.setData(exception.getMessage());

return response;

}

}

public class GlobeException extends Exception {

public GlobeException(String message) {

super(message);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
@Controller是Spring MVC中的注解,用于标注一个类是Controller,其作用是接收请求并处理业务逻辑,通常用于接收用户的请求并调用Service层来处理业务逻辑。@Controller注解会自动将该类注册为Spring容器中的Bean,可以使用@Autowired或@Resource等注解来注入其他Bean。 @Controller中定义的处理请求的方法(如@RequestMapping注解标识的方法)的返回值通常有以下几种类型: 1. ModelAndView对象:ModelAndView是Spring MVC中最常用的返回类型,其作用是封装要返回的数据和视图名称,可以通过addObject()方法将数据添加到ModelAndView中,也可以通过.setViewName()方法设置视图名称。 2. String类型:String类型返回值通常表示返回的视图名称,Spring MVC会自动根据视图名称找到对应的视图。 3. void类型:void类型返回值表示不需要返回任何数据,也不需要跳转到任何视图,通常用于处理Ajax请求等。 4. 其他类型:除了上述三种类型外,还可以返回其他类型的数据,例如JSON格式的数据,Spring MVC会自动将其转换成JSON格式的响应返回给客户端。 值得注意的是,对于返回String类型或void类型的方法,如果没有使用@ResponseBody注解,Spring MVC会自动将其视为返回视图名称,即调用ViewResolver来解析视图。如果使用@ResponseBody注解,则Spring MVC会自动将返回值转换成JSON格式的响应,这适用于处理Ajax请求等场景。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值