From 3194da665339af599905a76ee7431fe83cebfe56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E7=8C=BFDD?= Date: Mon, 18 Apr 2016 14:52:18 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8swagger=E6=9E=84=E5=BB=BArest?= =?UTF-8?q?ful=20api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Chapter3-1-5/pom.xml | 66 +++++++++++++++++++ .../main/java/com/didispace/Application.java | 22 +++++++ .../src/main/java/com/didispace/Swagger2.java | 45 +++++++++++++ .../main/java/com/didispace/domain/User.java | 41 ++++++++++++ .../com/didispace/web/HelloController.java | 22 +++++++ .../com/didispace/web/UserController.java | 62 +++++++++++++++++ .../src/main/resources/application.properties | 0 7 files changed, 258 insertions(+) create mode 100755 Chapter3-1-5/pom.xml create mode 100755 Chapter3-1-5/src/main/java/com/didispace/Application.java create mode 100644 Chapter3-1-5/src/main/java/com/didispace/Swagger2.java create mode 100755 Chapter3-1-5/src/main/java/com/didispace/domain/User.java create mode 100755 Chapter3-1-5/src/main/java/com/didispace/web/HelloController.java create mode 100755 Chapter3-1-5/src/main/java/com/didispace/web/UserController.java create mode 100755 Chapter3-1-5/src/main/resources/application.properties diff --git a/Chapter3-1-5/pom.xml b/Chapter3-1-5/pom.xml new file mode 100755 index 0000000..6906818 --- /dev/null +++ b/Chapter3-1-5/pom.xml @@ -0,0 +1,66 @@ + + + 4.0.0 + + com.didispace + SpringBootWithSwagger + 1.0.0 + jar + + SpringBootWithSwagger + SpringBootWithSwagger + + + org.springframework.boot + spring-boot-starter-parent + 1.3.2.RELEASE + + + + + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.springframework.boot + spring-boot-starter-web + + + + io.springfox + springfox-swagger2 + 2.2.2 + + + io.springfox + springfox-swagger-ui + 2.2.2 + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + diff --git a/Chapter3-1-5/src/main/java/com/didispace/Application.java b/Chapter3-1-5/src/main/java/com/didispace/Application.java new file mode 100755 index 0000000..f7a9e16 --- /dev/null +++ b/Chapter3-1-5/src/main/java/com/didispace/Application.java @@ -0,0 +1,22 @@ +package com.didispace; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * + * @author 程序猿DD + * @version 1.0.0 + * @blog http://blog.didispace.com + * + */ +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + + SpringApplication.run(Application.class, args); + + } + +} diff --git a/Chapter3-1-5/src/main/java/com/didispace/Swagger2.java b/Chapter3-1-5/src/main/java/com/didispace/Swagger2.java new file mode 100644 index 0000000..c014b4e --- /dev/null +++ b/Chapter3-1-5/src/main/java/com/didispace/Swagger2.java @@ -0,0 +1,45 @@ +package com.didispace; + +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; + +/** + * @author 程序猿DD + * @version 1.0.0 + * @date 16/4/18 下午12:02. + * @blog http://blog.didispace.com + */ +@Configuration +@EnableSwagger2 +public class Swagger2 { + + @Bean + public Docket createRestApi() { + return new Docket(DocumentationType.SWAGGER_2) + .apiInfo(apiInfo()) + .select() + .apis(RequestHandlerSelectors.basePackage("com.didispace.web")) + .paths(PathSelectors.any()) + .build(); + } + + private ApiInfo apiInfo() { + return new ApiInfoBuilder() + .title("Spring Boot中使用Swagger2构建RESTful APIs") + .description("更多Spring Boot相关文章请关注:http://blog.didispace.com/") + .termsOfServiceUrl("http://blog.didispace.com/") + .contact("程序猿DD") + .license("") + .licenseUrl("") + .version("1.0") + .build(); + } + +} diff --git a/Chapter3-1-5/src/main/java/com/didispace/domain/User.java b/Chapter3-1-5/src/main/java/com/didispace/domain/User.java new file mode 100755 index 0000000..208cfe3 --- /dev/null +++ b/Chapter3-1-5/src/main/java/com/didispace/domain/User.java @@ -0,0 +1,41 @@ +package com.didispace.domain; + + + +/** + * + * @author 程序猿DD + * @version 1.0.0 + * @blog http://blog.didispace.com + * + */ +public class User { + + private Long id; + private String name; + private Integer age; + + 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; + } +} diff --git a/Chapter3-1-5/src/main/java/com/didispace/web/HelloController.java b/Chapter3-1-5/src/main/java/com/didispace/web/HelloController.java new file mode 100755 index 0000000..1e82ee4 --- /dev/null +++ b/Chapter3-1-5/src/main/java/com/didispace/web/HelloController.java @@ -0,0 +1,22 @@ +package com.didispace.web; + +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +/** + * + * @author 程序猿DD + * @version 1.0.0 + * @blog http://blog.didispace.com + * + */ +@RestController +public class HelloController { + + @RequestMapping(value = "/hello", method = RequestMethod.GET) + public String index() { + return "Hello World"; + } + +} \ No newline at end of file diff --git a/Chapter3-1-5/src/main/java/com/didispace/web/UserController.java b/Chapter3-1-5/src/main/java/com/didispace/web/UserController.java new file mode 100755 index 0000000..f5c7ee1 --- /dev/null +++ b/Chapter3-1-5/src/main/java/com/didispace/web/UserController.java @@ -0,0 +1,62 @@ +package com.didispace.web; + +import com.didispace.domain.User; + +import java.util.*; + +import org.springframework.web.bind.annotation.*; + +/** + * + * @author 程序猿DD + * @version 1.0.0 + * @blog http://blog.didispace.com + * + */ +@RestController +@RequestMapping(value="/users") // 通过这里配置使下面的映射都在/users下,可去除 +public class UserController { + + static Map users = Collections.synchronizedMap(new HashMap()); + + @RequestMapping(value="/", method=RequestMethod.GET) + public List getUserList() { + // 处理"/users/"的GET请求,用来获取用户列表 + // 还可以通过@RequestParam从页面中传递参数来进行查询条件或者翻页信息的传递 + List r = new ArrayList(users.values()); + return r; + } + + @RequestMapping(value="/", method=RequestMethod.POST) + public String postUser(@ModelAttribute User user) { + // 处理"/users/"的POST请求,用来创建User + // 除了@ModelAttribute绑定参数之外,还可以通过@RequestParam从页面中传递参数 + users.put(user.getId(), user); + return "success"; + } + + @RequestMapping(value="/{id}", method=RequestMethod.GET) + public User getUser(@PathVariable Long id) { + // 处理"/users/{id}"的GET请求,用来获取url中id值的User信息 + // url中的id可通过@PathVariable绑定到函数的参数中 + return users.get(id); + } + + @RequestMapping(value="/{id}", method=RequestMethod.PUT) + public String putUser(@PathVariable Long id, @ModelAttribute User user) { + // 处理"/users/{id}"的PUT请求,用来更新User信息 + User u = users.get(id); + u.setName(user.getName()); + u.setAge(user.getAge()); + users.put(id, u); + return "success"; + } + + @RequestMapping(value="/{id}", method=RequestMethod.DELETE) + public String deleteUser(@PathVariable Long id) { + // 处理"/users/{id}"的DELETE请求,用来删除User + users.remove(id); + return "success"; + } + +} \ No newline at end of file diff --git a/Chapter3-1-5/src/main/resources/application.properties b/Chapter3-1-5/src/main/resources/application.properties new file mode 100755 index 0000000..e69de29