【问题标题】:Swagger Integration with Dropwizard与 Dropwizard 的 Swagger 集成
【发布时间】:2016-01-19 06:50:41
【问题描述】:

我的资源结构是这样的:

@Path("/v1")
class A{
   @Path("/abc") 
   B getB(){}
   @Path("/xyz")
   C getC(){}
}

class Bclass C 中定义了API。你能告诉我在哪里使用@Api()@ApiOperation() 注释。因为在尝试了很多可能性之后,它对我来说并不奏效。它仍然没有出现在 swagger.json 中

同样在 Application 类中,我只需要添加 A,而不是 B 和 C。

【问题讨论】:

标签: java swagger dropwizard swagger-2.0


【解决方案1】:

在类级别使用@Api,在方法级别使用@ApiOperation。您可以将代码相应地修改为以下原型:

import javax.ws.rs.*;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiResponse;
import com.wordnik.swagger.annotations.ApiResponses;

@Path("/v1")
@Api(value = "/v1", description = "Things at A")
class A {

    @GET
    @Path("/abc/{path}") 
    @ApiOperation(value = "Things from B", response = B.class)
    @ApiResponses(value = {
            @ApiResponse(code=HttpStatus.SC_OK, message="OK"),
            @ApiResponse(code=HttpStatus.SC_INTERNAL_SERVER_ERROR, message="Server Error")
    })
    B getB(@PathParam("path") String pathParam) {
        // perform ABC task
   }

   @POST
   @Path("/xyz")
   @ApiOperation(value = "Things from B", response = C.class)
   @ApiResponses(value = {
            @ApiResponse(code=HttpStatus.SC_OK, message="OK")
            @ApiResponse(code=HttpStatus.SC_BAD_REQUEST, message="Bad Request")
   })
    C getC(@ApiParam RequestC requestC) {
       //perform XYZ task
    }
}

在您的应用程序类中,您可以将覆盖的run() 注册调用添加为:

@Override
public void run(SwaggerCustomConfig config, Environment environment) throws Exception {
    environment.jersey().register(A.class);
    ....
}

【讨论】: