七、注册-业务层

7.1规划异常

7.1.1用户在进行注册的时候,可能会产生用户名被占用的错误,抛出一个异常;

RuntimeException异常,作为该异常的子类,然后再去定义具体的异常类型继承这个异常。

写一个业务层异常的基类,ServiceException异常,这个异常继承RuntimeException异常。

package com.example.store.service.ex;

/**
* @Description:业务层异常的基类 throws new ServiceException("业务层产生未知的异常")
* @Author:
* @Date:
*/
public class ServiceException extends RuntimeException{
public ServiceException() {
super();
} public ServiceException(String message) {
super(message);
} public ServiceException(String message, Throwable cause) {
super(message, cause);
} public ServiceException(Throwable cause) {
super(cause);
} protected ServiceException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}

根据业务层不同的功能来详细定义具体的异常类型,统一的去继承ServiceException异常类。

7.1.2用户在注册的时候可能会产生用户名被占用的错误,抛出一个异常:UsernameDuplicatedException异常

public class UsernameDuplicatedException extends ServiceException{
public UsernameDuplicatedException() {
super();
} public UsernameDuplicatedException(String message) {
super(message);
} public UsernameDuplicatedException(String message, Throwable cause) {
super(message, cause);
} public UsernameDuplicatedException(Throwable cause) {
super(cause);
} protected UsernameDuplicatedException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}

7.1.3正在执行数据插入操作的时候,服务器、数据库宕机。处于正在执行插入操作过程中所产生的异常:

InsertException异常。

public class InsertException extends ServiceException {
public InsertException() {
super();
} public InsertException(String message) {
super(message);
} public InsertException(String message, Throwable cause) {
super(message, cause);
} public InsertException(Throwable cause) {
super(cause);
} protected InsertException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}

7.2设计接口和抽象方法

7.2.1在service包下创建一个IUserService

/**用户模块业务层接口**/
public interface IUserService {
/**
*用户注册方法
* @param user用户的数据对象
*/
void reg(User user);
}

7.2.2创建一个实现类UserServiceImpl类,需要实现这个接口,并且实现抽象方法。

7.2.3在单元测试包下创建一个UserServiceTests类,在这个类中添加单元测试的功能。

八、注册-控制层

8.1创建响应

状态码,状态描述信息,数据。这部分功能封装在一个类中,将这个类作为方法返回值,返回给前端浏览器。

/**
* @Description:Json格式的数据进行响应
* @Author:
* @Date:
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class JsonResult<E> implements Serializable {//Serializable接口
/**状态码*/
private Integer state;
/**描述信息*/
private String message;
/**泛型数据*/
private E data;
}

8.2 设计请求

依据当前的业务功能模块进行请求的设计。
请求路径:/user/reg
请求参数:user user
请求类型:POST(有敏感数据就用POST,没有就用get)
响应结果:JsonResult

8.3处理请求

8.3.1创建一个控制层对应的类UserController类。依赖于业务层的接口。

package com.example.store.controller;
import org.springframework.stereotype.Controller;
import com.example.store.entity.User;
import com.example.store.service.IUserService;
import com.example.store.service.ex.InsertException;
import com.example.store.service.ex.UsernameDuplicatedException;
import com.example.store.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
/**
* @Description:
* @Author:
* @Date:
*/
//@Controller //当前类交给Spring来管理、
@RestController//等效于@Controller+@ResponseBody
@RequestMapping("/users")
public class UserController {
@Autowired
private IUserService userService;//依赖于业务层的接口,即userService接口,然后交给Spring自动装配,最后什么样的请求被拦截在子类当中呢。 @RequestMapping("/reg")
//@ResponseBody //表示此方法的响应结果以json格式进行数据的响应给到前端
//如果在这个类中编写了大量的请求处理方法,也就意味着每个类中都要编写一个ResponseBody,是很麻烦的,所以并不建议这样写,这里写组合注解
public JsonResult<Void> reg(User user){ //声明一个User对象,作为方法的参数列表,来接收前端的数据
//创建响应结果对象
JsonResult<Void> result = new JsonResult<>();
try {
userService.reg(user);//在调这个方法的时候,如果抛异常,在Code里面点Surround With 的try/catch
result.setState(200);
result.setMessage("用户名注册成功");
} catch (UsernameDuplicatedException e) { //抛的异常即我们在业务层所定义的异常,这里是两个
result.setState(4000);
result.setMessage("用户名被占用");
}
catch (InsertException e) { //抛的异常即我们在业务层所定义的异常,这里是两个
result.setState(5000);
result.setMessage("注册时产生未知的异常");
}
return result;
}
}

8.4 控制层优化设计

在控制层抽离一个父类,在这个父类中统一的去处理关于异常的相关操作。编写一个BaseController类,统一处理异常。

重新构建了reg()方法。

package com.example.store.controller;
import org.springframework.stereotype.Controller;
import com.example.store.entity.User;
import com.example.store.service.IUserService;
import com.example.store.service.ex.InsertException;
import com.example.store.service.ex.UsernameDuplicatedException;
import com.example.store.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
/**
* @Description:
* @Author:
* @Date:
*/
//@Controller //当前类交给Spring来管理、
@RestController//等效于@Controller+@ResponseBody
@RequestMapping("/users")
public class UserController extends BaseController{
@Autowired
private IUserService userService;//依赖于业务层的接口,即userService接口,然后交给Spring自动装配,最后什么样的请求被拦截在子类当中呢。
@RequestMapping("/reg")
//@ResponseBody //表示此方法的响应结果以json格式进行数据的响应给到前端
//如果在这个类中编写了大量的请求处理方法,也就意味着每个类中都要编写一个ResponseBody,是很麻烦的,所以并不建议这样写,这里写组合注解
public JsonResult<Void> reg(User user){ //声明一个User对象,作为方法的参数列表,来接收前端的数据
userService.reg(user);
return new JsonResult<>(OK);
}

此为UserController类

此为BaseController类

package com.example.store.controller;

import com.example.store.service.ex.InsertException;
import com.example.store.service.ex.ServiceException;
import com.example.store.service.ex.UsernameDuplicatedException;
import com.example.store.util.JsonResult;
import org.springframework.web.bind.annotation.ExceptionHandler; /**
* @Description:控制层的基类
* @Author:
* @Date:
*/
public class BaseController {
/**操作成功的状态码**/
public static final int OK =200;
//请求处理方法,这个方法的返回值就是需要传递给全端的数据
//自动将异常对象传递给此方法的参数列表上
//当前项目中产生了异常,被统一拦截到此方法中,这个方法此时就充当的是请求处理方法,方法的返回值直接给到前端
@ExceptionHandler(ServiceException.class)//用于统一处理抛出的异常
public JsonResult<Void> handleException(Throwable e){
JsonResult<Void> result = new JsonResult<>(e);
if(e instanceof UsernameDuplicatedException){
result.setState(400);
result.setMessage("用户名已经被占用");
} else if(e instanceof InsertException){
result.setState(5000);
result.setMessage("注册时产生了未知的异常");
}
return result;
}
}

springboot项目记录2用户注册功能的更多相关文章

  1. springboot项目实现批量新增功能

    这个困扰我一整天东西,终于解决了. 首先是mybatis中的批量新增sql语句. 注意:这里我给的是我需要新增的字段,你们改成你们需要的字段. <insert id="insertBa ...

  2. idea配置springboot项目记录

    配置文件application.properties server.port=80 server.servlet.context-path=/bookManage spring.mvc.static- ...

  3. Django项目: 3.用户注册功能

    本章内容的补充知识点 导入库的良好顺序: 1.系统库 2.django库 3.自己定义的库(第三方库) redis缓存数据库的数据调用速度快,但是不利于长时间保存. mysql用于长时间存储,但是调用 ...

  4. 2019-03-26 SpringBoot项目部署遇到跨域问题,记录一下解决历程

    近期SpringBoot项目部署遇到跨域问题,记录一下解决历程. 要严格限制,允许哪些域名访问,在application.properties文件里添加配置,配置名可以自己起: cors.allowe ...

  5. SpringBoot项目实现配置实时刷新功能

    需求描述:在SpringBoot项目中,一般业务配置都是写死在配置文件中的,如果某个业务配置想修改,就得重启项目.这在生产环境是不被允许的,这就需要通过技术手段做到配置变更后即使生效.下面就来看一下怎 ...

  6. 从零开始的SpringBoot项目 ( 五 ) 整合 Swagger 实现在线API文档的功能

    综合概述 spring-boot作为当前最为流行的Java web开发脚手架,越来越多的开发者选择用其来构建企业级的RESTFul API接口.这些接口不但会服务于传统的web端(b/s),也会服务于 ...

  7. Centos8.3、docker部署springboot项目实战记录

    引言    目前k8s很是火热,我也特意买了本书去学习了一下,但是k8s动辄都是成百上千的服务器运维,对只有几台服务器的应用来说使用k8s就有点像大炮打蚊子.只有几台服务器的应用运维使用传统的tomc ...

  8. 构建Springboot项目、实现简单的输出功能、将项目打包成可以执行的JAR包(详细图解过程)

    1.构建SpringBoot项目 大致流程 1.新建工程 2.选择环境配置.jdk版本 3.选择 依赖(可省略.后续要手动在pom文件写) 4.项目名 1.1 图解建立过程 1.2 项目结构 友情提示 ...

  9. springboot项目新功能开发

    在原有的springboot项目上,复制了一个,然后将其中的src下的所有java文件都删除,gradle下把中间件都删除,直流springframework的,重新启动,发现 错误Failed to ...

  10. SpringBoot 项目脚手架

    写在前面 之前也一直很少有写SpringBoot项目相关的文章,今天 准备整理一个我自己初始化SpringBoot项目时的一个脚手架,便于自己后面查阅.因为SpringBoot的约定大于配置,在整合各 ...

随机推荐

  1. debian11 bspwm+polybar问题记录(siji字体无法正常显示)

    一.siji字体无法显示. 很懒很菜,就想用开箱即用的原始配置依然遇到了问题...plybar中的bitmap字体siji无法正常显示.即便按照github的siji官方脚本安装了siji字体还是不行 ...

  2. 通过adb查看手机app的包名

    在进行App自动时,需要查看手机应用包名.Activity的信息,下面介绍一种简单的查看手机应用的信息: 1.启动手机的app 2.使用adb shell dumpsys window | finds ...

  3. VMWARE 虚拟机配置优化

    如果硬件性能不足,可做如下优化. 1.禁用 VMWARE  虚拟内存功能. 编辑->首选项-> 内存  , 设置如下,禁用内存交换. 2. 如果虚拟机装在机械盘,而电脑有固太硬盘,可通过 ...

  4. centos上安装python3.6环境和 pip3

    本篇使用的方法,在不删除Python2的版本,使得Python3和Python2共存 1.yum 安装依赖命令: yum -y install zlib-devel bzip2-devel opens ...

  5. CMMI审核期间的主要流程

    整个阶段大致2个月左右,正式评估大概一个星期 一 计划准备阶段,包括我们一些资料的准备,做评估计划等 二 执行评估阶段 启动会议 主要主任评估师讲,所有人员参会 会议内容:主任评估师自我介绍 ,项目信 ...

  6. 【git】2.5远程仓库的使用

    资料来源 (1) https://git-scm.com/book/zh/v2/Git-%E5%9F%BA%E7%A1%80-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93% ...

  7. git常见问题集合

    注1:问题总结来自于实际使用,关于搜到的资料链接一并粘贴; 场景1:GIT本地代码处于detached HEAD的情况(又称游离状态)的解决办法; 问题:有时候git由于一些操作的问题出现了detac ...

  8. SED fitting

    Using the Robitaille (2017) YSO SED models https://notebook.community/hyperion-rt/paper-2017-sed-mod ...

  9. SQL Server【提高】分区表

    分区表 分区视图 分区表可以从物理上将一个大表分成几个小表,但是从逻辑上来看,还是一个大表. 什么时候需要分区表 数据库中某个表中的数据很多. 数据是分段的 分区的方式 水平分区 水平表分区就是将一个 ...

  10. MySQL之中文数据问题

    随笔记录方便自己和同路人查阅. #------------------------------------------------我是可耻的分割线--------------------------- ...