Skip to content

Commit

Permalink
增加springboot-swagger示例工程
Browse files Browse the repository at this point in the history
  • Loading branch information
xuwujing committed Mar 17, 2019
1 parent faa3f6a commit d926f1b
Show file tree
Hide file tree
Showing 14 changed files with 405 additions and 0 deletions.
53 changes: 53 additions & 0 deletions springboot-swagger/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<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.pancm</groupId>
<artifactId>springboot-swagger</artifactId>
<version>0.0.1-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>test</scope>
</dependency>

<!-- swagger RESTful API -->
<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>

</project>
33 changes: 33 additions & 0 deletions springboot-swagger/src/main/java/com/pancm/SwaggerApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.pancm;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
*
* @Title: Application
* @Description: springBoot 主程序
* @Version:1.0.0
* @author pancm
* @date 2018年1月5日
*/

@SpringBootApplication
public class SwaggerApplication {

private static final Logger logger = LoggerFactory.getLogger(SwaggerApplication.class);

/*
* SpringApplication 则是用于从main方法启动Spring应用的类。默认,它会执行以下步骤:
* 1.创建一个合适的ApplicationContext实例 (取决于classpath)。
* 2.注册一个CommandLinePropertySource,以便将命令行参数作为Spring properties。 3.刷新application
* context,加载所有单例beans。 4.激活所有CommandLineRunner beans。
*/
public static void main(String[] args) {
// 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
SpringApplication.run(SwaggerApplication.class, args);
logger.info("Swagger程序启动成功!");
}
}
47 changes: 47 additions & 0 deletions springboot-swagger/src/main/java/com/pancm/config/Swagger2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.pancm.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;

/**
*
* @Title: Swagger2
* @Description:
* Swagger2配置
* @Version:1.0.0
* @author pancm
* @date 2018年5月29日
*/
@Configuration
@EnableSwagger2
public class Swagger2 {

@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.pancm"))
.paths(PathSelectors.any())
.build();
}

private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Spring Boot中使用Swagger2构建RESTful APIs")
.description("测试")
.termsOfServiceUrl("http://www.panchengming.com/")
.contact("xuwujing")
.version("1.0")
.build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* @Title: package-info
* @Description:
* @Version:1.0.0
* @author pancm
* @date 2019年3月17日
*/
package com.pancm.config;
8 changes: 8 additions & 0 deletions springboot-swagger/src/main/java/com/pancm/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* @Title: package-info
* @Description:
* @Version:1.0.0
* @author pancm
* @date 2019年3月17日
*/
package com.pancm;
56 changes: 56 additions & 0 deletions springboot-swagger/src/main/java/com/pancm/pojo/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.pancm.pojo;
/**
*
* @Title: User
* @Description:用户pojo类
* @Version:1.0.0
* @author pancm
* @date 2017年9月26日
*/
public class User {
/** 编号 */
private Long id;
/** 姓名 */
private String name;

/** 年龄 */
private Integer age;

public User(){
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

/**
*
*/
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
}



}
11 changes: 11 additions & 0 deletions springboot-swagger/src/main/java/com/pancm/pojo/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
*
*/
/**
* Title: package-info
* Description: 实体类
* Version:1.0.0
* @author pancm
* @date 2018年1月4日
*/
package com.pancm.pojo;
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.pancm.web;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.pancm.pojo.User;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;



/**
*
* @Title: UserRestController
* @Description:
* 用户控制层
* 在浏览器输入 http://localhost:8183/swagger-ui.html 即可查看
* @Version:1.0.0
* @author pancm
* @date 2018年3月19日
*/
@RestController
@RequestMapping(value = "/api")
public class UserRestController {

private final Logger logger = LoggerFactory.getLogger(this.getClass());

/**
* @ApiOperation注解来给API增加说明、通过@ApiImplicitParams注解来给参数增加说明。
* value 是标题,notes是详细说明
* @param user
* @return
*/
@ApiOperation(value="创建用户", notes="根据User对象创建用户")
@ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
@PostMapping("/user")
public boolean insert(@RequestBody User user) {
logger.info("开始新增用户信息!请求参数:{}",user);
return true;
}

@ApiOperation(value="更新用户", notes="根据User对象更新用户")
@ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
@PutMapping("/user")
public boolean update(@RequestBody User user) {
logger.info("开始更新用户信息!请求参数:{}",user);
return true;
}

@ApiOperation(value="删除用户", notes="根据User对象删除用户")
@ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
@DeleteMapping("/user")
public boolean delete(@RequestBody User user) {
logger.info("开始删除用户信息!请求参数:{}",user);
return true;
}

/**
* 注:@GetMapping("/user")是spring 4.3的新注解等价于:
* @RequestMapping(value = "/user", method = RequestMethod.GET)
* @param user
* @return
*/
@ApiOperation(value="获取用户列表", notes="根据User对象查询用户信息")
@ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
@GetMapping("/user")
public User findByUser(User user) {
logger.info("开始查询用户列表,请求参数:{}",user);
User user2 =new User();
user2.setId(1L);
user2.setAge(18);
user2.setName("xuwujing");
return user2;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* @Title: package-info
* @Description:
* @Version:1.0.0
* @author pancm
* @date 2019年3月17日
*/
package com.pancm.web;
13 changes: 13 additions & 0 deletions springboot-swagger/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# encode
banner.charset=UTF-8
server.tomcat.uri-encoding=UTF-8
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
spring.messages.encoding=UTF-8


## port
server.port=8183


11 changes: 11 additions & 0 deletions springboot-swagger/target/classes/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Manifest-Version: 1.0
Implementation-Title: springboot-swagger
Implementation-Version: 0.0.1-SNAPSHOT
Built-By: pancm
Implementation-Vendor-Id: com.pancm
Build-Jdk: 1.8.0_152
Implementation-URL: http://projects.spring.io/spring-boot/springboot-s
wagger/
Created-By: Maven Integration for Eclipse
Implementation-Vendor: Pivotal Software, Inc.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#Generated by Maven Integration for Eclipse
#Sun Mar 17 19:45:50 GMT+08:00 2019
version=0.0.1-SNAPSHOT
groupId=com.pancm
m2e.projectName=springboot-swagger
m2e.projectLocation=F\:\\\u81EA\u5DF1\u7684\u9879\u76EE\\github\\springBoot-study\\springboot-swagger
artifactId=springboot-swagger
Loading

0 comments on commit d926f1b

Please sign in to comment.