提交 0d4bb98e authored 作者: mry's avatar mry

fix(web): 修改了部分bug

上级 602ae2eb
......@@ -44,5 +44,8 @@ public class Environment extends BaseEntity {
@ApiModelProperty("ip,例如(http:www.abc.com)")
private String ip;
@ApiModelProperty("是否默认")
private Boolean isDefault;
}
package org.matrix.database.service;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.matrix.database.entity.Environment;
import com.baomidou.mybatisplus.extension.service.IService;
......@@ -13,4 +15,10 @@ import com.baomidou.mybatisplus.extension.service.IService;
*/
public interface IEnvironmentService extends IService<Environment> {
/**
*
* @param isDefault
* @param wrappers
*/
Boolean setIsDefaultByWrapper(Boolean isDefault, Wrapper wrappers);
}
package org.matrix.database.service.impl;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.matrix.database.entity.Environment;
import org.matrix.database.mapper.EnvironmentMapper;
import org.matrix.database.service.IEnvironmentService;
import org.springframework.stereotype.Service;
import java.util.Optional;
/**
* <p>
* 实例表,项目对应的环境实例,例如:实验室环境,开发环境等 服务实现类
......@@ -17,4 +21,11 @@ import org.springframework.stereotype.Service;
@Service
public class EnvironmentServiceImpl extends ServiceImpl<EnvironmentMapper, Environment> implements IEnvironmentService {
@Override
public Boolean setIsDefaultByWrapper(Boolean isDefault, Wrapper wrappers) {
Environment environment = new Environment();
environment.setIsDefault(isDefault);
return Optional.of(this
.update(environment,wrappers)).get();
}
}
package org.matrix.autotest.swaggerData;
import lombok.Data;
import java.util.List;
/**
* @author mry
*/
@Data
public class InterfaceInformation {
/**
* ip 以及端口信息
*/
private String host;
/**
* /
*/
private String basePath;
/**
* 请求方式
*/
private String request;
/**
* 标签
*/
private String tags;
/**
* 总结
*/
private String summary;
/**
* 方法拼接的url
*/
private String methodUrl;
/**
* 参数
*/
private List<Parameter> parameterAllList;
}
package org.matrix.autotest.swaggerData;
import lombok.Data;
/**
* @author mry
*/
@Data
public class Parameter {
/**
* 名称
*/
private String name;
/**
* 类型
*/
private String type;
/**
* in
*/
private String in;
}
package org.matrix.autotest.swaggerData;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.matrix.database.entity.Environment;
import org.matrix.database.service.IEnvironmentService;
import org.matrix.exception.GlobalException;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
......@@ -29,6 +34,12 @@ import java.util.Set;
@Api(tags = "Swagger接口读取与解析")
public class SwaggerController {
private final IEnvironmentService service;
public SwaggerController(IEnvironmentService service) {
this.service = service;
}
public String loadJson(String url) {
BufferedReader reader;
StringBuilder json = new StringBuilder();
......@@ -43,15 +54,21 @@ public class SwaggerController {
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
throw new GlobalException("请求swagger数据失败");
}
return json.toString();
}
@GetMapping
public Object parameter(String url) {
@SuppressWarnings(value = "all")
List<String> list = new ArrayList();
@PostMapping
@Transactional(rollbackFor = Exception.class)
@ApiOperation(value = "快速添加接口")
public List<InterfaceInformation> parameter(Long projectId, Long id) {
service.setIsDefaultByWrapper(false,
Wrappers.lambdaQuery(Environment.class).eq(Environment::getProjectId, projectId));
service.setIsDefaultByWrapper(true,
Wrappers.lambdaQuery(Environment.class).eq(Environment::getId, id));
Environment environment = service.getById(id);
String url = String.format("%s/v2/api-docs", environment.getIp());
//获得json字符串
String json = loadJson(url);
JSONObject swaggerJson = JSONObject.parseObject(json);
......@@ -65,6 +82,9 @@ public class SwaggerController {
Map<String, String> pathsMaps = (Map<String, String>) paths;
//获取key
Set<String> methodUrls = pathsMaps.keySet();
List<InterfaceInformation> list = new ArrayList<>();
//循环获取每个methodUrl
for (String methodUrl : methodUrls) {
//将paths转成JSON
......@@ -82,33 +102,42 @@ public class SwaggerController {
Map<String, String> objRequestMaps = (Map<String, String>) objRequest;
Object parameters = objRequestMaps.get("parameters");
Object tags = objRequestMaps.get("tags");
Object summary = objRequestMaps.get("summary");
List<String> parameterAllList = new ArrayList<>();
String summary = objRequestMaps.get("summary");
List<Parameter> parameterAllList = new ArrayList<>();
if (parameters != null) {
@SuppressWarnings(value = "all")
List<String> parameterLists = (List<String>) parameters;
Parameter parameter = new Parameter();
for (Object parameterList : parameterLists) {
@SuppressWarnings(value = "unchecked")
Map<String, String> parameterMaps = (Map<String, String>) parameterList;
String name = parameterMaps.get("name");
String type = parameterMaps.get("type");
String in = parameterMaps.get("in");
if (name == null) {
name = "";
}
if (type == null) {
type = "";
}
if (in == null) {
in = "";
}
parameterAllList.add("name=" + name + " type=" + type + " in=" + in);
parameter.setIn(in);
parameter.setName(name);
parameter.setType(type);
parameterAllList.add(parameter);
}
}
list.add("{host=" + host + ", basePath=" + basePath + ", request=" + request + ", name=" + tags + "-" + summary + ", methodUrl=" + methodUrl + ", parameters=" + parameterAllList + "}");
InterfaceInformation interfaceInformation = new InterfaceInformation();
interfaceInformation.setHost(String.valueOf(host));
interfaceInformation.setParameterAllList(parameterAllList);
interfaceInformation.setBasePath(String.valueOf(basePath));
interfaceInformation.setRequest(request);
interfaceInformation.setSummary(summary);
interfaceInformation.setMethodUrl(methodUrl);
interfaceInformation.setTags(String.valueOf(tags));
list.add(interfaceInformation);
}
}
return JSON.toJSON(list);
return list;
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论