springcloud后端之路JavaWeb

Java-Swagger

2018-05-18  本文已影响496人  辛小二

1、swagger学习

Swagger定义
Swagger同类工具
Swagger和web项目结合
Swagger在公司项目中如何应用

2、Swagger定义

2.1 Swagger子项目

Swagger子项目

其实swagger是一个比较大的项目,就像spring一样,他下面有很多模块,我们现在只需要使用它的UI模。

3、相关学习博客

4、swagger和springmvc整合

4.1 依赖管理

 <!-- swagger-mvc -->
    <dependency>
      <groupId>com.mangofactory</groupId>
      <artifactId>swagger-springmvc</artifactId>
      <version>1.0.2</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.4.2</version>
    </dependency>
    <!-- swagger-mvc -->

4.2 Swagger配置

Swagger的配置最终就是配置一个config类,通过Java编码的方式实现配置:

@Configuration
@EnableSwagger
public class SwaggerConfig {

    private SpringSwaggerConfig springSwaggerConfig;

    /**
     * Required to autowire SpringSwaggerConfig
     */
    @Autowired
    public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig)
    {
        this.springSwaggerConfig = springSwaggerConfig;
    }

    /**
     * Every SwaggerSpringMvcPlugin bean is picked up by the swagger-mvc
     * framework - allowing for multiple swagger groups i.e. same code base
     * multiple swagger resource listings.
     */
    @Bean
    public SwaggerSpringMvcPlugin customImplementation()
    {
        return new SwaggerSpringMvcPlugin(this.springSwaggerConfig)
                .apiInfo(apiInfo())
                .includePatterns(".*?");
    }

    private ApiInfo apiInfo()
    {
        ApiInfo apiInfo = new ApiInfo(
                "My Apps API Title",
                "My Apps API Description",
                "My Apps API terms of service",
                "My Apps API Contact Email",
                "My Apps API Licence Type",
                "My Apps API License URL");
        return apiInfo;
    }
}

这个是个模板,里面内容可以自己定义,修改基本上是对apiinfo自定义数据。
在spring中注入Bean:
<context:component-scan base-package="net.shopin.swagger"/>

swagger基本就算是配置好了,剩下的事情就是接口编写和展示了。

引入swagger-UI组件
更改项目文档路径

更改项目文档路径

修改index.html文件中的路径:默认是从连接http://petstore.swagger.io/v2/swagger.json获取API的JSON,我们改为 http://{ip}:{port}/{projectName}/api-docs 的形式,也就ip:端口号/项目名/api-docs

配置Xml

最后就是对controller的书写:

@Api(value = "product", description = "商品管理", produces = MediaType.APPLICATION_JSON_VALUE)
@Controller
public class ProductController {

    @Autowired
    private ProductService productService;

    @ApiOperation(value = "获得商品信息", notes = "获取商品信息(用于数据同步)", httpMethod = "POST", produces=MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses(value = {@ApiResponse(code = 200, message = "商品信息"),
        @ApiResponse(code = 201, message = ErrorType.errorCheckToken + "(token验证失败)",  response=String.class),
        @ApiResponse(code = 202, message = ErrorType.error500 + "(系统错误)",response = String.class)})
    @RequestMapping(value = RestUrl.getProduct, method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO<Products> getProduct(@ApiParam(value = "json参数",  required = true) @RequestBody BaseParam param)throws Exception {
        return productService.getProduct(param);
    }
}

当然你还需要是配置XML

ぐlovefor丶  11:21:08
 <!-- swagger 集成-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.6.1</version>
        </dependency>
配置XML的文件

代码效果

WechatIMG1153.png
实现效果
WechatIMG1156.png

备注:

swagger 在不同语言中的应用,可以直接在网页上书写并生成Demo地址

上一篇 下一篇

猜你喜欢

热点阅读