- 浏览: 2552527 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
Spring Boot and Swagger in JAVA
Followed this document
https://springframework.guru/spring-boot-restful-api-documentation-with-swagger-2/
Find some examples
https://github.com/springfox/springfox-demos
Git clone the base project to local
> git clone https://github.com/springfox/springfox
Build and generate package to local maven
> ./gradlew clean build publishToMavenLocal -i
Then I can import and check the example project as follow
springfox-demos/springfox-integration-webflux
Usually the generated documents will be here
http://localhost:8080/v2/api-docs
The Swagger UI will be here
http://localhost:8080/swagger-ui.html#/
More annotation example
https://github.com/swagger-api/swagger-core/wiki/annotations#quick-annotation-overview
Here is my working Swagger Configuration.
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
Swagger Configuration
package com.kikokang.connector.netsuite.config;
import static com.google.common.base.Predicates.not;
import static springfox.documentation.builders.PathSelectors.regex;
import static springfox.documentation.builders.RequestHandlerSelectors.basePackage;
import java.util.Collections;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).select().apis(not(basePackage("org.springframework.boot")))
.paths(not(regex("/error"))).build().apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfo("Netsuite Connector RESTful API", null, "API V1", null, null, null, null,
Collections.emptyList());
}
}
In the Controller
@Api(value = "/api/customer/")
@ApiOperation(value = "Get Customer", response = com.kikokang.connector.netsuite.model.CustomerDTO.class)
@GetMapping(path = "/v1/{id}", produces = MediaTypes.JSON_UTF_8)
public Mono<CustomerDTO> get(@ApiParam(defaultValue = "1525", required = true) @PathVariable("id") String id,
@ApiParam(defaultValue = “xxxx1", required = true) @RequestHeader(name = "account", required = false) String account,
@ApiParam(defaultValue = “xxxx2", required = true) @RequestHeader(name = "tbaConsumerKey", required = false) String tbaConsumerKey,
@ApiParam(defaultValue = “xxxx3", required = true) @RequestHeader(name = "tbaConsumerSecret", required = false) String tbaConsumerSecret,
@ApiParam(defaultValue = “xxxx4", required = true) @RequestHeader(name = "tbaTokenId", required = false) String tbaTokenId,
@ApiParam(defaultValue = “xxxx5", required = true) @RequestHeader(name = "tbaTokenSecret", required = false) String tbaTokenSecret) {
LOGGER.info("account:" + account + " tbaConsumerKey:" + tbaConsumerKey + " tbaConsumerSecret:"
+ tbaConsumerSecret + " tbaTokenId:" + tbaTokenId + " tbaTokenSecret:" + tbaTokenSecret);
if (!WebUtil.validateHeader(account, tbaConsumerKey, tbaConsumerSecret, tbaTokenId, tbaTokenSecret)) {
return Mono.error(new HeaderMissingException("Token and ConsumerKey missing in Header"));
}
HeaderDO header = WebUtil.getHeaderDO(account, tbaConsumerKey, tbaConsumerSecret, tbaTokenId, tbaTokenSecret);
try {
Customer customer = customerService.getByID(id, header);
CustomerDTO returnDto = CustomerTransform.convertToCustomerDTO(customer);
return Mono.just(returnDto);
} catch (ServiceException e) {
LOGGER.error("ServiceException", e);
return Mono.error(e);
}
}
References:
https://sillycat.iteye.com/blog/2247372
https://github.com/springframeworkguru/springboot_swagger_example
https://springframework.guru/spring-boot-restful-api-documentation-with-swagger-2/
https://swagger.io/
Followed this document
https://springframework.guru/spring-boot-restful-api-documentation-with-swagger-2/
Find some examples
https://github.com/springfox/springfox-demos
Git clone the base project to local
> git clone https://github.com/springfox/springfox
Build and generate package to local maven
> ./gradlew clean build publishToMavenLocal -i
Then I can import and check the example project as follow
springfox-demos/springfox-integration-webflux
Usually the generated documents will be here
http://localhost:8080/v2/api-docs
The Swagger UI will be here
http://localhost:8080/swagger-ui.html#/
More annotation example
https://github.com/swagger-api/swagger-core/wiki/annotations#quick-annotation-overview
Here is my working Swagger Configuration.
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
Swagger Configuration
package com.kikokang.connector.netsuite.config;
import static com.google.common.base.Predicates.not;
import static springfox.documentation.builders.PathSelectors.regex;
import static springfox.documentation.builders.RequestHandlerSelectors.basePackage;
import java.util.Collections;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).select().apis(not(basePackage("org.springframework.boot")))
.paths(not(regex("/error"))).build().apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfo("Netsuite Connector RESTful API", null, "API V1", null, null, null, null,
Collections.emptyList());
}
}
In the Controller
@Api(value = "/api/customer/")
@ApiOperation(value = "Get Customer", response = com.kikokang.connector.netsuite.model.CustomerDTO.class)
@GetMapping(path = "/v1/{id}", produces = MediaTypes.JSON_UTF_8)
public Mono<CustomerDTO> get(@ApiParam(defaultValue = "1525", required = true) @PathVariable("id") String id,
@ApiParam(defaultValue = “xxxx1", required = true) @RequestHeader(name = "account", required = false) String account,
@ApiParam(defaultValue = “xxxx2", required = true) @RequestHeader(name = "tbaConsumerKey", required = false) String tbaConsumerKey,
@ApiParam(defaultValue = “xxxx3", required = true) @RequestHeader(name = "tbaConsumerSecret", required = false) String tbaConsumerSecret,
@ApiParam(defaultValue = “xxxx4", required = true) @RequestHeader(name = "tbaTokenId", required = false) String tbaTokenId,
@ApiParam(defaultValue = “xxxx5", required = true) @RequestHeader(name = "tbaTokenSecret", required = false) String tbaTokenSecret) {
LOGGER.info("account:" + account + " tbaConsumerKey:" + tbaConsumerKey + " tbaConsumerSecret:"
+ tbaConsumerSecret + " tbaTokenId:" + tbaTokenId + " tbaTokenSecret:" + tbaTokenSecret);
if (!WebUtil.validateHeader(account, tbaConsumerKey, tbaConsumerSecret, tbaTokenId, tbaTokenSecret)) {
return Mono.error(new HeaderMissingException("Token and ConsumerKey missing in Header"));
}
HeaderDO header = WebUtil.getHeaderDO(account, tbaConsumerKey, tbaConsumerSecret, tbaTokenId, tbaTokenSecret);
try {
Customer customer = customerService.getByID(id, header);
CustomerDTO returnDto = CustomerTransform.convertToCustomerDTO(customer);
return Mono.just(returnDto);
} catch (ServiceException e) {
LOGGER.error("ServiceException", e);
return Mono.error(e);
}
}
References:
https://sillycat.iteye.com/blog/2247372
https://github.com/springframeworkguru/springboot_swagger_example
https://springframework.guru/spring-boot-restful-api-documentation-with-swagger-2/
https://swagger.io/
发表评论
-
Update Site will come soon
2021-06-02 04:10 1679I am still keep notes my tech n ... -
Portainer 2020(4)Deploy Nginx and Others
2020-03-20 12:06 431Portainer 2020(4)Deploy Nginx a ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 436Private Registry 2020(1)No auth ... -
Docker Compose 2020(1)Installation and Basic
2020-03-15 08:10 374Docker Compose 2020(1)Installat ... -
VPN Server 2020(2)Docker on CentOS in Ubuntu
2020-03-02 08:04 456VPN Server 2020(2)Docker on Cen ... -
Nginx Deal with OPTIONS in HTTP Protocol
2020-02-15 01:33 356Nginx Deal with OPTIONS in HTTP ... -
PDF to HTML 2020(1)pdftohtml Linux tool or PDFBox
2020-01-29 07:37 405PDF to HTML 2020(1)pdftohtml Li ... -
Elasticsearch Cluster 2019(2)Kibana Issue or Upgrade
2020-01-12 03:25 720Elasticsearch Cluster 2019(2)Ki ... -
Spark Streaming 2020(1)Investigation
2020-01-08 07:19 295Spark Streaming 2020(1)Investig ... -
Hadoop Docker 2019 Version 3.2.1
2019-12-10 07:39 295Hadoop Docker 2019 Version 3.2. ... -
MongoDB 2019(3)Security and Auth
2019-11-16 06:48 241MongoDB 2019(3)Security and Aut ... -
MongoDB 2019(1)Install 4.2.1 Single and Cluster
2019-11-11 05:07 294MongoDB 2019(1) Follow this ht ... -
Monitor Tool 2019(1)Monit Installation and Usage
2019-10-17 08:22 325Monitor Tool 2019(1)Monit Insta ... -
Ansible 2019(1)Introduction and Installation on Ubuntu and CentOS
2019-10-12 06:15 312Ansible 2019(1)Introduction and ... -
Timezone and Time on All Servers and Docker Containers
2019-10-10 11:18 332Timezone and Time on All Server ... -
Kafka Cluster 2019(6) 3 Nodes Cluster on CentOS7
2019-10-05 23:28 283Kafka Cluster 2019(6) 3 Nodes C ... -
K8S Helm(1)Understand YAML and Kubectl Pod and Deployment
2019-10-01 01:21 326K8S Helm(1)Understand YAML and ... -
Rancher and k8s 2019(5)Private Registry
2019-09-27 03:25 362Rancher and k8s 2019(5)Private ... -
Jenkins 2019 Cluster(1)Version 2.194
2019-09-12 02:53 444Jenkins 2019 Cluster(1)Version ... -
Redis Cluster 2019(3)Redis Cluster on CentOS
2019-08-17 04:07 373Redis Cluster 2019(3)Redis Clus ...
相关推荐
java 使用 Swagger 创建一个Spring Boot 的 Web 服务java 使用 Swagger 创建一个Spring Boot 的 Web 服务java 使用 Swagger 创建一个Spring Boot 的 Web 服务java 使用 Swagger 创建一个Spring Boot 的 Web 服务java...
Spring Boot 是一个基于 Java 的框架,用于快速构建生产级别的应用程序。Swagger 是一个流行的 API 文档工具,能够生成在线接口文档,帮助开发人员和调用接口的人员更好地理解和使用 API。下面,我们将探讨如何在 ...
Spring Boot Schedule Swagger 知识点详解 在现代的Web应用开发中,Spring Boot因其便捷的配置和强大的功能而被广泛采用。"spring boot schedule swagger"的主题涵盖了两个关键领域:Spring Boot的定时任务调度...
在本教程中,我们将深入探讨如何将Swagger2与Spring Boot集成,同时考虑到Spring Security和JWT(JSON Web Token)的安全机制。Swagger2是一个流行的API文档工具,它允许开发者以交互式方式展示和测试RESTful API。...
Spring Boot 2.7.5 是一款流行的Java框架,它简化了微服务应用程序的开发过程。Swagger 3,即OpenAPI Specification的第三个主要版本,是一个用于描述RESTful API的规范,它提供了强大的工具来生成、测试和文档化API...
Spring Boot 是一个基于Java的框架,它简化了创建独立、生产级别的基于Spring的应用程序的过程。它的目标是让你能够快速地“起步并运行”,通过提供默认配置来减少项目的复杂性。Swagger 则是一个流行的API文档和...
Spring Boot 整合 Swagger2 是一个常见的需求,用于构建RESTful API的文档系统。Swagger2是一个流行的API开发工具,它可以自动生成API文档,方便开发者理解和使用API。在Spring Boot项目中整合Swagger2,可以让我们...
在本项目中,我们主要关注的是使用Spring Boot 2.6.11、Spring Cloud以及Swagger3构建微服务架构的过程。这些技术都是现代云应用开发中的核心组件,它们各自提供了强大的功能来简化开发和增强应用的可维护性。 首先...
Spring Boot 是一个流行的Java开发框架,它简化了创建独立、生产级别的基于Spring的应用程序。随着版本的升级,Spring Boot引入了对新特性和工具的支持。在Spring Boot 2.7及以上版本,它开始支持Swagger 3,这是一...
**Spring Boot 集成 Swagger 知识点详解** Swagger 是一个流行的 API 文档和测试工具,它允许开发者通过注解来定义 RESTful API 的接口,生成交互式的文档,便于测试和理解 API。Spring Boot 是一个快速开发框架,...
Spring Boot 整合 Swagger 是一种常见的方式,用于构建RESTful API的交互式文档。Swagger 提供了一种标准化的方式来描述 RESTful API,使得开发者能够轻松地理解接口的使用方法,并进行在线调试。以下是对如何在 ...
综合以上信息,我们可以得出,这是一个使用Java Spring Boot框架,集成Swagger进行API文档化的项目。开发者可能使用Eclipse 4.5.2作为IDE,通过Maven管理依赖并进行构建。项目可能包含一个或多个RESTful服务,并且...
在本文中,我们将探讨如何将Spring Boot框架与Swagger集成以实现API管理。首先,我们来了解一下Swagger工具的背景和作用。 Swagger是由Smartbear公司创建的,它是一个支持RESTful API的开发工具,允许开发者描述API...
1. **Spring Boot**: Spring Boot是Spring框架的一个扩展,旨在简化Spring应用的初始搭建以及开发过程。它内置了Tomcat服务器,可以快速创建独立运行的Java应用。Spring Boot通过“约定优于配置”的原则,减少了大量...
Spring Boot 2.x基础教程:使用Swagger2构建强大的API文档 Spring Boot 2.x基础教程:JSR-303实现请求参数校验 Spring Boot 2.x基础教程:Swagger接口分类与各元素排序问题详解 Spring Boot 2.x基础教程:Swagger...
Spring Boot 是一个基于Java的框架,它简化了创建独立、生产级别的基于Spring的应用程序过程。Swagger2 是一个流行的API开发工具,它允许开发者通过简洁的注解来文档化RESTful API,提供了一种友好的图形界面,使得...
Spring Boot是Java开发领域中的一款热门框架,它简化了基于Spring的应用程序的初始设置和配置。这个压缩包包含了丰富的Spring Boot学习资料以及实际项目案例,是深入理解和掌握Spring Boot技术的宝贵资源。 首先,...
Spring Boot 是一个基于Java的框架,它简化了创建独立、生产级别的基于Spring的应用程序的流程。Swagger 是一个流行的API开发工具,它允许开发者通过简单的注解来定义RESTful API,并生成交互式的API文档,便于前后...
而Swagger是目前最流行的接口文档解决方案,本文主要通过代码实战的方式讲解Spring Boot 和Swagger集成生成Restful接口文档。教程参见 http://blog.csdn.net/zjx2016/article/details/74407832
### Spring Boot的自动化配置实现swagger2引入spring boot生成API文档 #### 一、Spring Boot与Swagger集成概述 在现代Web开发中,API文档对于确保良好的系统间通信至关重要。随着微服务架构的兴起,API文档的需求...