com.nxyfan.framework.common.handler.SwaggerDefaultOkStatusCodeHandler Maven / Gradle / Ivy
package com.nxyfan.framework.common.handler;
import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import springfox.documentation.builders.OperationBuilder;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.OperationBuilderPlugin;
import springfox.documentation.spi.service.contexts.OperationContext;
/**
*
* Describe: 修改swagger中默认的200状态码的处理器
* Author: caoyang
* Create Time: 2022年12月7日 上午8:32:50
* Copyright @ 2022 51LIFE
*/
@Slf4j
@Component
public class SwaggerDefaultOkStatusCodeHandler implements OperationBuilderPlugin {
@Override
public void apply(OperationContext operationContext) {
// 获取Builder
OperationBuilder builder = operationContext.operationBuilder();
// 获取Response集合(这里是通过流重新生成了集合,而跟builder中的responses属性不属于同一个引用)
Set responses = builder.build().getResponseMessages();
// 移除200状态码
// responses.removeIf(response -> response.getCode() == HttpStatus.OK.value());
// 将200修改成100
/*Set list = new HashSet();
Iterator it1 = responses.iterator();
ResponseMessage response100 = null;
while(it1.hasNext()) {
ResponseMessage response = it1.next();
if(response.getCode() == 100) {
response100 = response;
break;
}
}
responses.removeIf(response -> response.getCode() == 100);
if(response100 != null) {
Iterator it2 = responses.iterator();
while(it2.hasNext()) {
ResponseMessage response = it2.next();
if(response.getCode() == HttpStatus.OK.value()) {
//BeanUtil.setFieldValue(response100, "responseModel", response.getResponseModel());
list.add(response100);
BeanUtil.setFieldValue(response, "message", "忽略此状态码,目前没法删除");
list.add(response);
}else {
list.add(response);
}
}
}*/
Iterator it = responses.iterator();
while(it.hasNext()) {
ResponseMessage response = it.next();
if(response.getCode() == HttpStatus.OK.value()) {
//BeanUtil.setFieldValue(response, "message", "忽略此状态码,此为框架自带状态码,目前无法删除");
setFieldValue(response, "message", "忽略此状态码,此为框架自带状态码,目前无法删除");
}
}
// 通过反射清空builder中的responseMessages属性值
//BeanUtil.setFieldValue(builder, "responseMessages", new HashSet());
setFieldValue(builder, "responseMessages", new HashSet());
// 重新赋值response,注意这里的responseMessages()方法里是循环当前传入参数,再add。所以必须通过反射先清空私有属性
operationContext.operationBuilder().responseMessages(responses);
}
/**
*
* Describe: 设置对象属性值
* Author: Administrator
* Create Time: 2023年11月9日 上午11:39:36
* @param obj
* @param fieldName
* @param value
*/
public static void setFieldValue(Object obj, String fieldName, Object value) {
try {
Field field = obj.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
field.set(obj, value);
}catch(NoSuchFieldException e) {
log.error("error while set obj field value", e);
}catch(IllegalAccessException e1) {
log.error("error while set obj field value", e1);
}
}
@Override
public boolean supports(@NonNull DocumentationType documentationType) {
return true;
}
}