java项目Swagger工具使用方法

swagger对应后端开发而言,几乎就是一个神器,调试后端api接口非常好用的工具,我们项目是分布式微服务架构,利用Spring Cloud技术,那么如何在java项目中配置好,分为下面几个步骤:
1、在独自的服务模块启动类中加载
以下是启动数据库连接、FeignClientConfig、CommonConfig、SwaggerConfig等整体服务信息。

package xxxxx.test;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.leader.LeaderLatch;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.integration.annotation.IntegrationComponentScan;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.session.web.http.CookieSerializer;
import org.springframework.session.web.http.DefaultCookieSerializer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;

/**
 * Created by zhangsan on 2017/4/11.
 */
@SpringBootApplication
@ComponentScan(value = {"xxxx.*"},
        excludeFilters = {@ComponentScan.Filter(type = FilterType.REGEX,pattern = {"xxxxx.order.*","xxxx.common.base.*"})})
@EnableAspectJAutoProxy
@EnableDiscoveryClient
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
@IntegrationComponentScan
@EnableFeignClients
@EnableCaching
@EnableRabbit
public class PscmQualityTestApplication {


    @Value("${leaderLatch.index}")
    private String leaderLatchIndex;


    public static void main(String[] args) {
        Object[] sources = {PscmQualityTestApplication.class, DataSourceConfig.class, FeignClientConfig.class, SwaggerConfig.class, HttpSessionConfig.class, CommonConfig.class, SecurityConfig.class};
        SpringApplication.run(sources, args);
    }

2、上面的启动类包括了Swagger工具配置类:

package test;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
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;

/**
 * Created by zhangsan on 2017/8/10.
 */
@Configuration
@Profile("!production")
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("aa.bb.cc"))//扫描aa.bb.cc路径下的所有
                .paths(PathSelectors.any())
                .build();
    }
}

3、在pom文件中,引入依赖的jar包:
第一个引入swagge核心的jar包,第二个引入swagger的UI

 
            io.springfox
            springfox-swagger2
            2.7.0
        
        
            io.springfox
            springfox-swagger-ui
            2.7.0
        

4、启动服务成功
5、访问服务中的所有api接口
java项目Swagger工具使用方法_第1张图片

你可能感兴趣的:(Swagger)