可能的和非常簡單的
import com.fasterxml.jackson.core.JsonProcessingException;
import io.swagger.annotations.*;
import io.swagger.jaxrs.Reader;
import io.swagger.models.Swagger;
import io.swagger.util.Json;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.net.HttpURLConnection;
import java.util.HashSet;
import java.util.Set;
@SwaggerDefinition(
info = @Info(
title = "title",
version = "0.2",
description = "description",
termsOfService = "termsOfService",
contact = @Contact(
name = "contact",
url = "http://contact.org",
email = "[email protected]"
),
license = @License(
name = "Apache2",
url = "http://license.org/license"
)
),
host = "host.org",
basePath = "",
schemes = SwaggerDefinition.Scheme.HTTPS
)
public class SwaggerMain {
@Path("/a")
@Api(value = "/a", description = "aaa")
public class A {
@GET
@Path("/getA")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Method for A.")
@ApiResponses(value = {
@ApiResponse(code = HttpURLConnection.HTTP_OK, message = "OK"),
@ApiResponse(code = HttpURLConnection.HTTP_UNAUTHORIZED, message = "Unauthorized"),
@ApiResponse(code = HttpURLConnection.HTTP_NOT_FOUND, message = "Not found"),
@ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = "Internal server problems")
})
public String getA() {
return "Hello, A";
}
}
@Path("/b")
@Api(value = "/b", description = "bbb")
public class B {
@GET
@Path("/getA")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Method for B.")
@ApiResponses(value = {
@ApiResponse(code = HttpURLConnection.HTTP_OK, message = "OK"),
@ApiResponse(code = HttpURLConnection.HTTP_UNAUTHORIZED, message = "Unauthorized"),
@ApiResponse(code = HttpURLConnection.HTTP_NOT_FOUND, message = "Not found"),
@ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = "Internal server problems")
})
public String getA() {
return "Hello, B";
}
}
public static void main(String[] args) {
Set<Class<?>> classes = new HashSet<Class<?>>();
classes.add(SwaggerMain.class);
classes.add(A.class);
classes.add(B.class);
Swagger swagger = new Reader(new Swagger()).read(classes);
try {
System.out.println(Json.mapper().writeValueAsString(swagger));;
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
}
給出JSON:
{
"swagger": "2.0",
"info": {
"description": "description",
"version": "0.2",
"title": "title",
"termsOfService": "termsOfService",
"contact": {
"name": "contact",
"url": "http://contact.org",
"email": "[email protected]"
},
"license": {
"name": "Apache2",
"url": "http://license.org/license"
}
},
"host": "host.org",
"tags": [
{
"name": "a"
},
{
"name": "b"
}
],
"schemes": [
"https"
],
"paths": {
"https://stackoverflow.com/a/getA": {
"get": {
"tags": [
"a"
],
"summary": "Method for A.",
"description": "",
"operationId": "getA",
"produces": [
"application/json"
],
"parameters": [],
"responses": {
"200": {
"description": "OK"
},
"401": {
"description": "Unauthorized"
},
"404": {
"description": "Not found"
},
"500": {
"description": "Internal server problems"
}
}
}
},
"/b/getA": {
"get": {
"tags": [
"b"
],
"summary": "Method for B.",
"description": "",
"operationId": "getA",
"produces": [
"application/json"
],
"parameters": [],
"responses": {
"200": {
"description": "OK"
},
"401": {
"description": "Unauthorized"
},
"404": {
"description": "Not found"
},
"500": {
"description": "Internal server problems"
}
}
}
}
}
}
這工作得很好,謝謝!我通過返回'Swagger'對象來設法服務於端點的swagger json! – user1981275
我喜歡Hugues M.和egorlitvinenko的答案,兩種解決方案都能正常工作。我會接受egorlitvinenko的解決方案,因爲他的解決方案實際上就是我所要求的。 – user1981275