diff --git a/.gitignore b/.gitignore index 90e15af88b04a4333d4b95af010656830565ff4f..b0756b07c4d591d32602b2b712f265b0207e75e2 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/config/SpringMvcConfiguration.java b/src/main/java/com/snhanyue/course/config/SpringMvcConfiguration.java new file mode 100644 index 0000000000000000000000000000000000000000..5716e3e50209a2b5090b0fd90af6d61b216b0003 --- /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 0000000000000000000000000000000000000000..643ba5766ef3aefdcf87c471f7ed610d5568a088 --- /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 = "/api")//指定返回值为json格式字符集为utf-8; +public class UsersController { + + @Autowired + private IUsersService usersService; + + @PostMapping("/users/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 0000000000000000000000000000000000000000..6a84032f49e522fd6c7f6ab281a77ec9d64e98d3 --- /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 589390c5f0e8d4a2236b6126b1464ded62623e88..14b8bbae37cb1f08464ae1686614f97e95631b9e 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 9e7cc5cc49ee11432abce75194f16aafb241c247..89e3c746436796c3c709ca94743781e8248d1b48 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 0000000000000000000000000000000000000000..1633d3ef201f10cae5f94f0de18e93801a1e8e71 --- /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/main/resources/application.yml b/src/main/resources/application.yml index b9d502308f0b322ff6aec9baf6b58cfe866bc38f..aae151a052a9b36e0205efe77f6694ba4337692b 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 new file mode 100644 index 0000000000000000000000000000000000000000..767b87a9a51d1c65d6f8d91d5e6dea8c5a6182bb --- /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"; + //请求协议w + 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()); // 打印日志 + } +}