-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ee357b0
commit 32f915b
Showing
11 changed files
with
375 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/target/ | ||
!.mvn/wrapper/maven-wrapper.jar | ||
|
||
### STS ### | ||
.apt_generated | ||
.classpath | ||
.factorypath | ||
.project | ||
.settings | ||
.springBeans | ||
.sts4-cache | ||
|
||
### IntelliJ IDEA ### | ||
.idea | ||
*.iws | ||
*.iml | ||
*.ipr | ||
|
||
### NetBeans ### | ||
/nbproject/private/ | ||
/build/ | ||
/nbbuild/ | ||
/dist/ | ||
/nbdist/ | ||
/.nb-gradle/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?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> | ||
<artifactId>springboot-swagger</artifactId> | ||
|
||
<name>springboot-swagger</name> | ||
<description>SprigBoot集成Swagger2</description> | ||
|
||
<parent> | ||
<groupId>com.lance.learn</groupId> | ||
<artifactId>springbootlearning</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
</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>io.springfox</groupId> | ||
<artifactId>springfox-swagger2</artifactId> | ||
<version>2.7.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.springfox</groupId> | ||
<artifactId>springfox-swagger-ui</artifactId> | ||
<version>2.7.0</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
<finalName>SpringBoot-Swagger2-${profileActive}</finalName> | ||
</build> | ||
|
||
|
||
</project> |
12 changes: 12 additions & 0 deletions
12
...Swagger/src/main/java/com/lance/learn/springbootswagger/SpringbootSwaggerApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.lance.learn.springbootswagger; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class SpringbootSwaggerApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(SpringbootSwaggerApplication.class, args); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
SpringBoot-Swagger/src/main/java/com/lance/learn/springbootswagger/bean/Book.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.lance.learn.springbootswagger.bean; | ||
|
||
import lombok.Data; | ||
|
||
import java.util.Date; | ||
|
||
/** | ||
* @author lance(ZYH) | ||
* @function Book实体类 | ||
* @date 2018-07-09 21:37 | ||
*/ | ||
@Data | ||
public class Book { | ||
private Long id; | ||
private String name; | ||
private Integer price; | ||
private String author; | ||
private Date publishDate; | ||
} |
15 changes: 15 additions & 0 deletions
15
SpringBoot-Swagger/src/main/java/com/lance/learn/springbootswagger/bean/User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.lance.learn.springbootswagger.bean; | ||
|
||
import lombok.Data; | ||
|
||
/** | ||
* @author lance(ZYH) | ||
* @function | ||
* @date 2018-07-09 21:54 | ||
*/ | ||
@Data | ||
public class User { | ||
private Long id; | ||
private String username; | ||
private String password; | ||
} |
54 changes: 54 additions & 0 deletions
54
...r/src/main/java/com/lance/learn/springbootswagger/configuration/SwaggerConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.lance.learn.springbootswagger.configuration; | ||
|
||
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.service.Contact; | ||
import springfox.documentation.spi.DocumentationType; | ||
import springfox.documentation.spring.web.plugins.Docket; | ||
import springfox.documentation.swagger2.annotations.EnableSwagger2; | ||
|
||
/** | ||
* @author lance(ZYH) | ||
* @function Swagger启动配置类 | ||
* @date 2018-07-09 21:24 | ||
*/ | ||
@Configuration | ||
@EnableSwagger2 | ||
public class SwaggerConfiguration { | ||
|
||
/** | ||
* swagger2的配置文件,这里可以配置swagger2的一些基本的内容,比如扫描的包等等 | ||
* @return | ||
*/ | ||
@Bean | ||
public Docket createRestfulApi(){ | ||
return new Docket(DocumentationType.SWAGGER_2) | ||
.pathMapping("/") | ||
.select() | ||
.apis(RequestHandlerSelectors.basePackage("com.lance.learn.springbootswagger.controller")) //暴露接口地址的包路径 | ||
.paths(PathSelectors.any()) | ||
.build(); | ||
} | ||
|
||
/** | ||
* 构建 api文档的详细信息函数,注意这里的注解引用的是哪个 | ||
* @return | ||
*/ | ||
private ApiInfo apiInfo(){ | ||
return new ApiInfoBuilder() | ||
//页面标题 | ||
.title("Spring Boot 测试使用 Swagger2 构建RESTful API") | ||
//创建人 | ||
.contact(new Contact("LanveToBigData", "http://www.cnblogs.com/zhangyinhua/", "[email protected]")) | ||
//版本号 | ||
.version("1.0") | ||
//描述 | ||
.description("API 描述") | ||
.build(); | ||
} | ||
|
||
} |
69 changes: 69 additions & 0 deletions
69
...ot-Swagger/src/main/java/com/lance/learn/springbootswagger/controller/BookController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.lance.learn.springbootswagger.controller; | ||
|
||
import com.lance.learn.springbootswagger.bean.Book; | ||
import io.swagger.annotations.ApiImplicitParam; | ||
import io.swagger.annotations.ApiImplicitParams; | ||
import io.swagger.annotations.ApiOperation; | ||
import org.springframework.web.bind.annotation.*; | ||
import springfox.documentation.annotations.ApiIgnore; | ||
|
||
import java.util.*; | ||
|
||
/** | ||
* @author lance(ZYH) | ||
* @function | ||
* @date 2018-07-09 21:39 | ||
*/ | ||
@RestController | ||
@RequestMapping(value = "/bookcurd") | ||
public class BookController { | ||
Map<Long, Book> books = Collections.synchronizedMap(new HashMap<Long, Book>()); | ||
|
||
@ApiOperation(value="获取图书列表", notes="获取图书列表") | ||
@RequestMapping(value={""}, method= RequestMethod.GET) | ||
public List<Book> getBook() { | ||
List<Book> book = new ArrayList<>(books.values()); | ||
return book; | ||
} | ||
|
||
@ApiOperation(value="创建图书", notes="创建图书") | ||
@ApiImplicitParam(name = "book", value = "图书详细实体", required = true, dataType = "Book") | ||
@RequestMapping(value="", method=RequestMethod.POST) | ||
public String postBook(@RequestBody Book book) { | ||
books.put(book.getId(), book); | ||
return "success"; | ||
} | ||
@ApiOperation(value="获图书细信息", notes="根据url的id来获取详细信息") | ||
@ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long",paramType = "path") | ||
@RequestMapping(value="/{id}", method=RequestMethod.GET) | ||
public Book getBook(@PathVariable Long id) { | ||
return books.get(id); | ||
} | ||
|
||
@ApiOperation(value="更新信息", notes="根据url的id来指定更新图书信息") | ||
@ApiImplicitParams({ | ||
@ApiImplicitParam(name = "id", value = "图书ID", required = true, dataType = "Long",paramType = "path"), | ||
@ApiImplicitParam(name = "book", value = "图书实体book", required = true, dataType = "Book") | ||
}) | ||
@RequestMapping(value="/{id}", method= RequestMethod.PUT) | ||
public String putUser(@PathVariable Long id, @RequestBody Book book) { | ||
Book book1 = books.get(id); | ||
book1.setName(book.getName()); | ||
book1.setPrice(book.getPrice()); | ||
books.put(id, book1); | ||
return "success"; | ||
} | ||
@ApiOperation(value="删除图书", notes="根据url的id来指定删除图书") | ||
@ApiImplicitParam(name = "id", value = "图书ID", required = true, dataType = "Long",paramType = "path") | ||
@RequestMapping(value="/{id}", method=RequestMethod.DELETE) | ||
public String deleteUser(@PathVariable Long id) { | ||
books.remove(id); | ||
return "success"; | ||
} | ||
|
||
@ApiIgnore//使用该注解忽略这个API | ||
@RequestMapping(value = "/hi", method = RequestMethod.GET) | ||
public String jsonTest() { | ||
return " hi you!"; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...ot-Swagger/src/main/java/com/lance/learn/springbootswagger/controller/UserController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.lance.learn.springbootswagger.controller; | ||
|
||
import com.lance.learn.springbootswagger.bean.User; | ||
import io.swagger.annotations.ApiImplicitParam; | ||
import io.swagger.annotations.ApiOperation; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
/** | ||
* @author lance(ZYH) | ||
* @function | ||
* @date 2018-07-09 21:55 | ||
*/ | ||
@RestController | ||
@RequestMapping(value = "/user") | ||
public class UserController { | ||
|
||
|
||
@ApiOperation(value = "查询用户", notes = "根据UserId对象查询") | ||
@ApiImplicitParam(name = "userId", value = "id", required = true, dataType = "Long", paramType = "path") | ||
@RequestMapping(method = RequestMethod.GET, value = "{id}") | ||
public User findById(@PathVariable("id") Long id) { | ||
return new User(); | ||
} | ||
|
||
@ApiOperation(value = "添加用户", notes = "根据User对象添加") | ||
@ApiImplicitParam(name = "user", value = "user", required = true, dataType = "User") | ||
@RequestMapping(method = RequestMethod.GET, value = "add") | ||
public User insert() { | ||
User user = new User(); | ||
user.setId(Long.valueOf("123")); | ||
user.setUsername("张三"); | ||
user.setPassword("1234567"); | ||
return user; | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...gger/src/main/java/com/lance/learn/springbootswagger/controller/UserDetailController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.lance.learn.springbootswagger.controller; | ||
|
||
import com.lance.learn.springbootswagger.bean.User; | ||
import io.swagger.annotations.ApiImplicitParam; | ||
import io.swagger.annotations.ApiImplicitParams; | ||
import io.swagger.annotations.ApiOperation; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.*; | ||
|
||
/** | ||
* @author lance(ZYH) | ||
* @function | ||
* @date 2018-07-09 22:00 | ||
*/ | ||
|
||
@RestController | ||
@RequestMapping(value="/users") | ||
public class UserDetailController { | ||
static Map<Long, User> users = Collections.synchronizedMap(new HashMap<Long, User>()); | ||
|
||
@ApiOperation(value="获取用户列表", notes="") | ||
@RequestMapping(value={""}, method= RequestMethod.GET) | ||
public List<User> getUserList() { | ||
List<User> r = new ArrayList<User>(users.values()); | ||
return r; | ||
} | ||
|
||
@ApiOperation(value="创建用户", notes="根据User对象创建用户") | ||
@ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User") | ||
@RequestMapping(value="", method=RequestMethod.POST) | ||
public String postUser(@RequestBody User user) { | ||
users.put(user.getId(), user); | ||
return "success"; | ||
} | ||
|
||
@ApiOperation(value="获取用户详细信息", notes="根据url的id来获取用户详细信息") | ||
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long") | ||
@RequestMapping(value="/{id}", method=RequestMethod.GET) | ||
public User getUser(@PathVariable Long id) { | ||
return users.get(id); | ||
} | ||
|
||
@ApiOperation(value="更新用户详细信息", notes="根据url的id来指定更新对象,并根据传过来的user信息来更新用户详细信息") | ||
@ApiImplicitParams({ | ||
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long"), | ||
@ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User") | ||
}) | ||
@RequestMapping(value="/{id}", method=RequestMethod.PUT) | ||
public String putUser(@PathVariable Long id, @RequestBody User user) { | ||
User u = new User(); | ||
users.put(id, u); | ||
return "success"; | ||
} | ||
|
||
@ApiOperation(value="删除用户", notes="根据url的id来指定删除对象") | ||
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long") | ||
@RequestMapping(value="/{id}", method=RequestMethod.DELETE) | ||
public String deleteUser(@PathVariable Long id) { | ||
users.remove(id); | ||
return "success"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
server.port=8003 |
16 changes: 16 additions & 0 deletions
16
...er/src/test/java/com/lance/learn/springbootswagger/SpringbootSwaggerApplicationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.lance.learn.springbootswagger; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest | ||
public class SpringbootSwaggerApplicationTests { | ||
|
||
@Test | ||
public void contextLoads() { | ||
} | ||
|
||
} |