java - 如何为 swagger 注释 Play 2 webapp 模型?

标签 java rest playframework playframework-2.0 swagger

我是 Android/java 开发人员,刚接触 Play2 框架。我正在尝试使用 swagger 为我的 RESTful API 生成文档.

我已经设法将 swagger 包含到我的 Play2 webapp 中并生成简单的 api-docs.json。我唯一缺少的部分是模型描述。我相应地在/controllers 和/models 中有用户 Controller 和用户模型。

@Api(value = "/user", listingPath = "/api-docs.{format}/user", description = "User registration and authorisation")
public class User extends Controller {

    @POST
    @ApiOperation(value = "Create user", notes = "Used to register new user.")
    @ApiParamsImplicit(@ApiParamImplicit(name = "body", value = "Created user object", required = true, dataType = "User", paramType = "body"))
    @BodyParser.Of(BodyParser.Json.class)
    public static Result createUser() {
        JsonNode json = request().body().asJson();
        ObjectNode result = Json.newObject();
        JsonNode body = json.findPath("body");
        if(body.isMissingNode()) {
            result.put("status", "KO");
            result.put("message", "Missing parameter [body]");
            return badRequest(result);
        }

        JsonNode name = body.get("name");

        if(name == null) {
            result.put("status", "KO");
            result.put("message", "Missing parameter [body.name]");
            return badRequest(result);
        }

        result.put("status", "OK");
        result.put("message", "Hello " + name.getTextValue());
        return ok(result);
    }

}

我已经尝试完全按照 example 中的注释模型

@XmlRootElement(name = "User")
public class User {
    public String name;

    @XmlElement(name = "name")
    public String getName() {
        return name;
    }
}

结果是:

{
    apiVersion: "beta",
    swaggerVersion: "1.1",
    basePath: "http://localhost:9000",
    resourcePath: "/user",
    apis: [
        {
            path: "/user",
            description: "User registration and authorisation",
                operations: [
                {
                    httpMethod: "POST",
                    summary: "Create user",
                    notes: "Used to register new user.",
                    responseClass: "void",
                    nickname: "createUser",
                    parameters: [
                        {
                        name: "body",
                        description: "Created user object",
                        paramType: "body",
                        required: true,
                        allowMultiple: false,
                        dataType: "User"
                        }
                    ]
                }
            ]
        }
    ]
}

有什么想法吗?

最佳答案

我自己找到了答案。 看起来 swagger 在用作返回值时承认模型,即 responseClass:

@ApiOperation(  value = "Find quiz by ID",
        notes = "Returns a quiz with given ID",
        responseClass = "models.Quiz" )
@ApiErrors(     value = {
        @ApiError(code = 400, reason = "Invalid ID supplied"),
        @ApiError(code = 404, reason = "Quiz not found") })
public static Result getQuizById(
        @ApiParam(value = "ID of question that needs to be fetched", required = true) @PathParam("quizId")
        String quizId) {

    ObjectNode result = Json.newObject();
    return ok(result);
}

简单地添加这样的方法使得相应的模型出现在api-docs.json中。

关于java - 如何为 swagger 注释 Play 2 webapp 模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15051456/

相关文章:

java - 玩! 2 Framework - 添加Java Mongo驱动

java - ProcessBuilder对一个jar包中的一个可执行jar作为资源

java - 具有流畅接口(interface)和继承的自绑定(bind)泛型类型

java - 当用户摆弄 html 范围 slider 时如何更改内容(文本)?

api - paypal rest api 测试信用卡号码

java - 内部 Web 服务的最佳实践

javascript - 在 AngularJS 中使用 Wcf 数据服务

java - 迁移到 2.4 后 Play 的路由文件出现问题

scala - 如何模拟 Scala 单例对象?

java - 从静态方法获取 Application 实例