同一个Controller里的同一个Service实例,在当前的Controller里的不同方法中状态不一致

直接上代码如下:

@Controller
@RequestMapping("/views/information")
public class PubContentController extends BaseController{

  @Autowired
      private ContentCategoryService contentCategoryService;

  /**
     * 新增资讯
     *
     * @param pubContent
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/pubContent_add", method = RequestMethod.POST)
    @AuthAnnotation(authStr = "func_news_add")
    @LogAnnotation(module = "资讯管理", act = "新增")
    @ResponseBody
    public Object add(@ModelAttribute("pubContent") PubContent pubContent, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        String currUserId = currUserId(request);
        String title = pubContent.getTitle().trim();
    
        if (StringHelper.isEmpty(title, true))
            throw new ParameterException(MsgConstant.EXCEPTION_NAME_NULL);
        
        //根据分类ID获取资讯分类,再判断是一级或二级分类
        String categoryId = pubContent.getSubid();
        Map<String,Object> params = Maps.newConcurrentMap();
        if (!StringHelper.isEmpty(categoryId, true)){
            params.put("id", categoryId);
        }else{
            throw new ParameterException(MsgConstant.EXCEPTION_CATEGORYID_NULL);
        }    
        ContentCategory category = contentCategoryService.get("get", params);
        
        String id = category.getId();
        //父id
        String pid = category.getPid();
        
        if(StringHelper.isEmpty(pid, true) || pid.equals("0") ){
            //没有父id,是一级分类
            //设置一级分类id
            pubContent.setCid(id);
            //设置二级分类id
            pubContent.setSubid("");
        }else{
            //有父id,是二级分类
            //设置一级分类id
            pubContent.setCid(pid);
            //设置二级分类id
            pubContent.setSubid(id);
        }

pubContent.setCreateBy(currUserId);
        pubContent.setUpdateBy(currUserId);
        int addid= pubContentService.insert(pubContent);

if (addid <= 0) {
            return new Result(CodeConstant.RETCODE_500, null, null, MsgConstant.ERROR);
        }
        //更新新上传的附件
        uploadPubAttachment(request, response, pubContent.getId()+"");
        CheckLog checkLog=new CheckLog();
        checkLog.setPubid(addid);
        checkLog.setCreateBy(currUserId);
        checkLog.setNote("");
        checkLog.setStatus(0);
        checkLog.setUserName(currUser(request).getUserName());
        checkLogService.insert(checkLog);
        return new Result(CodeConstant.RETCODE_200, null, null, MsgConstant.SUCCESS);
    }

  /**
     * 获取
     *
     * @param id
     * @return category
     */
    @RequestMapping(value = "/get_Category", method = RequestMethod.POST)
    @ResponseBody
    private ContentCategory get_Category(@RequestParam(value = "id", required = true) String id,
            HttpServletRequest request,
            HttpServletResponse response, Model model)throws Exception {
        Map<String,Object> params = Maps.newConcurrentMap();
        if (!StringHelper.isEmpty(id, true)){
            params.put("id", id);
        }    
        ContentCategory category = contentCategoryService.get("get", params);
        return category;
    }

}

带下划线的service是同一个service,绿色代码处的是没有问题,在红色代码处出现java.lang.NullPointerException异常问题,

后面查看对象,id是有值的, params的Map也是有值的,最后发现 contentCategoryService 对象是空的,找了好久都没有发现

是什么问题导致了contentCategoryService 是空的,而调用另一个方法的contentCategoryService 是有实体对象的没有任何问题,

最后请教大神指点,经过多次的测试发现原来是 访问权限的问题,后一个contentCategoryService 为空的方法是访问权限修饰符是private,

不为空的方法是访问权限修饰符是public,所以导致了contentCategoryService为空,一直空指针异常。(自己排除错误的时候一直没有

注意到访问权限修饰符)修改了修饰符就解决了问题。

/**
     * 获取
     *
     * @param id
     * @return category
     */
    @RequestMapping(value = "/get_Category", method = RequestMethod.POST)
    @ResponseBody
    public ContentCategory get_Category(@RequestParam(value = "id", required = true) String id,
            HttpServletRequest request,
            HttpServletResponse response, Model model)throws Exception {
        Map<String,Object> params = Maps.newConcurrentMap();
        if (!StringHelper.isEmpty(id, true)){
            params.put("id", id);
        }    
        ContentCategory category = contentCategoryService.get("get", params);
        return category;
    }

复制粘贴,排除错误的时候记得要检查权限修饰的

时间: 2024-08-13 19:42:35

同一个Controller里的同一个Service实例,在当前的Controller里的不同方法中状态不一致的相关文章

进行同一Controller里的不同方法Get请求时,总是定位到Controller里的同一个方法的解决

环境:我用的是webapi+EF4.0+html搭建的环境 问题:进行同一Controller里的不同方法Get请求时,总是定位到Controller里的同一个方法,具体如下: 1.Controller里有如下两个方法: [HttpGet]        public HttpResponseMessage AddPersonToPersonInfo(string pno, string pname); [HttpGet]        public HttpResponseMessage Mo

同一个PC只能运行一个应用实例(考虑多个用户会话情况)

原文:同一个PC只能运行一个应用实例(考虑多个用户会话情况) 1 class Program 2 { 3 private static Mutex m; 4 5 [STAThread] 6 static void Main() 7 { 8 bool createNew = false; 9 10 /* 11 * 在运行终端服务的服务器上,已命名的系统 mutex 可以具有两级可见性. 12 * 如果名称以前缀“Global\”开头,则 mutex 在所有终端服务器会话中均为可见. 13 * 如果

远程调用服务里的方法service,进程间通信adil的学习

1当一个进程需要调用另外一个进程的方法时候,进程可以通过aidl文件以接口的方式将方法抛出.比如android没有对外提供挂电话的方法,若用户想要调用这个方法就必须与电话管理这个应用程序通信,调用挂电话的方法. 2.下面我就举例一个demo调用远程服务里的方法.为了验证service能否单独启动,这个demo启动了2个远程服务,一个有activity的一个只有service的.并且他们抛出的接口名字相同,正好学习一下同名的引用,发现一个java文件里只能import  1个同同名的类,若想调用另

使用Myeclipse 8.5开发基于JAX-WS的Web service实例

使用Myeclipse 8.5开发基于JAX-WS的Web service实例  本文为Web service 开发入门篇,主要介绍在Myeclipse 8.5环境下开发Web service的服务程序和客户端程序的基本流程.  在Weblogic 10.3.4 中部署Web service服务.   开发环境如下:  JAVA IDE: Myeclipse 8.5 开发Web service服务程序,需要了解以下相关内容, WSDL, SOAP, XML.这些是组成Web service 的基

VMWARE里启动kylin16.0时出现&#39;SMBus Host Controller not enabled&#39;(还未进入系统)

在Vmware里安装完Ubuntu16.10,启动时出现'SMBus Host Controller not enabled'错误提示,进不到图形界面.网上搜了一下,解决办法是在图形界面里进终端窗口,编辑blacklist.conf文件,禁止i2c_piix4驱动的加载.但现在系统还没加载完,进不去终端窗口,如何处理呢? 解决办法如下(亲测可用): 在虚拟机上运行Linux内核版本为4.7或以上的系统都在安装过程中或在启动时会因为加载intel_powerclamp驱动而导致崩溃.解决办法:1.

在虚拟机里安装centos 6.4和centos 5.8里配置vim 7.4安装过程

下是centos 6.4里安装vim配置操作命令 [BEGIN] 2014/5/13 星期二 上午 10:08:54 [[email protected] ~]# rpm -qa | grep vim [[email protected] ~]# yum remove vim vim-enhanced vim-common vim-minimal [[email protected] ~]# wget ftp://ftp.vim.org/pub/vim/unix/vim-7.4.tar.bz2

sharepoint 2016 配置project service出错:拓扑服务在此服务器场中不可用

测试环境中配置project service出错:拓扑服务在此服务器场中不可用. 原因是由于这个测试环境中,只有一台服务器,配置minrole时,选择的前端,改为单一服务正常:

@有两个含义:1,在参数里,以表明该变量为伪参数 ,在本例中下文里将用@name变量代入当前代码中2,在字串中,@的意思就是后面的字串以它原本的含义显示,如果不

@有两个含义:1,在参数里,以表明该变量为伪参数 ,在本例中下文里将用@name变量代入当前代码中 2,在字串中,@的意思就是后面的字串以它原本的含义显示,如果不加@那么需要用一些转义符\来显示一些特殊字符 举例1:(2) stringsql ="update Table1 set name [email protected] where ID = '1'";//未采用SqlParameter SqlParameter sp =newSqlParameter("@name&q

C# 防止同一个应用程序运行多个实例

这里以C# Winform为例子说明, 最近在实现网络应用的时候,每个程序只能打开一次,因为会使用同一个端口, 所以为了防止客户不知道的情况下点击多次或者其他,防止这样的情况出现,写了一个小功能实现阻止这件事情发生, 说明: "Chatter" //程序运行的时候,在资源管理器里看到的 “ 映像名称 ” 1 using System; 2 using System.Collections.Generic; 3 using System.Windows.Forms; 4 using Sy