mirror of
https://gitee.com/lakernote/easy-admin.git
synced 2026-09-03 05:33:47 +08:00
[Optimize] demo controller.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package com.laker.admin.framework.ext.mvc;
|
||||
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.web.bind.support.WebDataBinderFactory;
|
||||
import org.springframework.web.context.request.NativeWebRequest;
|
||||
@@ -27,17 +28,15 @@ public class PageRequestArgumentResolver implements HandlerMethodArgumentResolve
|
||||
PageRequest.PageRequestBuilder builder = PageRequest.builder();
|
||||
String page = webRequest.getParameter("page");
|
||||
String pageSize = webRequest.getParameter("size");
|
||||
|
||||
page = StrUtil.isBlank(page) ? "1" : page;
|
||||
pageSize = StrUtil.isBlank(pageSize) ? "10" : pageSize;
|
||||
String[] directionParameter = webRequest.getParameterValues("sort");
|
||||
List<PageRequest.Order> allOrders = new ArrayList<>();
|
||||
assert pageSize != null;
|
||||
assert page != null;
|
||||
builder.page(Integer.parseInt(page))
|
||||
.size(Integer.parseInt(pageSize))
|
||||
.orders(allOrders);
|
||||
if (ArrayUtil.isNotEmpty(directionParameter)) {
|
||||
for (String part : directionParameter) {
|
||||
|
||||
if (part == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -24,16 +24,14 @@ public class WebSocketEventListener {
|
||||
@Autowired
|
||||
StompMessageService stompMessageService;
|
||||
|
||||
|
||||
/**
|
||||
* 建立连接监听
|
||||
*/
|
||||
@EventListener
|
||||
public void handleConnectListener(SessionConnectedEvent sessionConnectedEvent) {
|
||||
EasyPrincipal user = (EasyPrincipal) sessionConnectedEvent.getUser();
|
||||
log.warn("建立连接 {} -> {}", user, sessionConnectedEvent);
|
||||
log.info("建立连接 {} -> {}", user, sessionConnectedEvent);
|
||||
// 加入在线用户列表
|
||||
|
||||
ThreadUtil.execute(() -> {
|
||||
// 缓几秒钟再广播消息防止,接收不到
|
||||
try {
|
||||
@@ -56,7 +54,7 @@ public class WebSocketEventListener {
|
||||
@EventListener
|
||||
public void handleDisconnectListener(SessionDisconnectEvent sessionDisconnectEvent) {
|
||||
EasyPrincipal user = (EasyPrincipal) sessionDisconnectEvent.getUser();
|
||||
log.warn("断开连接 -> {}", sessionDisconnectEvent);
|
||||
log.info("断开连接 -> {}", sessionDisconnectEvent);
|
||||
// 剔除在线用户列表
|
||||
if (user == null) {
|
||||
return;
|
||||
|
||||
@@ -21,4 +21,9 @@ public enum Distance implements IEnum<String> {
|
||||
public String getValue() {
|
||||
return unit;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return unit;
|
||||
}
|
||||
}
|
||||
@@ -3,14 +3,25 @@ package com.laker.admin.module.ext.controller;
|
||||
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
|
||||
import com.laker.admin.framework.ext.mvc.CurrentUser;
|
||||
import com.laker.admin.framework.ext.mvc.PageRequest;
|
||||
import com.laker.admin.framework.model.Response;
|
||||
import com.laker.admin.module.enums.DemoTypeEnum;
|
||||
import com.laker.admin.module.enums.Distance;
|
||||
import com.laker.admin.module.ext.vo.qo.City;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiImplicitParam;
|
||||
import io.swagger.annotations.ApiImplicitParams;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -21,8 +32,8 @@ import java.util.List;
|
||||
* @author laker
|
||||
* @since 2021-08-16
|
||||
*/
|
||||
@Api(tags = "示例-各种请求参数示例")
|
||||
@ApiSupport(order = 100)
|
||||
@Api(tags = "示例-△△△各种请求参数示例△△△")
|
||||
@ApiSupport(order = -1)
|
||||
@RestController
|
||||
@RequestMapping("/ext/demo")
|
||||
@Slf4j
|
||||
@@ -37,39 +48,114 @@ public class DemoController {
|
||||
}
|
||||
|
||||
@GetMapping("/1")
|
||||
@ApiOperation(value = "枚举 - querystring")
|
||||
public void pageAll2(Distance distance) {
|
||||
@ApiOperation(value = "枚举 - querystring,默认是枚举值 都是大写的这里是可以自定义")
|
||||
public void pageAll1(Distance distance) {
|
||||
log.info(distance.toString());
|
||||
}
|
||||
|
||||
@GetMapping("/2")
|
||||
@ApiOperation(value = "对象 - querystring")
|
||||
public void pageAll3(City city) {
|
||||
@ApiImplicitParams({
|
||||
@ApiImplicitParam(name = "distance", value = "距离", required = true, dataType = "Distance", paramType = "query")
|
||||
})
|
||||
public Response<City> pageAll2(City city) {
|
||||
log.info(city.toString());
|
||||
return Response.ok(city);
|
||||
}
|
||||
|
||||
@PostMapping("/3")
|
||||
@ApiOperation(value = "实体 - json")
|
||||
public City pageAll4(@RequestBody City city) {
|
||||
public Response<City> pageAll3(@RequestBody City city) {
|
||||
log.info(city.toString());
|
||||
return city;
|
||||
return Response.ok(city);
|
||||
}
|
||||
|
||||
@PostMapping("/4")
|
||||
@ApiOperation(value = "实体 - json")
|
||||
public void pageAll5(@RequestBody City city, CurrentUser user) {
|
||||
@ApiOperation(value = "实体-json + 自动填充CurrentUser")
|
||||
public void pageAll4(@RequestBody City city, CurrentUser user) {
|
||||
log.info("city:{},user{}", city, user.toString());
|
||||
}
|
||||
|
||||
@GetMapping("/5")
|
||||
@ApiOperation(value = "请求参数的PageRequest对象自动填充 - PageRequest")
|
||||
public void pageAll6(PageRequest user) {
|
||||
@ApiImplicitParams({
|
||||
@ApiImplicitParam(name = "page", value = "当前页", example = "1", dataType = "int", paramType = "query"),
|
||||
@ApiImplicitParam(name = "size", value = "每页大小", example = "10", dataType = "int", paramType = "query"),
|
||||
@ApiImplicitParam(name = "sort", value = "排序", example = "create_time,asc", dataType = "String", paramType = "query")
|
||||
})
|
||||
public void pageAll5(PageRequest user) {
|
||||
log.info(user.toString());
|
||||
}
|
||||
|
||||
@PostMapping("/6")
|
||||
@ApiOperation(value = "请求参数的CurrentUser对象自动填充 - CurrentUser")
|
||||
public void pageAll5(CurrentUser user) {
|
||||
public void pageAll6(CurrentUser user) {
|
||||
log.info("user{}", user.toString());
|
||||
}
|
||||
|
||||
@PostMapping("/upload")
|
||||
@ApiOperation(value = "上传文件")
|
||||
public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile file) {
|
||||
// 处理文件上传逻辑
|
||||
return ResponseEntity.ok("File uploaded successfully");
|
||||
}
|
||||
|
||||
@GetMapping("/download/{fileId}")
|
||||
@ApiOperation(value = "下载文件")
|
||||
public ResponseEntity<Resource> downloadFile(@PathVariable String fileId) {
|
||||
// 根据 fileId 获取文件并返回下载
|
||||
Resource fileResource = new FileSystemResource(Paths.get("storage/nginx.conf")); // 获取文件资源的逻辑
|
||||
return ResponseEntity.ok()
|
||||
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileResource.getFilename() + "\"")
|
||||
.body(fileResource);
|
||||
}
|
||||
|
||||
@GetMapping("/datas")
|
||||
@ApiOperation(value = "查询数据列表")
|
||||
public ResponseEntity<List<City>> fetchData(@RequestParam(required = false) String keyword) {
|
||||
List<City> dataList = new ArrayList<>(); // 查询数据的逻辑
|
||||
return ResponseEntity.ok(dataList);
|
||||
}
|
||||
|
||||
@GetMapping("/datas/{id}")
|
||||
@ApiOperation("获取单个数据信息")
|
||||
public ResponseEntity<City> getUserById(@PathVariable String id) {
|
||||
City city = new City();
|
||||
if (city != null) {
|
||||
return ResponseEntity.ok(city);
|
||||
} else {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@PostMapping("/datas")
|
||||
@ApiOperation(value = "新增数据")
|
||||
public ResponseEntity<City> addData(@RequestBody City city) {
|
||||
// 添加新数据的逻辑
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(city);
|
||||
}
|
||||
|
||||
@PutMapping("/datas/{id}")
|
||||
@ApiOperation(value = "更新数据")
|
||||
public ResponseEntity<City> updateData(@PathVariable String id, @RequestBody City city) {
|
||||
// 根据 id 更新数据的逻辑
|
||||
if (city != null) {
|
||||
return ResponseEntity.ok(city);
|
||||
} else {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
}
|
||||
|
||||
@DeleteMapping("/datas/{id}")
|
||||
@ApiOperation(value = "删除数据")
|
||||
public ResponseEntity<String> deleteData(@PathVariable String id) {
|
||||
// 根据 id 删除数据的逻辑
|
||||
if (true) {
|
||||
return ResponseEntity.noContent().build();
|
||||
} else {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,12 +1,18 @@
|
||||
package com.laker.admin.module.ext.vo.qo;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.laker.admin.module.enums.Distance;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@ApiModel(description = "城市信息")
|
||||
public class City {
|
||||
|
||||
@ApiModelProperty(value = "城市ID", example = "1")
|
||||
private String id;
|
||||
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
|
||||
|
||||
@ApiModelProperty(value = "距离", example = "km")
|
||||
// @JsonFormat(shape = JsonFormat.Shape.OBJECT)
|
||||
private Distance distance;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user