1.pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>swagger</groupId>
<artifactId>swagger</artifactId>
<version>0.0.1-SNAPSHOT</version> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.0</version>
</dependency> </dependencies>
</project>

  swagger2配置文件

package com.newtouch.swagger.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
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 SwaggerConfiguration { @Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.newtouch.swagger.controller"))
.paths(PathSelectors.any())
.build();
} private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("SpringBoot中使用Swagger2构建RESTfulAPIs")
.description("SpringBoot中使用Swagger2构建RESTfulAPIs")
.termsOfServiceUrl("http://blog.didispace.com/")
.contact("程序猿DD")
.version("1.0")
.build();
} }

  请求bean

package com.newtouch.swagger.controller.bean;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; @ApiModel("用户")
public class UserRequest { @ApiModelProperty(value="用户id",name="id",required=false)
private int id; @ApiModelProperty(value="用户name",name="name",required=false)
private String name; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} }

  controller

package com.newtouch.swagger.controller;

import java.util.HashMap;
import java.util.Map; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import com.newtouch.swagger.controller.bean.UserRequest; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam; @Api("用户操作类")
@RestController
public class UserController { @RequestMapping(value="/getName")
@ApiOperation("用户对象")
public Map getName(@RequestBody @ApiParam(name="用户对象",value="传入json格式",required=true) UserRequest u) {
Map<String, Object> resultMap = new HashMap<>();
resultMap.put("id", u.getId());
resultMap.put("name", u.getName()); return resultMap;
} }

  效果展示

swagger注解详解:https://blog.csdn.net/u014231523/article/details/76522486

springboot+swagger2案例的更多相关文章

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

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

  2. springboot+swagger2

    springboot+swagger2 小序 新公司的第二个项目,是一个配置管理终端机(比如:自动售卖机,银行取款机)的web项目,之前写过一个分模块的springboot框架,就在那个框架基础上进行 ...

  3. SpringBoot开发案例之多任务并行+线程池处理

    前言 前几篇文章着重介绍了后端服务数据库和多线程并行处理优化,并示例了改造前后的伪代码逻辑.当然了,优化是无止境的,前人栽树后人乘凉.作为我们开发者来说,既然站在了巨人的肩膀上,就要写出更加优化的程序 ...

  4. SpringBoot开发案例从0到1构建分布式秒杀系统

    前言 ​最近,被推送了不少秒杀架构的文章,忙里偷闲自己也总结了一下互联网平台秒杀架构设计,当然也借鉴了不少同学的思路.俗话说,脱离案例讲架构都是耍流氓,最终使用SpringBoot模拟实现了部分秒杀场 ...

  5. SpringBoot+Swagger2 整合

    SpringBoot+Swagger2四步整合 第一步:添加相关依赖 <parent> <groupId>org.springframework.boot</groupI ...

  6. Spring-boot官方案例分析之data-jpa

    Spring-boot官方案例分析之data-jpa package sample.data.jpa; import org.junit.Before; import org.junit.Test; ...

  7. Spring-boot官方案例分析之log4j

    Spring-boot官方案例分析之log4j 运行单元测试分析: @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfigur ...

  8. Xmemcached与SpringBoot实际案例

    在本人的这篇文章<Xmemcached集群与SpringBoot整合>基础上,进行XMemcached与SpringBoot实际案例的结合. 有以下这张表,将这张表的增删改查操作都添加到X ...

  9. Springboot+swagger2.7集成开发

    Springboot+swagger2.7集成开发 本篇文章是介绍最新的springboot和swagger2.7集成开发和2.0稍微有一些出入: Springboot集成环境配置 Swagger2. ...

随机推荐

  1. 采用dlopen、dlsym、dlclose dlopen dlerror加载动态链接库【总结】

    .前言 为了使程序方便扩展,具备通用性,可以采用插件形式.采用异步事件驱动模型,保证主程序逻辑不变,将各个业务已动态链接库的形式加载进来,这就是所谓的插件.linux提供了加载和处理动态链接库的系统调 ...

  2. JavaScript cookie操作实现点赞功能

    JavaScript cookie操作实现点赞功能 参考实现原理,但是代码不够简洁,简洁代码参考:js操作cookie 实现一个点赞功能十分简单,主要问题在于不能重复点赞.  若是一个有用户的网站,可 ...

  3. 微信小程序文字水平垂直居中对齐问题

    我们知道常用的居中对齐方式有很多种例如: text-align:center; align-items:center; justify-content: center; margin: auto; # ...

  4. R中ifelse、which、%in%的用法

    R中ifelse.which.%in%的用法 (2014-02-08 13:54:08)标签: 教育 在R学习过程中,遇到了ifelse.which.%in%,下面分别举例,说明他们的用法.1.ife ...

  5. [hadoop读书笔记]译者序

    一.并行数据库系统 新一代高性能的数据库系统,是在MPP和集群并行计算环境的基础上建立的数据库系统. MPP:大规模并行处理计算机:Massive Parallel Processor.指的是一种处理 ...

  6. Python 类的属性再解

    #类的属性 class A(): #定义一个类的属性,类似于c++中的静态变量 num=0 def __init__(self): #实例对象属性定义赋值 self.name = "hha& ...

  7. go ftp通信

    main.go package main import ( "strings" ftp4go "github.com/shenshouer/ftp4go" &q ...

  8. 关于Unity中场景的导入与导出(专题九)

    Unity场景 场景虽然是由场景美术做好的,但是我们经常需要自己去导导出以及从别的项目导入,所以我们需要对场景的导入和导出有一个详细的了解 1: 场景是由美术人员搭建完成后提交给程序;2: 场景一般包 ...

  9. Selenium常用操作汇总二——如何得到弹出窗口

    在selenium 1.X里面得到弹出窗口是一件比较麻烦的事,特别是新开窗口没有id.name的时候.当时还整理了处理了几种方法,详见:http://seleniumcn.cn/read.php?ti ...

  10. 前段时间,接手一个项目使用的是原始的jdbc作为数据库的访问,发布到服务器上在运行了一段时间之后总是会出现无法访问的情况,登录到服务器,查看tomcat日志发现总是报如下的错误。    Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected est

    前段时间,接手一个项目使用的是原始的jdbc作为数据库的访问,发布到服务器上在运行了一段时间之后总是会出现无法访问的情况,登录到服务器,查看tomcat日志发现总是报如下的错误. Caused by: ...