利用idea在项目中配置swagger

第一步:在pom.xml文件中配置依赖



	io.springfox
	springfox-swagger2
	2.6.1



	io.springfox
	springfox-swagger-ui
	2.6.1

第二步:在配置类型新建两个类,分布为:SwaggerConfig.java和SwaggerUiConfig.java

    1、SwaggerConfig.java类

public class SwaggerConfig {

    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.bigdata.web.controller"))
                .paths(PathSelectors.any())
                .build();
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("大数据api文档")
                .description("简单优雅的restful风格")
                .termsOfServiceUrl("http://localhost:8080/swagger-ui.html")
                .version("1.0")
                .build();
    }

2、SwaggerUiConfig.java类

@Configuration
@EnableSwagger2
public class Swagger2UIConfig {
	/**获取API标题 */
	public static final String TITLE = "大数据api文档";
	/**网站描述 */
	public static final String DESCRIPTION = "简单优雅的restful风格";
	/**网站访问地址 */
	public static final String SERVICEURL = "http://localhost:8081/swagger-ui.html";
	/**扫描接口包路径,多个用逗号分隔 */
	// public static final String basePackages = "com.bigdata.web.controller";
	public static final String basePackages = "com.bigdata.web.controller,com.j2ee.action";

	/**
	 * Swagger2创建Docket的Bean 
	 *@return Docket 
	 */
	@Bean
	public Docket createRestApi() {
		return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
				.apis(Swagger2UIConfig.basePackage(basePackages)).paths(PathSelectors.any()).build();
	}

	public static Predicate basePackage(final String basePackage) {
		return new Predicate() {
			@Override
			public boolean apply(RequestHandler input) {
				return declaringClass(input).transform(handlerPackage(basePackage)).or(true);
			}
		};
	}

	/**
	 *  处理包路径配置规则,支持多路径扫描匹配以逗号隔开 
	 *  @param basePackage 扫描包路径 
	 *  @return Function
	 */
	private static Function, Boolean> handlerPackage(final String basePackage) {
		return new Function, Boolean>() {
			@Override
			public Boolean apply(Class input) {
				for (String strPackage : basePackage.split(",")) {
					boolean isMatch = input.getPackage().getName().startsWith(strPackage);
					if (isMatch) {
						return true;
					}
				}
				return false;
			}
		};
	}

	private static Optional> declaringClass(RequestHandler input) {
		return Optional.fromNullable(input.declaringClass());
	}

	/** 
	  * Swagger2创建该Api的基本信息 * 
	  * @return ApiInfo 
	 */
	@Bean
	public ApiInfo apiInfo() {
		return new ApiInfoBuilder().title(TITLE).description(DESCRIPTION)
				.termsOfServiceUrl(SERVICEURL).version("1.0").build();
	}
}

第三步:编写Swagger的测试接口

@RestController
@Api(value = "Test",description = "测试接口详情")
@RequestMapping("/testSwagger")
public class TestSwaggerController {
		@Autowired
		private UserDataUpdateService service;

        @ApiOperation(value = "获取helloWorld", notes = "简单SpringMVC请求")
        @PostMapping("/")
        Swagger home() {
            Swagger s = new Swagger();
            s.setId("0002");
            s.setName("张三");
            return s;
        }
  
        }

 

 

 

 

 

 

 

你可能感兴趣的:(JAVA)