//Application 开启注解

@EnableSwagger2
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

pom.xml

                <!-- Swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>

SwaggerConfig.java

package com.qmorn.appapi.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; @Configuration
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.qmorn.appapi.user"))
.paths(PathSelectors.any())
//.apis(RequestHandlerSelectors.any())
//.paths(Predicates.not(PathSelectors.regex("/error.*")))
.build()
.apiInfo(apiInfo());
} private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("对外开放接口API 文档")
//.description("HTTP对外开放接口")
.version("1.0.0")
//.termsOfServiceUrl("http://xxx.xxx.com")
//.license("LICENSE")
//.licenseUrl("http://xxx.xxx.com")
.build();
}
}

SwaggerDemo.java

package com.qmorn.appapi.user;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore; /**
*
* @author YuwenTims
* @Date 2018-06-14
*/ @RestController
public class SwaggerDemo { @ApiOperation(value = "paramType : query", notes = "第一行说明</br>第二行")
@RequestMapping(value = "/byQuery", method = { RequestMethod.POST })
public String byQuery(@RequestParam Integer id) {
return "id = " + id;
} @ApiOperation(value = "paramType : path")
@RequestMapping(value = "/byPath/{id}", method = { RequestMethod.POST })
public String byPath(@PathVariable(name = "id") Long id) {
return "id = " + id;
} @ApiOperation(value = "paramType : header")
@ApiImplicitParams({ @ApiImplicitParam(paramType = "header", dataType = "String", name = "id", value = "参数", required = true) })
@RequestMapping(value = "/byHeader", method = { RequestMethod.POST })
public String byHeader(@RequestHeader ("id") String id) {
return "user id = " + id;
} @ApiOperation(value = "paramType : form")
@ApiImplicitParams({ @ApiImplicitParam(paramType = "form", dataType = "Long", name = "id", value = "参数", required = true) })
@RequestMapping(value = "/byForm", method = { RequestMethod.POST })
public String byForm(@RequestParam(value="id",defaultValue="0") Long id) {
return "user id = " + id;
} @ApiOperation(value = "paramType : body")
@ApiImplicitParams({ @ApiImplicitParam(paramType = "body", dataType = "String", name = "id", value = "参数", required = true) })
@RequestMapping(value = "/queryByBody", method = { RequestMethod.POST })
public String queryByBody(@RequestBody String id) {
return "user id = " + id;
} @ApiIgnore//忽略此接口说明(在swagger-ui文档中不显示)
@RequestMapping(value = "/ignore", method = { RequestMethod.POST })
public String ignore(@RequestParam Long id) {
return "user id = " + id;
}
}

  

swagger demo code的更多相关文章

  1. penpyxl basic function demo code

    Openpyxl basic function demo code demo code: #!/usr/bin/env python # -*- coding: utf-8 -*- "&qu ...

  2. RAD Studio Demo Code和几个国外FMX网站 good

    FireMonkey X –  Amazing overview of FireMonkey FMX Feeds – All your FireMonkey news in one place FMX ...

  3. SpringBoot + Swagger Demo

    Swagger是什么? Swagger 是一个规范且完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.  Swagger 的目标是对 REST API 定义一个标准且和语 ...

  4. ps2keyboard demo code for 8052

    #pragma code symbols debug objectextend #include <reg51.h> /* special function register declar ...

  5. ( ! ) Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in D:\demo\code\yolo\index\index.php on li

    sql语句为:$sql="select count(*) from com where a_id=$v['id']"; 出现以下错误: 原因: 变量没有用花括号引起来 改为:  $ ...

  6. npm install 安装报错:npm ERR! EPERM npm ERR! -4048 npm ERR! Error: EPERM: operation not permitted, unlink 'D:\test\demo\code\materialT\node_modules\.staging'

    更新项目依赖包,删除掉package-lock.json.node_modules,运行npm install,报如上错误信息,查询资料说是没有权限,本人用管理员身份打开powershell,运行np ...

  7. c++ demo code

    /* //多继承 #include <iostream> using namespace std; class Sofa { public: Sofa(); ~Sofa(); void s ...

  8. 使用dropwizard(5)--加入swagger

    前言 Swagger已经成API service的规范了,本处在dropwizard中简单集成Swagger. Demo source https://github.com/Ryan-Miao/l4d ...

  9. Asp.Net Core配置Swagger

    本文主要参考:Using Swagger with ASP.net Core 1.创建WebApi项目 本文使用ASP.Net Core Web API项目模板演示Swagger到使用,首先创建Web ...

随机推荐

  1. GCD 学习(八)dispatch_semaphore

    dispatch_semaphore 信号量基于计数器的一种多线程同步机制.在多个线程访问共有资源时候,会因为多线程的特性而引发数据出错的问题.     dispatch_queue_t queue ...

  2. 树莓派研究笔记(1)-- 安装Mono

    职业病啊,原谅我,第一步就是要安装Mono搞DOTNET 1. 更新系统 sudo apt-get update 2. 安装 Mono sudo apt-get install mono-comple ...

  3. ConfigParser简介

    一.ConfigParser简介 ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号“[ ]”内包含的为section.section 下面为类似于key-value 的配置 ...

  4. PHP 中for循环的一个小小改进

    注意 : 1, $size这个值可以放在for循环中的第一个 ';' 前声明, 因为这个声明只会执行一次; 2, 第二个 ';' 中的内容, 会重复运行, 所以$i < $size 这个判断会每 ...

  5. Java命令之javap初探

    javap是jdk自带的一个工具在jdk安装目录的/bin下面可以找到,可以对代码反编译,也可以查看java编译器生成的字节码,对代码的执行过程进行分析,了解jvm内部的工作. 下面列举javap命令 ...

  6. Category 分类

    1.Category 1)分类/类别(category): 允许以模块的方式向现有类定义添加新的方法(默认不能添加实例变量).用以扩展自己或他人以前实现的类,使它适合自己的需要. 分类的名称括在类名之 ...

  7. Mybatis的输出结果封装

    resultType属性可以指定结果集的类型,它支持基本类型和实体类类型. 需要注意的是,它和parameterType一样,如果注册过类型别名的,可以直接使用别名. 没有注册过的必须使用全限定类名. ...

  8. jzoj4916. 【GDOI2017模拟12.9】完全背包问题 (背包+最短路)

    题面 题解 考场上蠢了--这么简单的东西都想不到-- 首先排序加去重. 先来考虑一下,形如 \[a_1x_1+a_2x_2+...a_nx_n=w,a_1<a_2<...<a_n,x ...

  9. Python 文件和异常

    一.从文件中读取数据 #!/usr/bin/env python with open('pi') as file_object: contents = file_object.read() print ...

  10. cuda by example

    int offset= x+y*dim   x 线程块内的线程索引 y 线程块索引 dim 线程块的维度   tid = threadIdx.x+blockIdx.x*blockDim.x 计算大于或 ...