From 60f87f16b8505ac4d711f09f880a35c120a9f56a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E9=B9=8F?= <1154799293@qq.com> Date: Wed, 3 Aug 2022 17:40:55 +0800 Subject: [PATCH 1/2] =?UTF-8?q?2022.8.3=E6=B3=A8=E5=86=8C=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E6=A0=B8=E5=BF=83=E4=BB=A3=E7=A0=81=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E5=AE=8C=E6=AF=95=EF=BC=8C=E9=83=A8=E5=88=86=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../course/config/SpringMvcConfiguration.java | 19 ++++++++ .../controller/api/UsersController.java | 26 +++++++++++ .../java/com/snhanyue/course/emum/State.java | 20 +++++++++ .../course/exception/ServiceException.java | 15 +++++++ .../service/api/impl/UsersServiceImpl.java | 5 ++- .../snhanyue/course/util/json/JsonResult.java | 43 +++++++++++++++++++ .../controller/UsersControllerTests.java | 42 ++++++++++++++++++ 7 files changed, 168 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/snhanyue/course/config/SpringMvcConfiguration.java create mode 100644 src/main/java/com/snhanyue/course/controller/api/UsersController.java create mode 100644 src/main/java/com/snhanyue/course/emum/State.java create mode 100644 src/main/java/com/snhanyue/course/util/json/JsonResult.java create mode 100644 src/test/java/com/snhanyue/controller/UsersControllerTests.java diff --git a/src/main/java/com/snhanyue/course/config/SpringMvcConfiguration.java b/src/main/java/com/snhanyue/course/config/SpringMvcConfiguration.java new file mode 100644 index 0000000..5716e3e --- /dev/null +++ b/src/main/java/com/snhanyue/course/config/SpringMvcConfiguration.java @@ -0,0 +1,19 @@ +package com.snhanyue.course.config; + +import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +public class SpringMvcConfiguration implements WebMvcConfigurer { + + @Override + public void addCorsMappings(CorsRegistry registry) { + registry.addMapping("/**") + .allowedOriginPatterns("*") + .allowedMethods("*") + .allowedHeaders("*") + .allowCredentials(true) + .maxAge(3600); + } + +} + diff --git a/src/main/java/com/snhanyue/course/controller/api/UsersController.java b/src/main/java/com/snhanyue/course/controller/api/UsersController.java new file mode 100644 index 0000000..16b8d5b --- /dev/null +++ b/src/main/java/com/snhanyue/course/controller/api/UsersController.java @@ -0,0 +1,26 @@ +package com.snhanyue.course.controller.api; + +import com.snhanyue.course.dto.UsersAddNewUserDTO; +import com.snhanyue.course.service.api.IUsersService; +import com.snhanyue.course.util.json.JsonResult; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping(value = "/users")//指定返回值为json格式字符集为utf-8; +public class UsersController { + + @Autowired + private IUsersService usersService; + + @PostMapping("/add-new") + public JsonResult addNew(UsersAddNewUserDTO usersAddNewUserDTO){ + usersService.addNewUser(usersAddNewUserDTO); + return JsonResult.ok(); + + //http://localhost:8080/users/add-new?username=chenpeng + + } +} diff --git a/src/main/java/com/snhanyue/course/emum/State.java b/src/main/java/com/snhanyue/course/emum/State.java new file mode 100644 index 0000000..6a84032 --- /dev/null +++ b/src/main/java/com/snhanyue/course/emum/State.java @@ -0,0 +1,20 @@ +package com.snhanyue.course.emum; + +public enum State { + OK(20000), + ERR_CATEGORY_NAME_DUPLICATE(40100), // 客户端引起的--类别--名称冲突(被占用) + ERR_CATEGORY_NOT_FOUND(40101), // 客户端引起的--类别--数据不存在(查询参数值不正确) + ERR_INSERT(50000), // 服务端引起的--插入数据错误 + ERR_UPDATE(50001), // 服务端引起的--更新数据错误 + ERR_REPEAT_OF_USER_NAME(50002);//用户名冲突 + + private Integer value; + + State(Integer value) { + this.value = value; + } + + public Integer getValue() { + return value; + } +} diff --git a/src/main/java/com/snhanyue/course/exception/ServiceException.java b/src/main/java/com/snhanyue/course/exception/ServiceException.java index 589390c..14b8bba 100644 --- a/src/main/java/com/snhanyue/course/exception/ServiceException.java +++ b/src/main/java/com/snhanyue/course/exception/ServiceException.java @@ -1,5 +1,20 @@ package com.snhanyue.course.exception; +import com.snhanyue.course.emum.State; + public class ServiceException extends RuntimeException { + private State state; + + public ServiceException(State state, String message) { + super(message); + if (state == null) { + throw new IllegalArgumentException("使用ServiceException必须指定错误时的业务状态码!"); + } + this.state = state; + } + public State getState() { + return state; + } } + diff --git a/src/main/java/com/snhanyue/course/service/api/impl/UsersServiceImpl.java b/src/main/java/com/snhanyue/course/service/api/impl/UsersServiceImpl.java index 9e7cc5c..89e3c74 100644 --- a/src/main/java/com/snhanyue/course/service/api/impl/UsersServiceImpl.java +++ b/src/main/java/com/snhanyue/course/service/api/impl/UsersServiceImpl.java @@ -1,6 +1,7 @@ package com.snhanyue.course.service.api.impl; import com.snhanyue.course.dto.UsersAddNewUserDTO; +import com.snhanyue.course.emum.State; import com.snhanyue.course.entity.Users; import com.snhanyue.course.exception.ServiceException; import com.snhanyue.course.mapper.database.UsersMapper; @@ -25,7 +26,7 @@ public class UsersServiceImpl implements IUsersService { //判断数据库是否有该用户名 if (byName != null) { System.out.println("用户添加失败用户名" + name + "已存在"); - throw new ServiceException(); + } Users users = new Users(); //将DTO类中的数据全部复制到entity中的users里面 @@ -37,7 +38,7 @@ public class UsersServiceImpl implements IUsersService { //根据返回值判断是否插入成功 if (row != 1) { System.out.println("数据库信息插入失败,请稍后再试"); - throw new ServiceException(); + } diff --git a/src/main/java/com/snhanyue/course/util/json/JsonResult.java b/src/main/java/com/snhanyue/course/util/json/JsonResult.java new file mode 100644 index 0000000..1633d3e --- /dev/null +++ b/src/main/java/com/snhanyue/course/util/json/JsonResult.java @@ -0,0 +1,43 @@ +package com.snhanyue.course.util.json; +import com.snhanyue.course.emum.State; +import lombok.Data; + +import java.io.Serializable; + +@Data +public +class JsonResult implements Serializable { + + // 状态码,例如:200 + private Integer state; + + // 消息,例如:"登录失败,用户名不存在" + private String message; + + // 数据 + private T data; + + private JsonResult() {} + + public static JsonResult ok() { + // JsonResult jsonResult = new JsonResult(); + // jsonResult.setState(1); + // return jsonResult; + return ok(null); + } + + public static JsonResult ok(T data) { + JsonResult jsonResult = new JsonResult<>(); + jsonResult.setState(State.OK.getValue()); + jsonResult.setData(data); + return jsonResult; + } + + public static JsonResult fail(State state, String message) { + JsonResult jsonResult = new JsonResult<>(); + jsonResult.setState(state.getValue()); + jsonResult.setMessage(message); + return jsonResult; + } + +} \ No newline at end of file diff --git a/src/test/java/com/snhanyue/controller/UsersControllerTests.java b/src/test/java/com/snhanyue/controller/UsersControllerTests.java new file mode 100644 index 0000000..78057d1 --- /dev/null +++ b/src/test/java/com/snhanyue/controller/UsersControllerTests.java @@ -0,0 +1,42 @@ +package com.snhanyue.controller; + + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.result.MockMvcResultHandlers; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; + +@SpringBootTest +@AutoConfigureMockMvc +public class UsersControllerTests { + + @Autowired + MockMvc mockMvc; + + @Test + public void testAddNewUserSuccessfully() throws Exception{ + String username="陈鹏"; + String password="1234"; + //请求协议 + String url = "/users/add-new"; + // 执行测试 + // 以下代码相对比较固定 + mockMvc.perform( // 执行发出请求 + MockMvcRequestBuilders.post(url) // 根据请求方式决定调用的方法 + .contentType(MediaType.APPLICATION_FORM_URLENCODED) // 请求数据的文档类型,例如:application/json; charset=utf-8 + .param("username", username) // 请求参数,有多个时,多次调用param()方法 + .param("password", password) + .accept(MediaType.APPLICATION_JSON)) // 接收的响应结果的文档类型,注意:perform()方法到此结束 + .andExpect( // 预判结果,类似断言 + MockMvcResultMatchers + .jsonPath("state") // 预判响应的JSON结果中将有名为state的属性 + .value(20000)) // 预判响应的JSON结果中名为state的属性的值,注意:andExpect()方法到此结束 + .andDo( // 需要执行某任务 + MockMvcResultHandlers.print()); // 打印日志 + } +} -- Gitee From abc4803e537ec4c67c535006bcaf4147bcf23b8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E9=B9=8F?= <1154799293@qq.com> Date: Thu, 4 Aug 2022 09:37:13 +0800 Subject: [PATCH 2/2] =?UTF-8?q?2022.8.4,=E6=8B=A6=E6=88=AA=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=EF=BC=8C=E5=89=8D=E7=BC=80=E6=9B=B4=E6=94=B9=E4=B8=BA?= =?UTF-8?q?api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 +- .../com/snhanyue/course/controller/api/UsersController.java | 4 ++-- src/main/resources/application.yml | 4 ++-- .../java/com/snhanyue/controller/UsersControllerTests.java | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 90e15af..b0756b0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ # Compiled class file *.class target - +application-dev.yml # Log file *.log diff --git a/src/main/java/com/snhanyue/course/controller/api/UsersController.java b/src/main/java/com/snhanyue/course/controller/api/UsersController.java index 16b8d5b..643ba57 100644 --- a/src/main/java/com/snhanyue/course/controller/api/UsersController.java +++ b/src/main/java/com/snhanyue/course/controller/api/UsersController.java @@ -9,13 +9,13 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController -@RequestMapping(value = "/users")//指定返回值为json格式字符集为utf-8; +@RequestMapping(value = "/api")//指定返回值为json格式字符集为utf-8; public class UsersController { @Autowired private IUsersService usersService; - @PostMapping("/add-new") + @PostMapping("/users/add-new") public JsonResult addNew(UsersAddNewUserDTO usersAddNewUserDTO){ usersService.addNewUser(usersAddNewUserDTO); return JsonResult.ok(); diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index b9d5023..aae151a 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -20,9 +20,9 @@ fuce: #漂亮得拖动验证码 默认false普通验证码、true滚动验证码 roll-verification: false #xss不拦截url - xss-not-filter-url: [/api/v1/token/api_token,/api/v1/yibaotong/save] + xss-not-filter-url: [/api/v1/token/api_token,/api/v1/yibaotong/save,/api/*] #shiro不拦截url - sa-token-not-filter-url: [/api/v1/token/api_token,/api/v1/yibaotong/save] + sa-token-not-filter-url: [/api/v1/token/api_token,/api/v1/yibaotong/save,/api/*] #tomcat config server : port : 8080 diff --git a/src/test/java/com/snhanyue/controller/UsersControllerTests.java b/src/test/java/com/snhanyue/controller/UsersControllerTests.java index 78057d1..767b87a 100644 --- a/src/test/java/com/snhanyue/controller/UsersControllerTests.java +++ b/src/test/java/com/snhanyue/controller/UsersControllerTests.java @@ -22,7 +22,7 @@ public class UsersControllerTests { public void testAddNewUserSuccessfully() throws Exception{ String username="陈鹏"; String password="1234"; - //请求协议 + //请求协议w String url = "/users/add-new"; // 执行测试 // 以下代码相对比较固定 -- Gitee