运用spring集成swagger的springfox实现swagger API生成

1、首先pom引入相应jar:


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

2、swagger配置类注入

package com.loan.core.util;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.google.common.base.Predicate;
import com.google.common.base.Predicates;

import io.swagger.annotations.Api;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * Created by max on 8/16/16.
 *
 */
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {

	 @Bean
	 public Docket buildDocket(){
	  return new Docket(DocumentationType.SWAGGER_2)
	    .apiInfo(buildApiInf())
	    .select()
	    .apis(RequestHandlerSelectors.basePackage("com.loan"))
	    .paths(PathSelectors.any())
	    .build();
	 }
	 
	 private ApiInfo buildApiInf(){
	  return new ApiInfoBuilder()
	     .title("title")
	     .description("接口文档")
	     .build();
	 
	 }
	 

}



3、controller中写对应API注解

package com.loan.core.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.loan.fore.util.ReturnEntityUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import rop.thirdparty.com.alibaba.fastjson.JSON;
@Api(value="TestController",tags={"TestController"},description="测试")
@Controller
@RequestMapping("/core/test")
public class TestController {
	@RequestMapping("getBaseInfo")
	@ApiOperation(value = "计算器获取相关信息", httpMethod = "POST", notes = 
	"	  描述:加载文章详情\r\n" + 
			"	  HTTP Method: POST \r\n" + 
			"	  接口地址:http://ip:port/creditAPP/core/test/getBaseInfos\r\n" + 
			"	  \r\n" + 
			"	  @param request\r\n" + 
			"	  @param response\r\n" + 
			"	  @throws IOException\r\n" + 
			"	  \"remarks\": \"No remarks\" \r\n" + 
			"	  \"returnReason\": \"OK\" 成功 \r\n" + 
			"	  \"returnStatus\": 200 返回成功 \r\n" + 
			"	  \"returnTotal\": returnInformation的个数 \r\n" + 
			"	  \"returnInformation\": \r\n" + 
			"	  {\"result\":boolean-true:成功 false:失敗} \r\n" + 
			"	  Author:*** \r\n" + 
			"	  Date: 2017年10月23日 下午3:34:56")
	@ResponseBody
	public void getBaseInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Map map = new HashMap();
		map.put("result", true);
		response.getWriter().write(JSON.toJSONString(ReturnEntityUtils.SUCCESS_RETURN.put(map)));
	}
}
3、生成API

运用spring集成swagger的springfox实现swagger API生成_第1张图片

注:如果需要汉化,在jar包中swagger-ui.html中的引入:


  



你可能感兴趣的:(运用spring集成swagger的springfox实现swagger API生成)