refactor: 回退全局响应结果处理器

1.影响 API 文档生成
2.其他已知及未知影响
This commit is contained in:
Bull-BCLS
2023-11-29 23:34:00 +08:00
parent 27d401f44b
commit d668620bfe
13 changed files with 78 additions and 140 deletions

View File

@@ -78,9 +78,9 @@ public abstract class BaseController<S extends BaseService<L, D, Q, C>, L, D, Q,
@Operation(summary = "分页查询列表", description = "分页查询列表")
@ResponseBody
@GetMapping
public PageDataResp<L> page(Q query, @Validated PageQuery pageQuery) {
public R<PageDataResp<L>> page(Q query, @Validated PageQuery pageQuery) {
this.checkPermission(Api.LIST);
return baseService.page(query, pageQuery);
return R.ok(baseService.page(query, pageQuery));
}
/**
@@ -95,9 +95,9 @@ public abstract class BaseController<S extends BaseService<L, D, Q, C>, L, D, Q,
@Operation(summary = "查询树列表", description = "查询树列表")
@ResponseBody
@GetMapping("/tree")
public List<Tree<Long>> tree(Q query, SortQuery sortQuery) {
public R<List<Tree<Long>>> tree(Q query, SortQuery sortQuery) {
this.checkPermission(Api.LIST);
return baseService.tree(query, sortQuery, false);
return R.ok(baseService.tree(query, sortQuery, false));
}
/**
@@ -112,9 +112,9 @@ public abstract class BaseController<S extends BaseService<L, D, Q, C>, L, D, Q,
@Operation(summary = "查询列表", description = "查询列表")
@ResponseBody
@GetMapping("/list")
public List<L> list(Q query, SortQuery sortQuery) {
public R<List<L>> list(Q query, SortQuery sortQuery) {
this.checkPermission(Api.LIST);
return baseService.list(query, sortQuery);
return R.ok(baseService.list(query, sortQuery));
}
/**
@@ -128,9 +128,9 @@ public abstract class BaseController<S extends BaseService<L, D, Q, C>, L, D, Q,
@Parameter(name = "id", description = "ID", example = "1", in = ParameterIn.PATH)
@ResponseBody
@GetMapping("/{id}")
public D get(@PathVariable Long id) {
public R<D> get(@PathVariable Long id) {
this.checkPermission(Api.LIST);
return baseService.get(id);
return R.ok(baseService.get(id));
}
/**

View File

@@ -1,70 +0,0 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.cnadmin.common.handler;
import lombok.RequiredArgsConstructor;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import cn.hutool.core.util.StrUtil;
import top.charles7c.cnadmin.common.annotation.NoResponseAdvice;
import top.charles7c.cnadmin.common.model.resp.R;
/**
* 全局响应结果处理器
*
* @author BULL_BCLS
* @since 2023/10/8 20:19
*/
@RestControllerAdvice
@RequiredArgsConstructor
public class GlobalResponseBodyAdviceHandler implements ResponseBodyAdvice<Object> {
private static final String[] EXCLUDE = {"MultipleOpenApiWebMvcResource", "SwaggerConfigResource",};
private final ObjectMapper objectMapper;
@Override
public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> converterType) {
return !methodParameter.getParameterType().isAssignableFrom(R.class)
&& !methodParameter.hasMethodAnnotation(NoResponseAdvice.class)
&& !StrUtil.equalsAny(methodParameter.getDeclaringClass().getSimpleName(), EXCLUDE);
}
@Override
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request,
ServerHttpResponse response) {
if (String.class.equals(returnType.getGenericParameterType())) {
try {
return objectMapper.writeValueAsString(R.ok(body));
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
return R.ok(body);
}
}