请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

Whitelabel Error Page

浏览器页面显示:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Tue Oct 13 23:52:27 CST 2020
There was an unexpected error (type=Internal Server Error, status=500).

后端报错:
2020-10-13 23:52:27.329 ERROR 19779 — [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause

代码分别实:

package com.itmuch.usercenter.service.user;

import com.itmuch.usercenter.dao.user.UserMapper;
import com.itmuch.usercenter.domain.entity.user.User;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserService {
    @Resource
    private UserMapper userMapper;

    public User findUserId(Integer id){
        return this.userMapper.selectByPrimaryKey(id);
    }
}

package com.itmuch.usercenter.controller.user;

import com.itmuch.usercenter.domain.entity.user.User;
import com.itmuch.usercenter.service.user.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/users")
public class UserController {

    private UserService userService;

    @GetMapping("/{id}")
    public User findUserId(@PathVariable Integer id){
        return this.userService.findUserId(id);
    }


}

正在回答 回答被采纳积分+3

1回答

大目 2020-10-14 00:03:28

首先,分析日志:

2020-10-13 23:52:27.329 ERROR 19779 — [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause

从异常来看,出现了空指针异常。这种异常是Java世界最好解决的异常了。可以看下报空指针的异常栈(也就是空指针异常打印出来的完整信息),逐行排查,就可以找到问题在哪里。 

不过很遗憾,你贴到发生空指针就戛然而止了,这之后的内容,也是最重要的内容您没有贴出来。

======

那么不妨分析代码:

@RestController
@RequestMapping("/users")
public class UserController {
    private UserService userService;

这里少了个@Autowired注解。改为类似如下代码即可。

@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService userService;

=====

其实这是个非常简单的问题,我完全可以直接告诉你答案,但还是贴下分析过程,希望同学可以做个参考,并在学习的过程中逐步锻炼出一套自己的定位问题的思路、方法。

祝您学习愉快!

0 回复 有任何疑惑可以回复我~
  • 提问者 网虫3331772 #1
    谢谢老师
    回复 有任何疑惑可以回复我~ 2020-10-14 01:04:53
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号