树洞 [学习贴] 增加 swagger

顺流而下 · 2017年11月14日 · 最后由 顺流而下 回复于 2017年11月14日 · 1300 次阅读

如何增加个 swagger

-- POM.xml

<!-- swagger-mvc -->       <dependency>
           <groupId>io.springfox</groupId>
           <artifactId>springfox-swagger2</artifactId>
           <version>2.4.0</version>
       </dependency>
       <dependency>
           <groupId>io.springfox</groupId>
           <artifactId>springfox-swagger-ui</artifactId>
           <version>2.4.0</version>
       </dependency>
       <dependency>
           <groupId>com.google.guava</groupId>
           <artifactId>guava</artifactId>
           <version>15.0</version>
       </dependency>
       <dependency>
           <groupId>com.fasterxml</groupId>
           <artifactId>classmate</artifactId>
           <version>1.1.0</version>
       </dependency>

-- spring-mvc-config.xml


<!-- swagger -->
<bean class="springfox.documentation.swagger2.configuration.Swagger2DocumentationConfiguration" id="swagger2Config" />

<mvc:resources location="classpath:/META-INF/resources/" mapping="swagger-ui.html" />
<mvc:resources location="classpath:/META-INF/resources/webjars/" mapping="/webjars/**" />

<mvc:resources mapping="/docs/**" location="/WEB-INF/docs/" />

增加controller

package com.hf.kafka.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import springfox.documentation.annotations.ApiIgnore;

/**
 * Home redirection to swagger api documentation
 */
@ApiIgnore
@Controller
public class ApiController {

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home() {
        return "redirect:swagger-ui.html";
    }

    @RequestMapping(value = "/docapi", method = RequestMethod.GET)
    public String docApi() {
        return "redirect:swagger-ui.html";
    }
}

在对应的接口的controller上加上swagger

@Controller
@Api(tags = "Kafka数据解析", value = "/kafka") \\增加的swagger调用
@RequestMapping("/kafka")
public class KafkaController extends BaseController {
##############################################################下面是原来的controller
@RequestMapping(value = "/exec/{type}", method = RequestMethod.GET)
    @ResponseBody
    public synchronized String executeParse(@PathVariable("type") String type){
########################################################################下面是增加的swagger后的
    @ApiOperation(value = "/exec/{type}", httpMethod = "/exec/{type}", response = String.class, notes = "Kafka数")
    @RequestMapping(value = "/exec/{type}", method = RequestMethod.GET)
    @ResponseBody
    public synchronized String executeParse(@ApiParam(value = "二手房:szhome<br>", required = true) @PathVariable("type") String type){


共收到 4 条回复 时间 点赞

这是问题帖还是介绍帖,一脸茫然的

bauul 回复

自己的记录帖子,汗

顺流而下 回复

如何是自己的学习贴, 写的不完整, 还没准备好分享, 可以临时放到"树洞"节点下. 或者保存为草稿. 只有当你写好了, 你可以再转移到其他节点里

好的哈,以后放到树洞下;

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册