|
19 | 19 | import org.springframework.beans.factory.annotation.Autowired; |
20 | 20 | import org.springframework.data.domain.Page; |
21 | 21 | import org.springframework.http.MediaType; |
22 | | -import org.springframework.web.bind.annotation.*; |
| 22 | +import org.springframework.web.bind.annotation.PathVariable; |
| 23 | +import org.springframework.web.bind.annotation.RequestBody; |
| 24 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 25 | +import org.springframework.web.bind.annotation.RequestMethod; |
| 26 | +import org.springframework.web.bind.annotation.RestController; |
23 | 27 |
|
24 | | -import java.text.ParseException; |
25 | 28 | import java.util.List; |
| 29 | +import java.util.Optional; |
26 | 30 |
|
27 | 31 | /** |
28 | 32 | * 把今天最好的表现当作明天最新的起点..~ |
@@ -61,12 +65,10 @@ public UserController(UserService service) { |
61 | 65 | @ApiImplicitParams({ |
62 | 66 | @ApiImplicitParam(name = "id", value = "唯一id", required = true, dataType = "Long", paramType = "path"), |
63 | 67 | }) |
| 68 | + @SuppressWarnings("unchecked") |
64 | 69 | public Result findUserById(@PathVariable("id") Long id) { |
65 | | - UserModel userModel = service.findUserById(id); |
66 | | - if (userModel == null) { |
67 | | - return new Result(CodeConst.USER_NOT_FOUND.getResultCode(), CodeConst.USER_NOT_FOUND.getMessage()); |
68 | | - } |
69 | | - return new Result<>(userModel); |
| 70 | + Optional<UserModel> optional = service.findUserById(id); |
| 71 | + return optional.map(Result::new).orElseGet(() -> new Result(CodeConst.USER_NOT_FOUND.getResultCode(), CodeConst.USER_NOT_FOUND.getMessage())); |
70 | 72 | } |
71 | 73 |
|
72 | 74 | /** |
@@ -98,7 +100,7 @@ public Result addUser(@RequestBody UserModel user) { |
98 | 100 | @ApiImplicitParam(name = "密码", required = true, dataType = "String", paramType = "path") |
99 | 101 | }) |
100 | 102 | @RequestMapping(value = "register/{email}/{password}", method = RequestMethod.POST) |
101 | | - public Result register(@PathVariable("email") String email, @PathVariable("password") String password) throws Exception { |
| 103 | + public Result register(@PathVariable("email") String email, @PathVariable("password") String password) { |
102 | 104 | UserModel userModel = service.findUserByEmail(email); |
103 | 105 | //邮箱被占用 |
104 | 106 | if (userModel != null) { |
@@ -221,7 +223,7 @@ public Result deleteUserById(@PathVariable("id") Long id) throws UserNotFoundExc |
221 | 223 | @ApiOperation(value = "处理激活", notes = "处理激活", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) |
222 | 224 | @RequestMapping(value = "validateEmail", method = RequestMethod.POST) |
223 | 225 | public Result validateEmail(@RequestBody UserModel user |
224 | | - ) throws ServiceException, ParseException, UserNotFoundException { |
| 226 | + ) throws ServiceException { |
225 | 227 | //数据访问层,通过email获取用户信息 |
226 | 228 | UserModel userModel = service.findUserByEmail(user.getEmail()); |
227 | 229 | if (userModel != null) { |
|
0 commit comments