首先新建一个spring boot 工程:
我直接在以前的仓库里写了,application如下:
package demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
添加maven依赖:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
完整pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>spring-boot</groupId>
<artifactId>spring-study</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.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-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--spring-boot-starter-test 起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.29</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
添加swagger2配置:
//加入配置注解
@Configuration
//开启swagger
@EnableSwagger2
public class Swagger2 {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
.apis(RequestHandlerSelectors.basePackage("demo"))//这里是项目的基本包,改成项目对应的包路径
.paths(PathSelectors.regex("(?!/error.*).*")).build();
}
@SuppressWarnings("deprecation")
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("天王盖地虎api接口").description("说明RESTful APIs").contact("[email protected]").version("2018.0")
.build();
}
}
然后建一个相应的controller
对应的bean:
package demo.springboottest;
public class Bird {
String name;
String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
接口:
package demo.springboottest.service;
public interface Ibird {
String run();
String sayhello();
}
接口实现:
package demo.springboottest.controller;
import demo.springboottest.Bird;
import demo.springboottest.service.Ibird;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping(value = "/bird")
public class BirdController {
@Autowired
private Ibird ibird;
@GetMapping(value = "/run")
public String run() {
return ibird.run();
}
@PostMapping(value = "/hello")
public String sayHello() {
return ibird.sayhello();
}
@GetMapping(value = "/runparam")
public String runwithparam(@RequestParam(value = "param") String param) {
System.out.println("param is :" + param);
return ibird.run();
}
@PostMapping(value = "/helloparam")
public String sayHellowithparam(@RequestBody Bird bird) {
System.out.println("param is : " + bird);
return ibird.sayhello();
}
}
controller:
package demo.springboottest.controller;
import demo.springboottest.Bird;
import demo.springboottest.service.Ibird;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping(value = "/bird")
public class BirdController {
@Autowired
private Ibird ibird;
@GetMapping(value = "/run")
public String run() {
return ibird.run();
}
@PostMapping(value = "/hello")
public String sayHello() {
return ibird.sayhello();
}
@GetMapping(value = "/runparam")
public String runwithparam(@RequestParam(value = "param") String param) {
System.out.println("param is :" + param);
return ibird.run();
}
@PostMapping(value = "/helloparam")
public String sayHellowithparam(@RequestBody Bird bird) {
System.out.println("param is : " + bird);
return ibird.sayhello();
}
}
启动工程,浏览器输入:http://127.0.0.1:8080/swagger-ui.html#/
更多参考官网:https://swagger.io/
源码直接更新在练习spring boot 的项目里了:
https://github.com/xiaomingtongxie/springboot-study.git
代码已更新,对你有帮助的话,小哥哥小姐姐,帮点下赞哇
应评论小哥哥要求,将json/doc 转静态.html或pdf 别人写的demo已跑通,效果图:
demo地址:
https://github.com/Swagger2Markup/spring-swagger2markup-demo
待我研究一下,再更到博客里哇。