[心缘地方]同学录
首页 | 功能说明 | 站长通知 | 最近更新 | 编码查看转换 | 代码下载 | 常见问题及讨论 | 《深入解析ASP核心技术》 | 王小鸭自动发工资条VBA版
登录系统:用户名: 密码: 如果要讨论问题,请先注册。

[备忘]springMvc整合swagger2.3.1版本。

上一篇:[备忘]selenium python 切换tab页
下一篇:[备忘]jre1.8+tomcat8.5,启动报错的问题,jvm.dll拒绝访问

添加日期:2023/2/16 14:21:39 快速返回   返回列表 阅读210次
(1)pom添加


<!--引入swagger-->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.3.1</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.3.1</version>
</dependency>


注意版本号,2.3.1是可以。
2.6或者2.7好像跑不起来,注解写法有问题

(2)springMvc的xml里,添加


<mvc:resources mapping="/swagger-ui.html" location="classpath:/META-INF/resources/" />
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/" />
<bean class="cn.xxxx.index.controller.SwaggerConfig" id="swagger2Config"/>


如果换高版本的话,location的路径好像也不一样,坑爹
swagger要用springMVC的context,所以要放在mvc的xml里,别放spring的xml里。

(3)写一个配置类


package cn.xxx.controller;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2  //启用Swagger2
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .pathMapping("/")
                .select()
                .paths(PathSelectors.regex("/.*"))
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("xxx接口文档")
                .contact("aaa@aa.com")
                .description("这是swaggerUI生成的接口文档")
                .version("1.0.0.0")
                .build();
    }
}


(4)在你的controller类上写一堆注解
@ApiOperation不写httpMethod的话,会自动生成6种,烦死
@ApiResponse的code就是http的状态码,坑爹。瞎写会报错,No matching constant for 123什么的。


package cn.xxx.index.controller;

import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 */
@RestController
@Api(value = "电影Controller", tags = { "电影访问接口" })
@RequestMapping("/film")
public class FilmController {

    /**
     * 添加一个电影数据
     *
     * @param
     * @return
     */
    @ApiOperation(value = "添加一部电影",httpMethod = "POST")
    @RequestMapping("/addFilm")
    @ApiResponses(value = { @ApiResponse(code = 200, message = "成功"), @ApiResponse(code = 500, message = "失败"),
            @ApiResponse(code = 502, response = Integer.class,message = "缺少参数") })
    public int addFilm(@ApiParam("电影名称") @RequestParam("filmName") String filmName,
                               @ApiParam(value = "分数") @RequestParam("score") Short score,
                               @ApiParam("发布时间") @RequestParam(value = "publishTime",required = false) String publishTime,
                               @ApiParam("创建者id") @RequestParam("creatorId") Long creatorId) {


        return 1;
    }

    /**
     * 根据电影名字获取电影
     *
     * @param fileName
     * @return
     */
    @RequestMapping("/getFilms")
    @ApiOperation(value = "根据名字获取电影",httpMethod = "GET")
    @ApiResponses(value = { @ApiResponse(code = 200, message = "成功"), @ApiResponse(code = 500, message = "失败"),
            @ApiResponse(code = 502, message = "缺少参数") })
    public List<String> getFilmsByName(@ApiParam("电影名称") @RequestParam("fileName") String fileName) {
        List<String> films = new ArrayList<>();
        return films;

    }

    /**
     * 根据电影名更新
     *
     * @return
     */
    @RequestMapping("/updateScore")
    @ApiOperation(value = "根据电影名修改分数",httpMethod = "POST")
    @ApiResponses(value = { @ApiResponse(code = 200, message = "成功"), @ApiResponse(code = 500, message = "失败"),
            @ApiResponse(code = 502, message = "缺少参数") })
    public int updateFilmScore(@ApiParam("电影名称") @RequestParam("fileName") String fileName,
                                       @ApiParam("分数") @RequestParam("score") Short score) {
        return 1;
    }

    /**
     * 根据电影名删除电影
     *
     * @param request
     * @return
     */
    @RequestMapping("/delFilm")
    @ApiOperation(value = "根据电影名删除电影",httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "filmName",
            value = "电影名",
            dataType = "String",
            paramType = "query",
            required = true), @ApiImplicitParam(name = "id", value = "电影id", dataType = "int", paramType = "query") })
    public int deleteFilmByNameOrId(HttpServletRequest request) {
        return 1;
    }

    /**
     * 根据id获取电影
     *
     * @param id
     * @return
     */
    @RequestMapping("/{id}")
    @ApiOperation(value ="根据id获取电影",httpMethod = "GET")
    @ApiImplicitParam(name = "id", value = "电影id", dataType = "long", paramType = "path", required = true)
    public String getFilmById(@PathVariable Long id) {
        return "aaa";
    }

    /**
     * 修改整个电影
     *
     * @param film
     * @return
     */
    @RequestMapping("/insertFilm")
    @ApiOperation(value ="插入一部电影",httpMethod = "POST")
    public int insertFilm(@ApiParam("电影实体对象") @RequestBody String film) {
        return 1;
    }
}



(5)
高版本(如2.7或3.0版本)的ApiResourceController类,
    @RequestMapping({"/swagger-resources"})
    @ResponseBody
    ResponseEntity<List<SwaggerResource>> swaggerResources() {
这个@RequestMapping注解是写在类上的,方法上没有,导致不认,会404,折腾好久才发现,所以降到了2.3.1版本,就正常了。
坑爹

(6)增加Knife4j的话,就是换一个皮肤
<1>pom加引用


<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>swagger-bootstrap-ui</artifactId>
    <version>1.9.6</version>
</dependency>


<2>springMvc的xml加
<mvc:resources mapping="doc.html" location="classpath:/META-INF/resources/"/>
<3>用http://ip:port/xxx/doc.html访问
 

评论 COMMENTS
没有评论 No Comments.

添加评论 Add new comment.
昵称 Name:
评论内容 Comment:
验证码(不区分大小写)
Validation Code:
(not case sensitive)
看不清?点这里换一张!(Change it here!)
 
评论由管理员查看后才能显示。the comment will be showed after it is checked by admin.
CopyRight © 心缘地方 2005-2999. All Rights Reserved