SwaggerUI自动生成API文档(SwaggerUI+SpringBoot)



SwaggerUI+SpringBoot

测试 访问地址  http://localhost:8080/v1/swagger-ui.html#/
下面是具体配置

Maven依赖
   
   
<!-- Swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
<!-- END Swagger -->

properties
   
   
server.servlet-path=/v1

Config
   
   
package com.unioncast.db.config;
 
import static com.google.common.base.Predicates.or;
import static springfox.documentation.builders.PathSelectors.regex;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.ResponseEntity;
 
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
 
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
 
/**
* SwaggerConfig
*/
@Configuration
public class SwaggerConfig {
 
@Value("${server.servlet-path}")
private String pathMapping;
 
private ApiInfo initApiInfo() {
ApiInfo apiInfo = new ApiInfo("数据存储项目 Platform API", // 大标题
initContextInfo(), // 简单的描述
"1.0.0", // 版本
"服务条款", "后台开发团队", // 作者
"The Apache License, Version 2.0", // 链接显示文字
"http://www.baidu.com"// 网站链接
);
return apiInfo;
}
 
private String initContextInfo() {
StringBuffer sb = new StringBuffer();
sb.append("REST API 设计在细节上有很多自己独特的需要注意的技巧,并且对开发人员在构架设计能力上比传统 API 有着更高的要求。").append("<br/>")
.append("以下是本项目的API文档");
return sb.toString();
}
 
@Bean
public Docket restfulApi() {
System.out.println("http://localhost:8080" + pathMapping + "/swagger-ui.html");
return new Docket(DocumentationType.SWAGGER_2).groupName("RestfulApi")
// .genericModelSubstitutes(DeferredResult.class)
.genericModelSubstitutes(ResponseEntity.class).useDefaultResponseMessages(true).forCodeGeneration(false)
.pathMapping(pathMapping) // base,最终调用接口后会和paths拼接在一起
.select().paths(doFilteringRules()).build().apiInfo(initApiInfo());
// .select().paths(Predicates.not(PathSelectors.regex("/error.*"))).build().apiInfo(initApiInfo());
 
}
 
/**
* 设置过滤规则 这里的过滤规则支持正则匹配    //若有静态方法在此之前加载就会报集合相关的错误.
*
* @return
*/
private Predicate<String> doFilteringRules() {
// return Predicates.not(PathSelectors.regex("/error.*"));
// return or(regex("/hello.*"), regex("/rest/adxSspFinanceManagement.*"));//success
return or(regex("/hello.*"), regex("/rest.*"));
}
}

run
   
   
package com.unioncast.db;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
import springfox.documentation.swagger2.annotations.EnableSwagger2;
 
@SpringBootApplication
@EnableSwagger2
public class DbRestApiApplication {
 
// private static final Logger LOG = LogManager.getLogger(DbRestApiApplication.class);
 
public static void main(String[] args) {
SpringApplication.run(DbRestApiApplication.class, args);
}
 
// @Bean
// public WebMvcConfigurerAdapter adapter() {
// return new WebMvcConfigurerAdapter() {
// @Override
// public void addInterceptors(InterceptorRegistry registry) {
// super.addInterceptors(registry);
// registry.addInterceptor(new HandlerInterceptor() {
// @Override
// public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
// throws Exception {
// // TODO 计划把controller里那些日志转移到这里进行输出
// LOG.info("preHandle");
// return true;
// }
//
// @Override
// public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
// ModelAndView modelAndView) throws Exception {
// LOG.info("postHandle");
// }
//
// @Override
// public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
// Object handler, Exception ex) throws Exception {
// LOG.info("afterCompletion");
// }
// }).addPathPatterns("/**");
// }
// };
// }
}


case
   
   
package com.unioncast.db.api.rest.adx;
 
import java.net.URI;
import java.util.List;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.UriComponentsBuilder;
 
import com.unioncast.common.adx.model.AdxSspFinanceManagement;
import com.unioncast.common.page.Pagination;
import com.unioncast.common.restClient.RestResponse;
import com.unioncast.db.api.rest.GeneralController;
import com.unioncast.db.rdbms.core.exception.DaoException;
import com.unioncast.db.rdbms.core.service.adxDBService.AdxSspFinanceManagementService;
 
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
 
@Api(value = "API - AdxSspFinanceManagementController")
@RestController
@RequestMapping("/rest/adxSspFinanceManagement")
public class AdxSspFinanceManagementController extends GeneralController {
 
private static final Logger LOG = LoggerFactory.getLogger(AdxSspFinanceManagementController.class);
 
@Autowired
private AdxSspFinanceManagementService adxSspFinanceManagementService;
 
/**
* 查找所有
*
* @date 2016年10月19日 下午1:30:29
*
* @return
* @throws DaoException
*/
@ApiOperation(value = "查找所有", httpMethod = "POST", response = RestResponse.class)
@RequestMapping(value = "/findAll", method = RequestMethod.POST)
public RestResponse findAll() throws DaoException {
RestResponse restResponse = new RestResponse();
restResponse.setStatus(RestResponse.OK);
AdxSspFinanceManagement[] adxSspFinanceManagements = adxSspFinanceManagementService.findAll();
LOG.info("adxSspFinanceManagements:{}", (Object) adxSspFinanceManagements);
restResponse.setResult(adxSspFinanceManagementService.findAll());
return restResponse;
}
 
/**
* 分页条件查找
*
* @date 2016年9月29日 下午6:59:46
*
* @return
* @throws DaoException
*/
@ApiOperation(value = "分页条件查找", httpMethod = "POST", response = RestResponse.class)
@ApiImplicitParams({
@ApiImplicitParam(name = "currentPage", required = true, dataType = "Integer", paramType = "path"),
@ApiImplicitParam(name = "pageSize", required = true, dataType = "Integer", paramType = "path"),
@ApiImplicitParam(name = "adxSspFinanceManagement", required = true, dataType = "AdxSspFinanceManagement", paramType = "body") })
@RequestMapping(value = "/page/{currentPage}/{pageSize}", method = RequestMethod.POST)
public RestResponse page(@RequestBody AdxSspFinanceManagement adxSspFinanceManagement,
@PathVariable Integer currentPage, @PathVariable Integer pageSize) throws DaoException {
LOG.info("adxSspFinanceManagementCondition:{}", adxSspFinanceManagement);
LOG.info("currentPage:{}", currentPage);
LOG.info("pageSize:{}", pageSize);
RestResponse restResponse = new RestResponse();
restResponse.setStatus(RestResponse.OK);
Pagination<AdxSspFinanceManagement> pagination = adxSspFinanceManagementService.page(adxSspFinanceManagement,
currentPage, pageSize);
LOG.info("pagination:{}", pagination);
restResponse.setResult(adxSspFinanceManagementService.page(adxSspFinanceManagement, currentPage, pageSize));
return restResponse;
}
 
/**
* 根据id查找
*
* @date 2016年9月29日 下午7:09:03
*
* @param id
* @return
* @throws DaoException
*/
@ApiOperation(value = "根据id查找", httpMethod = "POST", response = RestResponse.class)
@ApiImplicitParams({ @ApiImplicitParam(name = "id", required = true, dataType = "Integer", paramType = "path") })
@RequestMapping(value = "/findById/{id}", method = RequestMethod.POST)
public RestResponse findById(@PathVariable Long id) throws DaoException {
LOG.info("id:{}", id);
RestResponse restResponse = new RestResponse();
restResponse.setStatus(RestResponse.OK);
AdxSspFinanceManagement adxSspFinanceManagement = adxSspFinanceManagementService.findById(id);
LOG.info("adxSspFinanceManagement:{}", adxSspFinanceManagement);
restResponse.setResult(adxSspFinanceManagementService.findById(id));
return restResponse;
}
 
/**
* 增加
*
* @date 2016年10月21日 下午5:02:59
*
* @param adxSspFinanceManagement
* @param uriComponentsBuilder
* @return
* @throws DaoException
*/
@ApiOperation(value = "增加", httpMethod = "POST", response = RestResponse.class)
@ApiImplicitParams({
@ApiImplicitParam(name = "adxSspFinanceManagement", required = true, dataType = "AdxSspFinanceManagement", paramType = "body") })
@RequestMapping(value = "/add", method = RequestMethod.POST)
public ResponseEntity<RestResponse> add(@RequestBody AdxSspFinanceManagement adxSspFinanceManagement,
UriComponentsBuilder uriComponentsBuilder) throws DaoException {
LOG.info("adxSspFinanceManagement:{}", adxSspFinanceManagement);
RestResponse restResponse = new RestResponse();
restResponse.setStatus(RestResponse.OK);
Long id = adxSspFinanceManagementService.save(adxSspFinanceManagement);
LOG.info("id:{}", id);
restResponse.setResult(id);
HttpHeaders httpHeaders = new HttpHeaders();
URI uri = uriComponentsBuilder.path("/rest/adxSspFinanceManagement/findById/").path(String.valueOf(id)).build()
.toUri();
httpHeaders.setLocation(uri);
return new ResponseEntity<RestResponse>(restResponse, httpHeaders, HttpStatus.CREATED);
}
 
/**
* 批量增加
*
* @date 2016年10月21日 下午5:02:47
*
* @param adxSspFinanceManagements
* @return
* @throws DaoException
*/
@ApiOperation(value = "批量增加", httpMethod = "POST", response = RestResponse.class)
@ApiImplicitParams({
@ApiImplicitParam(name = "adxSspFinanceManagements", required = true, dataType = "List<AdxSspFinanceManagement>", paramType = "body") })
@RequestMapping(value = "/batchAdd", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
public RestResponse batchAdd(@RequestBody List<AdxSspFinanceManagement> adxSspFinanceManagements)
throws DaoException {
RestResponse restResponse = new RestResponse();
restResponse.setStatus(RestResponse.OK);
List<Long> ids = adxSspFinanceManagementService.batchAdd(adxSspFinanceManagements);
LOG.info("ids:{}", ids);
restResponse.setResult(ids);
return restResponse;
}
 
/**
* 修改
*
* @date 2016年10月21日 下午5:03:38
*
* @param adxSspFinanceManagement
* @throws DaoException
*/
@ApiOperation(value = "修改", httpMethod = "POST", response = RestResponse.class)
@ApiImplicitParams({
@ApiImplicitParam(name = "adxSspFinanceManagement", required = true, dataType = "AdxSspFinanceManagement", paramType = "body") })
@RequestMapping(value = "/update", method = RequestMethod.POST)
public void update(@RequestBody AdxSspFinanceManagement adxSspFinanceManagement) throws DaoException {
LOG.info("adxSspFinanceManagement:{}", adxSspFinanceManagement);
adxSspFinanceManagementService.updateNotNullField(adxSspFinanceManagement);
}
 
/**
* 删除
*
* @date 2016年9月29日 下午7:27:57
*
* @param id
* @return 删除了多少条
* @throws DaoException
*/
@ApiOperation(value = "删除", httpMethod = "POST", response = RestResponse.class)
@ApiImplicitParams({ @ApiImplicitParam(name = "id", required = true, dataType = "Long", paramType = "path") })
@RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
public RestResponse delete(@PathVariable Long id) throws DaoException {
RestResponse restResponse = new RestResponse();
restResponse.setStatus(RestResponse.OK);
int i = adxSspFinanceManagementService.deleteById(id);
LOG.info("the number of delete:{}", i);
restResponse.setResult(i);
return restResponse;
}
 
/**
* 批量删除
*
* @date 2016年9月29日 下午7:29:22
*
* @param id
* @return 删除了多少条
* @throws DaoException
*/
@ApiOperation(value = "删除", httpMethod = "POST", response = RestResponse.class)
@ApiImplicitParams({ @ApiImplicitParam(name = "id", required = true, dataType = "Long", paramType = "path") })
@RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
public RestResponse batchDelete(@RequestBody List<Long> ids) throws DaoException {
RestResponse restResponse = new RestResponse();
int i = adxSspFinanceManagementService.batchDelete(ids);
restResponse.setStatus(RestResponse.OK);
restResponse.setResult(i);
LOG.info("the number of delete:{}", i);
return restResponse;
}
}


  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值