我使用Java中的JAX-RS/Jersey开发了自己的REST API。我想将其转换为Swagger基于UI的文档。请问有人能告诉我如何以简单明了的方式精确地进行操作吗?很抱歉,他们网站上给出的步骤对我来说有些含糊。
我使用Java中的JAX-RS/Jersey开发了自己的REST API。我想将其转换为Swagger基于UI的文档。请问有人能告诉我如何以简单明了的方式精确地进行操作吗?很抱歉,他们网站上给出的步骤对我来说有些含糊。
@Api
注解的类才会被 Swagger 扫描。 - Alex Rtrue
。请查看https://github.com/swagger-api/swagger-core/wiki/Annotations-1.5.X的第一部分以获取更多详细信息。 - Ron<plugin>
<groupId>com.sebastian-daschner</groupId>
<artifactId>jaxrs-analyzer-maven-plugin</artifactId>
<version>0.4</version>
<executions>
<execution>
<goals>
<goal>analyze-jaxrs</goal>
</goals>
<configuration>
<!-- Available backends are plaintext (default), swagger and asciidoc -->
<backend>plaintext</backend>
<!-- Domain of the deployed project, defaults to example.com -->
<deployedDomain>example.com</deployedDomain>
</configuration>
</execution>
</executions>
这将通过mvn clean install为您创建swagger json。据我所知,它不需要对web.xml等进行任何操作,因为它是通过字节码分析完成的。package ma.cars.iscraper;
import org.jboss.forge.roaster.Roaster;
import org.jboss.forge.roaster.model.source.*;
import java.util.List;
public class Main {
public static void main(String[] args) {
String originalClassSourceCode = "@Path(\"user\")\n public class SomeClass { @GET\n" +
" @Path(\"{userId}\")\n public Response getUserById() {\n return null; \n}";
System.out.println("Before : \n" + originalClassSourceCode);
JavaClassSource javaClass =
Roaster.parse(JavaClassSource.class,originalClassSourceCode );
String pathValue = null;
// extract Path annotation value
List<AnnotationSource<JavaClassSource>> listAnnotations = javaClass.getAnnotations();
for (AnnotationSource annotation :listAnnotations) {
if (annotation.getName().equals("Path")) {
pathValue = annotation.getStringValue();
}
}
AnnotationSource<JavaClassSource> apiAnnotation = javaClass.addAnnotation("com.wordnik.swagger.annotations.Api");
apiAnnotation.setLiteralValue("\"" + pathValue + "\"") ;
List<MethodSource<JavaClassSource>> methods = javaClass.getMethods();
for (MethodSource<JavaClassSource> method: methods) {
for (AnnotationSource annotation: method.getAnnotations()) {
if (annotation.getName().equals("DELETE") || annotation.getName().equals("GET")
|| annotation.getName().equals("POST") || annotation.getName().equals("PUT")) {
String returnTypeClass = method.getReturnType().getQualifiedName();
AnnotationSource<JavaClassSource> apiOperation = method.addAnnotation("com.wordnik.swagger.annotations.ApiOperation");
apiOperation.setLiteralValue("value", "\"value\"");
apiOperation.setLiteralValue("response", "\"" + returnTypeClass + ".class\"");
}
}
}
System.out.println(javaClass);
}
}
Before :
@Path("user")
public class SomeClass { @GET
@Path("{userId}")
public Response getUserById() {
return null;
}
After :
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;@Path("user")
@Api("user")
public class SomeClass { @GET
@Path("{userId}")
@ApiOperation(value = "value", response = "Response.class")
public Response getUserById() {
return null;
}
Swagger UI是一个无依赖的HTML、Javascript和CSS资源集合,可从符合Swagger规范的API动态生成美观的文档和沙盒。由于Swagger UI没有依赖性,因此您可以在任何服务器环境或本地计算机上托管它。