springboot自定义参数解析器-实现分页请求数据封装

硅谷探秘者 2321 0 0

        很多项目在开发的时候都会遇到分页的问题,一般分页主要用到两个参数page和limit,page是第几页,limit是请求多少条数据。而一般在请求数据库的时候我们用不到page参数,用到的是用page和limit计算出来的index参数。index的计算方法为 index=(page-1)*limit;。如果在搭建的框架没有任何措施的时候,那么page和limit参数需要我们手动写代码去判断其是否合法(page和limit必须大于0),并且index需要我们自己去计算。这时我们就可以自定义一个参数解析器,在请求接口之前让框架帮我们完成判断和计算的任务,我们只需要写一个注解即可。

自定义分页注解

这个注解用在controller的page参数上,下方会有示例。

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
 * 	自定义分页注解
 * @author Jiajiajia
 */
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Page {
}
自定义参数解析器

如何封装请求参数,都可以这个解析器中按照我们的意愿完成,前提是controller的参数上必须携带Page注解。

import com.dzqc.dormitorycommon.entity.PageParam;
import javax.servlet.http.HttpServletRequest;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
/**
 * 	自定义分页参数解析器
 * @author Jiajiajia
 */
public class RequestPageHandlerMethodArgumentResolver  implements HandlerMethodArgumentResolver {
    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        //绑定注解标签
        return  methodParameter.hasParameterAnnotation(Page.class);
    }
    @Override
    public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
    	HttpServletRequest request = nativeWebRequest.getNativeRequest(HttpServletRequest.class);
    	String page=request.getParameter("page");
    	if(page==null) 
    		throw new PageException("missing paging parameters page");
    	String limit=request.getParameter("limit");
    	if(limit==null) 
    		throw new PageException("missing paging parameters limit");
    	Integer p = null;
		Integer l = null;
		try {
			p = Integer.valueOf(page);
			if(p<=0) 
				throw new PageException("page 参数必须大于0");
			l = Integer.valueOf(limit);
			if(l<=0) 
				throw new PageException("limit 参数必须大于0");
		} catch (Exception e) {
			throw new PageException("分页参数错误");
		}
        return new PageParam(p,l);
    }
}
封装分页参数PageParam类

/**
 * 	参数接受
 * @author 硅谷探秘者(jia)
 */
public class PageParam {
	private Integer page;
	private Integer limit;
	private Integer index;
	public PageParam(int page,int limit) {
		this.page=page;
		this.limit=limit;
		this.index=(page-1)*limit;
	}
    //get set方法省略
}
自定义异常类
/**
 * 	自定义分页异常
 * @author Jiajiajia
 */
public class PageException extends Exception{
	private static final long serialVersionUID = 1L;
	public PageException(String msg) {
		super(msg);
	}
}
最后完成配置自定义参数解析器
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.List;

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(new RequestPageHandlerMethodArgumentResolver());
    }
}
测试

上述配置完成以后 写一个测试的方法:注意参数加了@Page注解

	@RequestMapping("test")
	@ResponseBody
	public Map<String,Object> test(@Page PageParam page) {
		
		System.out.println(page);//打印分装好的分页请求数据
		
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("are you ok?","ok!");
		return map;
	}

        正常请求时,如:.../test?page=2&limit=10 打印如下:PageParam [page=2, limit=10, index=10]并且正常返回数据,当缺失某个参数的时候如:.../test?page=2,就会报如下错误:

        这样返回错误信息似乎不太雅观。接下来就写一个配置,如果出现PageException异常的时候,将其拦截,并且返回json格式的提示,配置如下。

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
/**
 * 	自定义异常捕获
 * @author Jiajiajia
 */
@RestControllerAdvice
public class PageExceptionHandler {
    
    @ExceptionHandler(value=PageException.class)
    public Object handleMyException(PageException e, HttpServletRequest request) {
    	Map<String,Object> map=new HashMap<String, Object>();
    	map.put("code",500);
    	map.put("msg",e.getMessage());
        return map;
    }
}

这样再报错的时候就会返回json数据:

        经过上述配置以后,再写分页接口的时候,只需要在controller中加 @Page PageParam page 即可,参数的判断和计算都由框架完成。


评论区
请写下您的评论...
暂无评论...
猜你喜欢
框架 1729 呢?首先我们有一个基础的配置类BasePage,这个类里面只有page,limit,index这三个属性,我们在创建其他查询类的时候要继承BasePage这个类。然后用我们
框架 3892 springbootjson不返回对象的指字段在体类的字段上加上注importcom.fasterxml.jackson.annotation.JsonIgnore;例
算法基础,linux 4321 问题描述springboot项目在跨域名调用接口,并且需要传头时报错:AccesstoXMLHttpRequestat'http://ydatestapi.libawall.com
java项目 1394 springboot+mybatis配置多源并利用aop动切换(demo)
框架 1644 springboot+mybatis配置多源并利用aop动切换1.项目大致结构2.pom依赖dependencygroupIdorg.springframework.boot
official 1101 编码在网络应用中需要某种编,将原始字节的消息对象进行互相转换。网络中都是以字节码的形式来传输的,服务编码后发送到客户端,客户端需要对进行
框架 3078 例:java类如下:publicclassQuestionnaireSubject{ privateIntegerid; privateStringname; publicIntegergetId(){ returnid; } publicvoidsetId(Integerid){ this.id=id; } publicStringgetName(){ returnname; } publ
nginx,前端,java基础 1303   基于javanio+netty+websocket+protobuf+javascript等技术前后端高性能传输的demo模型。  github地址:https
归档
2018-11  12 2018-12  33 2019-01  28 2019-02  28 2019-03  32 2019-04  27 2019-05  33 2019-06  6 2019-07  12 2019-08  12 2019-09  21 2019-10  8 2019-11  15 2019-12  25 2020-01  9 2020-02  5 2020-03  16 2020-04  4 2020-06  1 2020-07  7 2020-08  13 2020-09  9 2020-10  5 2020-12  3 2021-01  1 2021-02  5 2021-03  7 2021-04  4 2021-05  4 2021-06  1 2021-07  7 2021-08  2 2021-09  8 2021-10  9 2021-11  16 2021-12  14 2022-01  7 2022-05  1 2022-08  3 2022-09  2 2022-10  2 2022-12  5 2023-01  3 2023-02  1 2023-03  4 2023-04  2 2023-06  3 2023-07  4 2023-08  1 2023-10  1 2024-02  1 2024-03  1 2024-04  1
标签
算法基础 linux 前端 c++ 数据结构 框架 数据库 计算机基础 储备知识 java基础 ASM 其他 深入理解java虚拟机 nginx git 消息中间件 搜索 maven redis docker dubbo vue 导入导出 软件使用 idea插件 协议 无聊的知识 jenkins springboot mqtt协议 keepalived minio mysql ensp 网络基础 xxl-job rabbitmq haproxy srs 音视频 webrtc javascript
目录
没有一个冬天不可逾越,没有一个春天不会来临。最慢的步伐不是跬步,而是徘徊,最快的脚步不是冲刺,而是坚持。