几个片段:
package io.swagger.jaxrs.listing; import io.swagger.annotations.ApiOperation; import org.apache.commons.lang3.StringUtils; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.*; @Path("/swagger.{type:json|yaml}") public class ApiListingResource extends BaseApiListingResource { @Context ServletContext context; @GET @Produces({MediaType.APPLICATION_JSON, "application/yaml"}) @ApiOperation(value = "The swagger definition in either JSON or YAML", hidden = true) public Response getListing( @Context Application app, @Context ServletConfig sc, @Context HttpHeaders headers, @Context UriInfo uriInfo, @PathParam("type") String type) { if (StringUtils.isNotBlank(type) && type.trim().equalsIgnoreCase("yaml")) { return getListingYamlResponse(app, context, sc, headers, uriInfo); } else { return getListingJsonResponse(app, context, sc, headers, uriInfo); } } }
package io.swagger.jaxrs.listing; import io.swagger.models.Swagger; import io.swagger.util.Json; import io.swagger.util.Yaml; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.ext.MessageBodyWriter; import javax.ws.rs.ext.Provider; import java.io.IOException; import java.io.OutputStream; import java.lang.annotation.Annotation; import java.lang.reflect.Type; @Provider @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, "application/yaml"}) public class SwaggerSerializers implements MessageBodyWriter<Swagger> { static boolean prettyPrint = false; Logger LOGGER = LoggerFactory.getLogger(SwaggerSerializers.class); public static void setPrettyPrint(boolean shouldPrettyPrint) { SwaggerSerializers.prettyPrint = shouldPrettyPrint; } @Override public boolean isWriteable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) { return Swagger.class.isAssignableFrom(type); } @Override public long getSize(Swagger data, Class type, Type genericType, Annotation[] annotations, MediaType mediaType) { return -1; } @Override public void writeTo(Swagger data, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> headers, OutputStream out) throws IOException { if (mediaType.isCompatible(MediaType.APPLICATION_JSON_TYPE)) { if (prettyPrint) { out.write(Json.pretty().writeValueAsString(data).getBytes("utf-8")); } else { out.write(Json.mapper().writeValueAsString(data).getBytes("utf-8")); } } else if (mediaType.toString().startsWith("application/yaml")) { headers.remove("Content-Type"); headers.add("Content-Type", "application/yaml"); out.write(Yaml.mapper().writeValueAsString(data).getBytes("utf-8")); } else if (mediaType.isCompatible(MediaType.APPLICATION_XML_TYPE)) { headers.remove("Content-Type"); headers.add("Content-Type", "application/json"); out.write(Json.mapper().writeValueAsString(data).getBytes("utf-8")); } } }
<!-- this scans the classes for resources --> <bean id="swaggerConfig" class="io.swagger.jaxrs.config.BeanConfig"> <property name="resourcePackage" value="io.swagger.sample.resource"/> <property name="version" value="1.0.0"/> <property name="host" value="localhost:8080/swagger-java-cxf-sample"/> <property name="basePath" value="/api"/> <property name="title" value="Swagger Petstore"/> <property name="description" value="This is a app."/> <property name="contact" value="[email protected]"/> <property name="license" value="Apache 2.0"/> <property name="licenseUrl" value="http://www.apache.org/licenses/LICENSE-2.0.html"/> <!-- <property name="filterClass" value="ApiAuthorizationFilterImpl"/> --> <property name="scan" value="true"/> </bean>
basePath 是定位到项目的前提下,访问谁
var url = window.location.search.match(/url=([^&]+)/); if (url && url.length > 1) { url = decodeURIComponent(url[1]); } else { url = "http://localhost:8080/swagger-java-cxf-sample/api/swagger.json"; }
访问谁,访问哪个路径
!IMPORTANT
这个是基于jax-rs 项目整合的
时间: 2024-10-05 10:04:02