用户名重复时抛出null异常
相关截图:
问题描述:
controller中判断用户名密码为空这些信息都能正确返回。但是用户名重复时就会抛出null异常,异常截图如上;
这个null异常是怎么回事?
相关代码:
package com.he.mall.controller;
import com.he.mall.common.ApiRestResponse;
import com.he.mall.exception.HeMallException;
import com.he.mall.exception.HeMallExceptionEnum;
import com.he.mall.model.pojo.User;
import com.he.mall.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* 用户控制器
*/
@Controller
public class UserController {
@Autowired
UserService userService;
@GetMapping("/test")
@ResponseBody
public User personalPage() {
return userService.getUser();
}
@PostMapping("/register")
@ResponseBody
public ApiRestResponse register(@RequestParam("userName") String userName, @RequestParam("password") String password) throws HeMallException {
//userName判断是否为空
if (StringUtils.isEmpty(userName)) {
return ApiRestResponse.error(HeMallExceptionEnum.NEED_USERNAME);
}
//password判断是否为空
if (StringUtils.isEmpty(password)) {
return ApiRestResponse.error(HeMallExceptionEnum.NEED_PASSWORD);
} else if (password.length() < 8) {
//密码长度不能少于8位
return ApiRestResponse.error(HeMallExceptionEnum.PASSWORD_TOO_SHORT);
}
//保存至数据库
userService.register(userName, password);
return ApiRestResponse.success();
}
}
相关代码:
package com.he.mall.exception;
public class HeMallException extends Exception {
private final Integer status;
private final String msg;
public HeMallException(Integer status, String msg) {
this.status = status;
this.msg = msg;
}
public HeMallException(HeMallExceptionEnum exceptionEnum) {
this(exceptionEnum.getStatus(), exceptionEnum.getMsg());
}
public Integer getStatus() {
return status;
}
public String getMsg() {
return msg;
}
}
相关代码:
package com.he.mall.exception;
/**
* 异常枚举
*/
public enum HeMallExceptionEnum {
NEED_USERNAME(10001,"用户名不能为空"),
NEED_PASSWORD(10002,"密码不能为空"),
PASSWORD_TOO_SHORT(10003,"密码长度不能少于8位"),
NAME_EXISTED(10004,"用户已存在,注册失败"),
INSERT_FAILDED(10005,"系统错误,注册失败");
Integer status;//异常状态代码
String msg;//异常信息
HeMallExceptionEnum(Integer status, String msg) {
this.status = status;
this.msg = msg;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
相关代码:
package com.he.mall.service.impl;
import com.he.mall.exception.HeMallException;
import com.he.mall.exception.HeMallExceptionEnum;
import com.he.mall.model.dao.UserMapper;
import com.he.mall.model.pojo.User;
import com.he.mall.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* UserService实现类
*/
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserMapper userMapper;
@Override
public User getUser() {
return userMapper.selectByPrimaryKey(1);
}
@Override
public void register(String userName, String password) throws HeMallException {
//查询是否存在相同userName
User userResult = userMapper.selectByUserName(userName);
if (userResult != null) {
throw new HeMallException(HeMallExceptionEnum.NAME_EXISTED);
}
//保存至数据库
User user = new User();
user.setUsername(userName);
user.setPassword(password);
int count = userMapper.insertSelective(user);
//保存失败时处理
if (count == 0) {
throw new HeMallException(HeMallExceptionEnum.INSERT_FAILDED);
}
}
}
25
收起
正在回答
1回答
同学你好,程序在输出异常堆栈信息的时候,会调用HeMallException类中getMessage()方法。由于同学HeMallException类中没有getMessage()方法,所以会输出null。
建议同学将HeMallException类中属性msg改为message,并重新生成get方法。
参考代码如下:
祝学习愉快~
java工程师2020版
- 参与学习 人
- 提交作业 9401 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星