springmvc集成swagger2和lombok

  • swagger

1、maven引入swagger

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

2、编写swagger配置类

package com.swagger.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }
}

3、注入配置类

<bean class="com.swagger.config.SwaggerConfig"/>

4、加入资源映射,访问时能找到swagger-ui.html页面

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

遇到错误解析:

1、swagger nested exception is java.lang.NoSuchMethodError

swagger里面会引用到spring的包,项目里也引用了spring的包,所以造成冲突,找到spring版本相对应的swagger。

springfox版本为2.6.1——>spring 4.2.6.RELEASE

springfox版本为2.7.0——>spring 4.3.8.RELEASE

springfox版本为2.5.0——>spring 4.1.8.RELEASE

springfox版本为2.2.2——>spring 4.1.7.RELEASE

参考文章:http://blog.csdn.net/zhaky/article/details/64906686

  • lombok

简单的说,使用lombok可以让model或者DTO只需要写字段,不用写getter或者setter等方法,引入lombok注解,在编译的时候会自动在model和DTO中写入getter和setter等方法,使代码变得简洁。

    <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.18</version>
        </dependency>

eclipse中支持lombok需要引入jar包。

参考文章:https://yq.aliyun.com/articles/59972

eclispse支持lombok文章:http://blog.csdn.net/cdyjy_litao/article/details/53759928

转载于:https://my.oschina.net/huangyuewu/blog/1550632

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值