Dubbo框架使用 RPC远程调用,使用分页插件PageHelper报java.lang.NullPointerException空指针异常

dao层接口中方法,使用了mybatis插件

  /**
     * 分页查询
     * @return
     */
    Page<CheckItem> selectByPage(@Param("queryString") String queryString);

dao层sql语句的xml配置文件,使用了mybatis插件与分页插件PageHelper
sql语句动态拼接

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.it.health.dao.CheckItemDao">
    <select id="selectByPage" resultType="com.it.health.pojo.CheckItem">
        SELECT * FROM t_checkitem
        <if test="queryString !=null and queryString.length>0">
            WHERE  code LIKE "%"#{queryString}"%" OR name LIKE  "%"#{queryString}"%";
        </if>
    </select>
</mapper>

service层impl实现类中的代码

   public PageResult findPage(QueryPageBean queryPageBean) {
   		//使用了lombok插件
        log.debug("CheckItemServiceImpl queryPageBean:{}",queryPageBean);
		// 使用分页插件PageHelper
      PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //获取分页数据
        Page<CheckItem> page = checkItemDao.selectByPage(queryPageBean.getQueryString());
        // 封装分页数据,返回分页对象
        return new PageResult(page.getTotal(),page.getResult());
    }

controller控制中心层代码

   @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            log.debug("checkItemService findPage:{}", queryPageBean);
            PageResult pageResult = checkItemService.findPage(queryPageBean);
            return pageResult;
        } catch (Exception e) {
            e.printStackTrace();
            //出现异常时的返回结果
            return new PageResult(0L, new ArrayList());
        }
    }

查询条件实体类QueryPageBean ,仅展示成员变量

public class QueryPageBean implements Serializable {
	private Integer currentPage;//页码
	private Integer pageSize;//每页记录数
	private String queryString;//查询条件
}

分页结果实体类PageResult ,仅展示成员变量

public class PageResult implements Serializable {
	private Long total;//总记录数
	private List rows;//当前页结果
}

错误信息

java.lang.NullPointerException
	at com.it.health.service.impl.CheckItemServiceImpl.findPage(CheckItemServiceImpl.java:55)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:206)
	at com.sun.proxy.$Proxy43.findPage(Unknown Source)
	at com.alibaba.dubbo.common.bytecode.Wrapper0.invokeMethod(Wrapper0.java)
	at com.alibaba.dubbo.rpc.proxy.javassist.JavassistProxyFactory$1.doInvoke(JavassistProxyFactory.java:47)
	at com.alibaba.dubbo.rpc.proxy.AbstractProxyInvoker.invoke(AbstractProxyInvoker.java:76)
	at com.alibaba.dubbo.config.invoker.DelegateProviderMetaDataInvoker.invoke(DelegateProviderMetaDataInvoker.java:52)
	at com.alibaba.dubbo.rpc.protocol.InvokerWrapper.invoke(InvokerWrapper.java:56)
	at com.alibaba.dubbo.rpc.filter.ExceptionFilter.invoke(ExceptionFilter.java:62)
	at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:72)
	at com.alibaba.dubbo.monitor.support.MonitorFilter.invoke(MonitorFilter.java:75)
	at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:72)
	at com.alibaba.dubbo.rpc.filter.TimeoutFilter.invoke(TimeoutFilter.java:42)
	at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:72)
	at com.alibaba.dubbo.rpc.protocol.dubbo.filter.TraceFilter.invoke(TraceFilter.java:78)
	at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:72)
	at com.alibaba.dubbo.rpc.filter.ContextFilter.invoke(ContextFilter.java:72)
	at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:72)
	at com.alibaba.dubbo.rpc.filter.GenericFilter.invoke(GenericFilter.java:131)
	at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:72)
	at com.alibaba.dubbo.rpc.filter.ClassLoaderFilter.invoke(ClassLoaderFilter.java:38)
	at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:72)
	at com.alibaba.dubbo.rpc.filter.EchoFilter.invoke(EchoFilter.java:38)
	at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:72)
	at com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol$1.reply(DubboProtocol.java:103)
	at com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.handleRequest(HeaderExchangeHandler.java:96)
	at com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.received(HeaderExchangeHandler.java:172)
	at com.alibaba.dubbo.remoting.transport.DecodeHandler.received(DecodeHandler.java:51)
	at com.alibaba.dubbo.remoting.transport.dispatcher.ChannelEventRunnable.run(ChannelEventRunnable.java:80)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)

pom.xml

 <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper</artifactId>
                <version>5.1.2</version>
            </dependency>

mybatis配置文件

  <properties>
        <!--指明数据数据库官方语言-->
        <property name="dialect" value="mysql"/>
    </properties>

最后检查发现,后端代码没有任何问题,最后发现前端代码发送异步请求时,参数列表封装错了,后端参数传递的是QueryPageBean,封装的是currentPage页码;pageSize每页记录数;queryString查询条件,而自己缺误将QueryPageBean,没有的属性total总记录数封装了进去

   // 封装请求参数,当前页、默认每页记录数、查询条件
                var params = {
                    currentPage: this.pagination.currentPage,
                     pageSize: this.pagination.pageSize,
                    //total: this.pagination.total,这里封装的数据不对,看后端参数 public PageResult findPage(@RequestBody QueryPageBean queryPageBean)
					//所以参数应该封装QueryPageBean里的数据,total不是
                    queryString: this.pagination.queryString,
                }
                axios.post(this.backend_url + "/checkItem/findPage.do", params).then((response) => {
 //把pojo的数据集和总记录数赋值给模型
                    this.pagination.total = response.data.total;
                    this.dataList = response.data.rows;
                });

总结:1.前端发送异步请求时,参数封装有误,导致空指针异常,因为多封装的参数total根本就没有
2.dubbo架构service层引用的@Service注解应该是
import com.alibaba.dubbo.config.annotation.
Service;是dubbo的,而不是spring框架的

你可能感兴趣的:(java,dubbo,rpc远程调用)