import com.google.common.base.Optional; import com.google.common.base.Strings; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import io.swagger.models.Model; import io.swagger.models.Path; import io.swagger.models.Swagger; import io.swagger.models.Tag; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.env.Environment; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.util.UriComponents; import springfox.documentation.annotations.ApiIgnore; import springfox.documentation.service.Documentation; import springfox.documentation.spring.web.DocumentationCache; import springfox.documentation.spring.web.PropertySourcedMapping; import springfox.documentation.spring.web.json.Json; import springfox.documentation.spring.web.json.JsonSerializer; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.mappers.ServiceModelToSwagger2Mapper; import javax.servlet.http.HttpServletRequest; import java.util.List; import java.util.Map; import static com.google.common.base.Strings.isNullOrEmpty; import static org.springframework.util.MimeTypeUtils.APPLICATION_JSON_VALUE; import static springfox.documentation.swagger.common.HostNameProvider.componentsFrom; @Controller @ApiIgnore public class Swagger2Controller { public static final String DEFAULT_URL = "/v2/api-docs2"; private static final Logger LOGGER = LoggerFactory.getLogger(Swagger2Controller.class); private static final String HAL_MEDIA_TYPE = "application/hal+json"; private final String hostNameOverride; private final DocumentationCache documentationCache; private final ServiceModelToSwagger2Mapper mapper; private final JsonSerializer jsonSerializer; @Autowired public Swagger2Controller( Environment environment, DocumentationCache documentationCache, ServiceModelToSwagger2Mapper mapper, JsonSerializer jsonSerializer) { this.hostNameOverride = environment.getProperty( "springfox.documentation.swagger.v2.host", "DEFAULT"); this.documentationCache = documentationCache; this.mapper = mapper; this.jsonSerializer = jsonSerializer; } @RequestMapping( value = DEFAULT_URL, method = RequestMethod.GET, produces = {APPLICATION_JSON_VALUE, HAL_MEDIA_TYPE}) @PropertySourcedMapping( value = "${springfox.documentation.swagger.v2.path}", propertyKey = "springfox.documentation.swagger.v2.path") @ResponseBody public ResponseEntity<Json> getDocumentation( @RequestParam(value = "group", required = false) String swaggerGroup, HttpServletRequest servletRequest, @RequestParam(value = "tagString", required = false) String tagString, @RequestParam(value = "pathString", required = false) String pathString, @RequestParam(value = "definitionString", required = false) String definitionString) { String groupName = Optional.fromNullable(swaggerGroup).or(Docket.DEFAULT_GROUP_NAME); Documentation documentation = documentationCache.documentationByGroup(groupName); if (documentation == null) { LOGGER.warn("Unable to find specification for group {}", groupName); return new ResponseEntity<Json>(HttpStatus.NOT_FOUND); } Swagger swagger = mapper.mapDocumentation(documentation); UriComponents uriComponents = componentsFrom(servletRequest, swagger.getBasePath()); swagger.basePath(Strings.isNullOrEmpty(uriComponents.getPath()) ? "/" : uriComponents.getPath()); if (isNullOrEmpty(swagger.getHost())) { swagger.host(hostName(uriComponents)); } List<Tag> tags = Lists.newArrayList(); tags.add(swagger.getTag(tagString));//aa-09-controller swagger.setTags(tags); Path path = swagger.getPath(pathString); ///aa09/addAa09 swagger.setTags(tags); Map<String, Path> paths = Maps.newHashMap(); paths.put(pathString, path); swagger.setPaths(paths); Map<String, Model> definitions = Maps.newHashMap(); definitions.put(definitionString, swagger.getDefinitions().get(definitionString));//Aa09 swagger.setDefinitions(definitions); // swagger.setp return new ResponseEntity<Json>(jsonSerializer.toJson(swagger), HttpStatus.OK); } private String hostName(UriComponents uriComponents) { if ("DEFAULT".equals(hostNameOverride)) { String host = uriComponents.getHost(); int port = uriComponents.getPort(); if (port > -1) { return String.format("%s:%d", host, port); } return host; } return hostNameOverride; } }
修改swagger 返回json,选取你部门,直接转到postman上
最新推荐文章于 2022-08-05 11:51:46 发布