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

1、首先pom引入相应jar:

<!-- swagger start -->
		<dependency>
			<groupId>io.swagger</groupId>
			<artifactId>swagger-core</artifactId>
			<version>1.5.16</version>
		</dependency>
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>2.7.0</version>
		</dependency>
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>2.7.0</version>
		</dependency>

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<String, Object> map = new HashMap<String, Object>();
		map.put("result", true);
		response.getWriter().write(JSON.toJSONString(ReturnEntityUtils.SUCCESS_RETURN.put(map)));
	}
}
3、生成API

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

<script src='webjars/springfox-swagger-ui/lang/translator.js' type='text/javascript'></script>
  <script src='webjars/springfox-swagger-ui/lang/zh-cn.js' type='text/javascript'></script>



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值