spring boot中swagger2使用

 1.pom.xml中添加

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

 

2.添加SwaggerConfig類

import io.swagger.annotations.ApiOperation;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
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.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * swagger配置類
 *
 * @author shanggq
 * 
 */
@Configuration
@EnableSwagger2
@ConditionalOnProperty(prefix = "leshare", name = "swagger-open", havingValue = "true")
public class SwaggerConfig {


    @Bean
    public Docket createRestApi() {

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))                         //這裏採用包含註解的方式來肯定要顯示的接口
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("LeShare Doc")
                .description("LeShare Api文檔")
                .termsOfServiceUrl("http://www.test.com")
                .contact("shanggq")
                .version("1.0")
                .build();
    }

}

3.啓動類添加@EnableSwagger2

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages = ("com.lh.service.client"))
@EnableSwagger2
public class ActivityApplication {

	public static void main(String[] args) {
		SpringApplication.run(ActivityApplication.class, args);
	}
}

4.ActivityController

import com.lh.api.ApiResult;
import com.lh.bo.ActivityBo;
import com.lh.entity.Activity;
import com.lh.service.IActivityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/activity")
@Api(value = "活動接口")
public class ActivityController {


    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private IActivityService iActivityService;

    /**
     * 活動列表查詢
     *
     * @param merchantId
     * @param actType
     * @return
     */
    @RequestMapping(value = "/{merchantId}/getActList", method = RequestMethod.GET)
    @ApiOperation(value = "獲取活動列表", notes = "必須須要帶merchantId過來")
    public ApiResult getActList(@PathVariable("merchantId") Integer merchantId,
                                @Nullable @RequestParam Integer actType) {
        
        List<Activity> activityList = iActivityService.selectActivityList(merchantId, actType);

        return ApiResult.build(activityList);
    }

5.訪問localhost:8080/swagger-ui.html顯示效果

6.訪問localhost:8080/swagger-ui.html顯示404,解決方法

      項目配置與swagger衝突了, 最後發如今 application.yml 中把html

#  freemarker:
#    cache: false
#    enabled: true
#  mvc:
#    static-path-pattern: /static/**

註釋掉就能夠了。java

相關文章
相關標籤/搜索