可以用springboot+mybatis-plus编写后端代码用户登录注册以及用户车位信息的增删改查(使用JWT+token以及拦截器和异常处理)的完整后端代码吗(不用给我依赖xml文件
时间: 2023-04-03 13:02:57 浏览: 103
可以的,以下是示例代码:
```java
@RestController
@RequestMapping("/api/user")
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/login")
public ResponseEntity<?> login(@RequestBody LoginRequest loginRequest) {
try {
User user = userService.login(loginRequest.getUsername(), loginRequest.getPassword());
String token = JwtUtil.generateToken(user.getId());
return ResponseEntity.ok(new LoginResponse(token));
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
}
}
@PostMapping("/register")
public ResponseEntity<?> register(@RequestBody RegisterRequest registerRequest) {
try {
User user = userService.register(registerRequest.getUsername(), registerRequest.getPassword());
String token = JwtUtil.generateToken(user.getId());
return ResponseEntity.ok(new LoginResponse(token));
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
}
}
@GetMapping("/parking")
public ResponseEntity<?> getParking(@RequestParam("userId") Long userId) {
try {
List<Parking> parkingList = userService.getParking(userId);
return ResponseEntity.ok(parkingList);
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}
@PostMapping("/parking")
public ResponseEntity<?> addParking(@RequestBody Parking parking) {
try {
userService.addParking(parking);
return ResponseEntity.ok().build();
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}
@PutMapping("/parking")
public ResponseEntity<?> updateParking(@RequestBody Parking parking) {
try {
userService.updateParking(parking);
return ResponseEntity.ok().build();
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}
@DeleteMapping("/parking")
public ResponseEntity<?> deleteParking(@RequestParam("id") Long id) {
try {
userService.deleteParking(id);
return ResponseEntity.ok().build();
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}
@ExceptionHandler(Exception.class)
public ResponseEntity<?> handleException(Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}
```
其中,`LoginRequest`、`RegisterRequest`、`LoginResponse`、`User`、`Parking` 分别表示登录请求、注册请求、登录响应、用户、车位等实体类。`UserService` 是业务逻辑层的接口,具体实现可以使用 MyBatis-Plus 的 `ServiceImpl` 类。`JwtUtil` 是 JWT 工具类,可以使用第三方库实现。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)