spring boot Swagger 集成
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>com.dalong.springboot</groupId> <artifactId>sw</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <!-- jdk版本号,angel在这里使用1.8,大家修改为大家本地配置的jdk版本号即可 --> <java.version>1.8</java.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.3.RELEASE</version> </parent> <dependencies> <!-- spring boot web支持:mvc,aop... --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Swagger --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2. 启用注解
@SpringBootApplication @EnableSwagger2 public class startApp { @Bean public Filter characterEncodingFilter() { CharacterEncodingFilter characterEncodingFilter =new CharacterEncodingFilter(); characterEncodingFilter.setEncoding("UTF-8"); characterEncodingFilter.setForceEncoding(true); return characterEncodingFilter; } public static void main(String[] args) { SpringApplication.run(startApp.class, args); } }
3. controller 使用APi 注解说明
@RestController public class firstController { @RequestMapping(value = "/hello") @ApiOperation(value = "add user",httpMethod ="POST", response = User.class,notes = "get user info") public User hello(@ApiParam(required = true,name = "paramData",value = "user json data") @RequestBody User user){ return user; } @RequestMapping("/app") @ApiOperation(value = "获取用户信息",httpMethod ="GET", response = User.class,notes = "获取用户信息") public User userinfo(){ User user=new User(); user.setAge(33); user.setName("dalong"); return user; } }
4. 测试页面: