一、新建SpringBoot站点
1.新建module,然后引入pom依赖:

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

2.新建Controller文件

@RestController
@RequestMapping("/demo")
public class DemoController {
@RequestMapping(value = "/index",method= RequestMethod.GET)
public String index(@RequestParam(value="name", required=false, defaultValue="World") String name) {
return "demo "+name;
}
}

3.新建SpringBoot启动文件

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class ContentPlatformWebapiApplication {
public static void main(String[] args) {
SpringApplication.run(ContentPlatformWebapiApplication.class,args);
}
}

4.运行,http://localhost:8080/demo/index?name=aa

二、配置Swagger2
1.引入Swagger pom依赖项

<dependencies>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>
</dependencies>

2.添加Swagger2 java配置文件

@Configuration
@EnableSwagger2
public class Swagger2Config { @Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage(“com.cathy.controller"))
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo());
} private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("测试接口平台API")
.description(“cathy demo API.")
.termsOfServiceUrl("Terms of service")
.contact("myeaddress@company.com")
.version("1.0")
.build();
} }

3.为controller添加swagger注解

@RestController
@RequestMapping("/demo")
@Api(value = "API - DemoController", description = "demo接口")
public class DemoController { @RequestMapping(value = "/index", method = RequestMethod.GET)
@ApiOperation(value = "首页", notes = "demo index")
@ApiImplicitParams({
@ApiImplicitParam(name = "name", value = "name", required = false,
dataType = "string", paramType = "query", defaultValue = "World")
})
public String index(@RequestParam(value = "name", required = false, defaultValue = "World") String name) {
return "demo " + name;
}
}

4.运行:http://localhost:8080/swagger-ui.html

JAVA入门[23]-SpringBoot配置Swagger2的更多相关文章

  1. Java入门和环境配置ideaJ安装

    Java入门及环境搭建 目录 Java入门及环境搭建 什么是Java Java Java的发展 Java的特性和优势 Java三大版本 JDK JRE JVM JAVA开发环境搭建 安装JDK 卸载J ...

  2. SpringBoot配置swagger2(亲测有效,如果没有配置成功,欢迎在下方留言)

    一.导包: <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swa ...

  3. SSM非springboot配置swagger2

    前提:maven,ssm,不是springboot项目 1.在maven中添加依赖 <!-- Swagger2 Begin --> <!--springfox的核心jar包--> ...

  4. springboot配置swagger2

    .在pom.xml里添加jar包: <dependency> <groupId>io.springfox</groupId> <artifactId>s ...

  5. springboot 配置 swagger2

    1.pom.xml 添加依赖 <!--swagger2 依赖--> <dependency> <groupId>io.springfox</groupId&g ...

  6. SpringBoot入门-15(springboot配置freemarker使用YML)

    https://blog.csdn.net/fengsi2009/article/details/78879924 application.yml spring: http: encoding: fo ...

  7. IDEA springboot配置

    基于springboot2.1.7 springboot项目创建 springboot热部署 springboot配置swagger2 springboot配置mybatis springboot配置 ...

  8. springboot新增swagger2配置

    转自http://www.cnblogs.com/jtlgb/p/8532433.html SpringBoot整合Swagger2 相信各位在公司写API文档数量应该不少,当然如果你还处在自己一个人 ...

  9. SpringBoot整合Swagger2案例,以及报错:java.lang.NumberFormatException: For input string: ""原因和解决办法

    原文链接:https://blog.csdn.net/weixin_43724369/article/details/89341949 SpringBoot整合Swagger2案例 先说SpringB ...

随机推荐

  1. python 什么是闭包

    1.啰嗦一下 学这个知识点的时候,我本来想先了解下定义, 知道个大概再说, 翻了几篇博客,基本上都是有例子带着进入理解这块.即使读了定义,思想还是不能显出个框架. 想吃快餐,有些行,有些就不可以(这里 ...

  2. 小米手机跨域问题,返回resphone:undefined,status 0

    小米手机跨域问题,返回resphone:undefined,status 0我小米note2的手机登录不上,返回resphone:undefined,status 0 我手机登录不了的问题解决了,后台 ...

  3. SynchronousQueue------TransferQueue源码分析

    不像ArrayBlockingQueue.LinkedBlockingDeque之类的阻塞队列依赖AQS实现并发操作,SynchronousQueue直接使用CAS实现线程的安全访问.由于源码中充斥着 ...

  4. Visual Studio 2010 集成 SP1 补丁 制作 Visual Studio 2010 Service Pack 1 完整版安装光盘的方法

    Now that Visual Studio 2010 SP1 has been released, administrators and developers may wish to install ...

  5. Android Studio环境安装

    Android Studio下载 http://www.android-studio.org/ JDK下载 https://www.oracle.com/technetwork/java/index. ...

  6. docker命令

    ## List Docker CLI commandsdockerdocker container --help ## Display Docker version and infodocker -- ...

  7. 利用phpspider爬取网站数据

    本文实例原址:PHPspider爬虫10分钟快速教程 在我们的工作中可能会涉及到要到其它网站去进行数据爬取的情况,我们这里使用phpspider这个插件来进行功能实现. 1.首先,我们需要php环境, ...

  8. 算法(第四版)C# 习题题解——1.3

    写在前面 整个项目都托管在了 Github 上:https://github.com/ikesnowy/Algorithms-4th-Edition-in-Csharp 这一节内容可能会用到的库文件有 ...

  9. PHP操作RabbitMQ的类 exchange、queue、route kye、bind

    RabbitMQ是常见的消息中间件.也许是还是不够了解的缘故,感觉功能还好吧. 讲到队列,大家脑子里第一印象是下边这样的. P生产者推送消息-->队列-->C消费者取出消息 结构很简单,但 ...

  10. eclipse中出现An internal error occurred during: "Initializing Java Tooling"

    关于这个问题我查了一下,就是删除.projct文件夹下的文件. 自己试了一下,这个可以及解决问题可是会出现新的问题. 1.SVN关联没了,这样做你的svn信息都没了,项目还要重新导一遍 2.出现了新的 ...