springboot项目记录2用户注册功能
七、注册-业务层
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用户注册功能的更多相关文章
- springboot项目实现批量新增功能
这个困扰我一整天东西,终于解决了. 首先是mybatis中的批量新增sql语句. 注意:这里我给的是我需要新增的字段,你们改成你们需要的字段. <insert id="insertBa ...
- idea配置springboot项目记录
配置文件application.properties server.port=80 server.servlet.context-path=/bookManage spring.mvc.static- ...
- Django项目: 3.用户注册功能
本章内容的补充知识点 导入库的良好顺序: 1.系统库 2.django库 3.自己定义的库(第三方库) redis缓存数据库的数据调用速度快,但是不利于长时间保存. mysql用于长时间存储,但是调用 ...
- 2019-03-26 SpringBoot项目部署遇到跨域问题,记录一下解决历程
近期SpringBoot项目部署遇到跨域问题,记录一下解决历程. 要严格限制,允许哪些域名访问,在application.properties文件里添加配置,配置名可以自己起: cors.allowe ...
- SpringBoot项目实现配置实时刷新功能
需求描述:在SpringBoot项目中,一般业务配置都是写死在配置文件中的,如果某个业务配置想修改,就得重启项目.这在生产环境是不被允许的,这就需要通过技术手段做到配置变更后即使生效.下面就来看一下怎 ...
- 从零开始的SpringBoot项目 ( 五 ) 整合 Swagger 实现在线API文档的功能
综合概述 spring-boot作为当前最为流行的Java web开发脚手架,越来越多的开发者选择用其来构建企业级的RESTFul API接口.这些接口不但会服务于传统的web端(b/s),也会服务于 ...
- Centos8.3、docker部署springboot项目实战记录
引言 目前k8s很是火热,我也特意买了本书去学习了一下,但是k8s动辄都是成百上千的服务器运维,对只有几台服务器的应用来说使用k8s就有点像大炮打蚊子.只有几台服务器的应用运维使用传统的tomc ...
- 构建Springboot项目、实现简单的输出功能、将项目打包成可以执行的JAR包(详细图解过程)
1.构建SpringBoot项目 大致流程 1.新建工程 2.选择环境配置.jdk版本 3.选择 依赖(可省略.后续要手动在pom文件写) 4.项目名 1.1 图解建立过程 1.2 项目结构 友情提示 ...
- springboot项目新功能开发
在原有的springboot项目上,复制了一个,然后将其中的src下的所有java文件都删除,gradle下把中间件都删除,直流springframework的,重新启动,发现 错误Failed to ...
- SpringBoot 项目脚手架
写在前面 之前也一直很少有写SpringBoot项目相关的文章,今天 准备整理一个我自己初始化SpringBoot项目时的一个脚手架,便于自己后面查阅.因为SpringBoot的约定大于配置,在整合各 ...
随机推荐
- yolov5查看训练日志图片和直方图(包括稀疏训练bn直方图)
0.D:\code\codePy\yolov5-6.1\runs\train\exp25文件夹下有 events.out.tfevents.1675823043.DESKTOP-ACC9FL4.521 ...
- python+POM项目设计模式
分为三层: 第一层:common对selenium进行二次封装,定位元素,操作元素的一些方法,公共方法比如连接数据库.读写yml文件等 第二层:页面封装pages 第三层:测试用例cases
- 分布式事务seata
1.事务的4大基本特征. 1)原子性 2)一致性 3)隔离性 4)持久性 2.什么是分布式事务? 本地事务:单服务进程,单数据库资源,同一个连接conn多个事务操作. 分布式事务:多服 ...
- 《Kubernetes零基础快速入门》PDF电子书赠阅
<Python 3.8从入门到精通(视频教学版)> <Kubernetes零基础快速入门> PDF电子书赠阅,个人学习使用,禁止任何形式的商用. https://pan.bai ...
- TP5 事务处理加锁
首先,数据库类型要是InnoDB,其次,加锁必须跟事务同时使用,还有,查询的时候都必须带锁,比如: db('sms')->lock(true)->where(['id'=>1])-& ...
- SqlSugar 代码生成 数据库及表
在实际开发中如何在sqlsugar中通过model生成数据表呢? 废话不说上代码 一.引入sqlsugarcore 二.编写Model代码 先写一个model举例 namespace 用户管理.Mod ...
- win7下virtualbox虚拟机中安装centos后设置共享文件夹
报错信息: building the main Guest Additions module FAILEDunable to find the sources of your current Linu ...
- js内置方法
数组: 1.push()数组最后添加元素,pop()数组删除最后一个: unshift()数组开头添加元素,shift()删除数字第一个: 注意:push()和unshift()可以添加 ...
- Vue基础 · 组件的使用(4)
组件 将公用的功能抽离出来,形成组件:目的:复用代码. 1.1 全局组件 <div id="app"> <!--引用组件,可多次引用--> <demo ...
- JS学习-从服务器获取数据
从服务器获取数据 Ajax 通过使用诸如 XMLHttpRequest 之类的API或者 - 最近以来的 Fetch API 来实现. 这些技术允许网页直接处理对服务器上可用的特定资源的 HTTP 请 ...