修改Swagger2ControllerWebMvc的返回值.
swagger Swagger2ControllerWebMvc 返回值修改
有些场景下相对swagger-doc 返回的⽂档信息做些增强等. 但pringfox.documentation.swagger.v2.path ⽆法⾃定义controller , 所以可以使⽤切⾯⽅式拦截下最后序列化的地⽅.
序列化
@Aspect
mvc的controller@Component
@AllArgsConstructor
public class SwaggerAspect {
private final ServerProperties serverProperties;
@Around("execution(* springfox.documentation.spring.web.Json(..))")
public Object switchDataSource(ProceedingJoinPoint joinPoint) throws Throwable {
Object[] args = Args();
if (ArrayUtils.isEmpty(args)) {
return joinPoint.proceed(args);
}
String serverIp = "127.0.0.1";
HttpServletRequest request = getRequest();
if (request != null) {
serverIp = Header("X-GATEWAY-CLIENT-IP");
//如果是nvriot-saas-uaa需要改成ip ?
if (StringUtils.isEmpty(serverIp)) {
//获取本机
serverIp = NetUtils.localIP() + ":" + Port();
}
}
String finalServerIp = serverIp;
Arrays.stream(args)
.filter(arg -> arg instanceof Swagger)
.map(arg -> (Swagger) arg)
.map(Swagger::getPaths)
.forEach(opMap -> {
for (Map.Entry<String, Path> mp : Set()) {
Path value = mp.getValue();
List<Operation> operations = Operations();
for (Operation operation : operations) {
//TODO currentIp
HeaderParameter headerParameter = new HeaderParameter();
headerParameter.setName("server_host");
headerParameter.setIn("header");
headerParameter.setType("string");
headerParameter.setDefault(finalServerIp);
value.addParameter(headerParameter);
operation.addParameter(headerParameter);
}
}
});
return joinPoint.proceed(args);
}
public static HttpServletRequest getRequest() {
RequestAttributes requestAttributes = RequestAttributes();
return (requestAttributes == null) ? null : ((ServletRequestAttributes) requestAttributes).getRequest(); }
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论