Swagger Integration

This wiki writes down the integration between spring-boot and Swagger. Swagger is a powerful restful API framework. It can discover and generate documentation for rest API by using the annotation at code level. This makes the documentation always synchronized with the actual API.

For more of Swagger's capability, please refer to: http://swagger.io

Setup

Here are the steps to set up your spring-boot application to use Swagger. Assume maven is used. 


  1. Add below dependencies to your POM.

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.2.2</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.2.2</version>
    </dependency>

    Swagger does not support spring-boot officially. The support comes from a third-party project springfox.  Here is the official site of springfox: http://springfox.github.io/springfox/


  2. In your main application, add swagger support by simply adding an annotation "@EnableSwagger2"

    @SpringBootApplication
    @EnableSwagger2
    public class Application {
    
        public static void main(String[] args) throws Exception {
            SpringApplication.run(Application.class, args);
        }
    }


    That's all the magic! restart the spring-boot application and visit in your browser: http://localhost:8090/v2/api-docs
    You will see all the restful API returned as a JSON. But that's not so friendly. 

  3. Using the swagger UI.

    NOTE: springfox docket is another option if you just want to use the default swagger ui. And It's not possible to use both options.

    Clone the project from https://github.com/swagger-api/swagger-ui and put the "dist" folder to src/main/resources/static and rename it to "swagger". Reboot the application and then visit: http://localhost:8090/swagger/index.html
    You will see all the registered APIs in your application.


    By default, swagger will generate the link text and description based on you controller's name. For example, "JobController" will be shown as "job-controller" and "Job Controller". 

    You can customize the display text in your code by using annotation, which will be covered in next section.

    You can also customize the UI code.

  4. Using Springfox Docket

    Springfox Docket is another option to show the service UI. Just declare a Docket using "@Bean" as shown below. Note if you are using docket, you got convenience by sacrificing the customizability.

    @SpringBootApplication
    @EnableAutoConfiguration(exclude = {HypermediaAutoConfiguration.class})
    @EnableSwagger2
    public class Application {
    
        public static void main(String[] args) throws Exception {
            SpringApplication.run(Application.class, args);
        }
    
        @Bean
        public Docket newsApi() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .groupName("Process Solution")
                    .apiInfo(apiInfo())
                    .select()
                    .build();
        }
    
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("Cfl Rest API")
                    .description("Powered By Spring Boot")
                    .build();
        }
    }


    And then visit: http://localhost:8090/swagger-ui.html

    For details, please refer to http://springfox.github.io/springfox/docs/current

Swagger annotations

The springfox is smart to generate the documentation by parsing the spring rest service declaration. Most of the time, this is enough. You just need to name your class and method in a meaningful way. But sometimes if you want to generate more detailed information, you can leverage the swagger annotation.

This wiki will only list the typical annotations, for full details please refer to: https://github.com/swagger-api/swagger-core/wiki/Annotations-1.5.X#api

  1. "@Api"From Swagger 2.0, this annotation is used to declared all the operations under the class.
  2. "@ApiOperation"
    This is used to document a rest method.
  3. "@ApiParam"
    This is used to document a parameter in a method.
  4. "@ApiModel"
    This is used to document the model(a java bean) that will be shown in swagger UI.


Here is a sample restful service with swagger:


@RestController
@RequestMapping("/job")
@Api(value = "Spark Job Controller", produces = "application/json", basePath = "/job",
authorizations = {
        @Authorization(value = "sample auth")
}, tags = {"spark","job"})
public class JobController {

    @Autowired
    private JobService jobService;

    @RequestMapping(value = "/all", method = RequestMethod.GET)
    @ApiOperation(value = "Get all running jobs", notes = "this includes spark standalone jobs as well as yarn jobs")
    public Set<Job> all() {
        return jobService.getAllJobs();
    }

    @RequestMapping(method = RequestMethod.PUT)
    public Job add(@RequestBody Job job) {
        return jobService.save(job);
    }

    @RequestMapping(method = RequestMethod.POST)
    public Job update(@RequestBody Job job) {
        return jobService.save(job);
    }

    @RequestMapping(value = "/{jobId:.+}", method = RequestMethod.DELETE)
    @ResponseStatus(HttpStatus.OK)
    public void delete(
            @ApiParam(value = "Spark job id", required = true)
            @PathVariable("jobId") String jobId) {
        jobService.delete(jobId);
    }

    @MessageMapping("/job/start")
    public void start(Job job) throws InterruptedException {
        jobService.start(job);
    }
}
 
 


Declare a model API to be inspected by swagger

Here shows the net effect:

转载于:https://my.oschina.net/chensanti234/blog/666495

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值