diff --git a/SpringBootSwagger2/.classpath b/SpringBootSwagger2/.classpath new file mode 100644 index 0000000..d2bc4e0 --- /dev/null +++ b/SpringBootSwagger2/.classpath @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/SpringBootSwagger2/.project b/SpringBootSwagger2/.project new file mode 100644 index 0000000..0c95dcb --- /dev/null +++ b/SpringBootSwagger2/.project @@ -0,0 +1,34 @@ + + + SpringBootSwagger2 + + + + + + org.eclipse.wst.common.project.facet.core.builder + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + org.springframework.ide.eclipse.boot.validation.springbootbuilder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + org.eclipse.wst.common.project.facet.core.nature + + diff --git a/SpringBootSwagger2/.settings/org.eclipse.core.resources.prefs b/SpringBootSwagger2/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..04cfa2c --- /dev/null +++ b/SpringBootSwagger2/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,6 @@ +eclipse.preferences.version=1 +encoding//src/main/java=UTF-8 +encoding//src/main/resources=UTF-8 +encoding//src/test/java=UTF-8 +encoding//src/test/resources=UTF-8 +encoding/=UTF-8 diff --git a/SpringBootSwagger2/.settings/org.eclipse.jdt.core.prefs b/SpringBootSwagger2/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..107b45b --- /dev/null +++ b/SpringBootSwagger2/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,10 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.methodParameters=generate +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.release=disabled +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/SpringBootSwagger2/.settings/org.eclipse.m2e.core.prefs b/SpringBootSwagger2/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000..14b697b --- /dev/null +++ b/SpringBootSwagger2/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/SpringBootSwagger2/.settings/org.eclipse.wst.common.project.facet.core.xml b/SpringBootSwagger2/.settings/org.eclipse.wst.common.project.facet.core.xml new file mode 100644 index 0000000..017b6f4 --- /dev/null +++ b/SpringBootSwagger2/.settings/org.eclipse.wst.common.project.facet.core.xml @@ -0,0 +1,4 @@ + + + + diff --git a/SpringBootSwagger2/.settings/org.springframework.ide.eclipse.prefs b/SpringBootSwagger2/.settings/org.springframework.ide.eclipse.prefs new file mode 100644 index 0000000..e587c65 --- /dev/null +++ b/SpringBootSwagger2/.settings/org.springframework.ide.eclipse.prefs @@ -0,0 +1,2 @@ +boot.validation.initialized=true +eclipse.preferences.version=1 diff --git a/SpringBootSwagger2/pom.xml b/SpringBootSwagger2/pom.xml new file mode 100644 index 0000000..d5b97ff --- /dev/null +++ b/SpringBootSwagger2/pom.xml @@ -0,0 +1,57 @@ + + 4.0.0 + + zz + SpringBoot2 + 0.0.1-SNAPSHOT + + SpringBootSwagger2 + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-devtools + true + + + + + mysql + mysql-connector-java + 8.0.15 + + + + io.springfox + springfox-swagger2 + 2.6.1 + + + io.springfox + springfox-swagger-ui + 2.6.1 + + + + + + alimaven + aliyun maven + http://maven.aliyun.com/nexus/content/groups/public/ + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + \ No newline at end of file diff --git a/SpringBootSwagger2/src/main/java/com/zz/App.java b/SpringBootSwagger2/src/main/java/com/zz/App.java new file mode 100644 index 0000000..78a2e87 --- /dev/null +++ b/SpringBootSwagger2/src/main/java/com/zz/App.java @@ -0,0 +1,21 @@ +package com.zz; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.scheduling.annotation.EnableScheduling; +/** + * 右键--》run as application 运行正启动类的main方法,就可以启动这个springboot项目。 +SpringBoot 自带了 tomcat, 运行这个main方法 的时候,会同时启动tomcat + * @author jiyu + * + */ +@SpringBootApplication +@EnableScheduling +public class App { + + public static void main(String[] args) { + // TODO Auto-generated method stub + SpringApplication.run(App.class, args); + } + +} diff --git a/SpringBootSwagger2/src/main/java/com/zz/config/SwaggerConfig.java b/SpringBootSwagger2/src/main/java/com/zz/config/SwaggerConfig.java new file mode 100644 index 0000000..c31109d --- /dev/null +++ b/SpringBootSwagger2/src/main/java/com/zz/config/SwaggerConfig.java @@ -0,0 +1,34 @@ +package com.zz.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.service.Contact; +import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spring.web.plugins.Docket; +import springfox.documentation.swagger2.annotations.EnableSwagger2; + +@Configuration +@EnableSwagger2 +public class SwaggerConfig { + @Bean + public Docket buildDocket() { + return new Docket(DocumentationType.SWAGGER_2) + .apiInfo(buildApiInf()) + .select() + .apis(RequestHandlerSelectors.basePackage("com.zz.controller")) + .paths(PathSelectors.any()) + .build(); + } + private ApiInfo buildApiInf() { + return new ApiInfoBuilder() + .title("系统RESTful API文档") + .contact(new Contact("Bsea", "https://me.csdn.net/h356363", "yinyouhai@aliyun.com")) + .version("1.0") + .build(); + } +} \ No newline at end of file diff --git a/SpringBootSwagger2/src/main/java/com/zz/controller/UserController.java b/SpringBootSwagger2/src/main/java/com/zz/controller/UserController.java new file mode 100644 index 0000000..7372847 --- /dev/null +++ b/SpringBootSwagger2/src/main/java/com/zz/controller/UserController.java @@ -0,0 +1,119 @@ +package com.zz.controller; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +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.ResponseBody; + +import com.zz.entity.User; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiImplicitParam; +import io.swagger.annotations.ApiImplicitParams; +import io.swagger.annotations.ApiOperation; +import springfox.documentation.annotations.ApiIgnore; + +@Api(value = "用户Controller") +@Controller +@RequestMapping("user") +public class UserController { + + @ApiIgnore + @GetMapping("hello") + public @ResponseBody String hello() { + return "hello"; + } + + @ApiOperation(value = "获取用户信息", notes = "根据用户id获取用户信息") + @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "Long", paramType = "path") + @GetMapping("/{id}") + public @ResponseBody User getUserById(@PathVariable(value = "id") String id) { + User user = new User(); + user.setId(id); + user.setName("mrbird"); + user.setAge(25); + return user; + } + + @ApiOperation(value = "获取用户列表", notes = "获取用户列表") + @GetMapping("/list") + public @ResponseBody List getUserList() { + List list = new ArrayList<>(); + User user1 = new User(); + user1.setId("1l"); + user1.setName("mrbird"); + user1.setAge(25); + list.add(user1); + User user2 = new User(); + user2.setId("2l"); + user2.setName("scott"); + user2.setAge(29); + list.add(user2); + return list; + } + + @ApiOperation(value = "新增用户", notes = "根据用户实体创建用户") + @ApiImplicitParam(name = "user", value = "用户实体", required = true, dataType = "User") + @PostMapping("/add") + public @ResponseBody Map addUser(@RequestBody User user) { + Map map = new HashMap<>(); + map.put("result", "success"); + return map; + } + + @ApiOperation(value = "删除用户", notes = "根据用户id删除用户") + @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "Long", paramType = "path") + @DeleteMapping("/{id}") + public @ResponseBody Map deleteUser(@PathVariable(value = "id") Long id) { + Map map = new HashMap<>(); + map.put("result", "success"); + return map; + } + + @ApiOperation(value = "更新用户", notes = "根据用户id更新用户") + @ApiImplicitParams({ + @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "Long", paramType = "path"), + @ApiImplicitParam(name = "user", value = "用户实体", required = true, dataType = "User") }) + @PutMapping("/{id}") + public @ResponseBody Map updateUser(@PathVariable(value = "id") Long id, @RequestBody User user) { + Map map = new HashMap<>(); + map.put("result", "success"); + return map; + } + +} + +/** + * + * + * Swagger常用注解 +@Api:修饰整个类,描述Controller的作用; +@ApiOperation:描述一个类的一个方法,或者说一个接口; +@ApiParam:单个参数描述; +@ApiModel:用对象来接收参数; +@ApiProperty:用对象接收参数时,描述对象的一个字段; +@ApiResponse:HTTP响应其中1个描述; +@ApiResponses:HTTP响应整体描述; +@ApiIgnore:使用该注解忽略这个API; +@ApiError :发生错误返回的信息; +@ApiImplicitParam:一个请求参数; +@ApiImplicitParams:多个请求参数。 +编写RESTful API接口 +Spring Boot中包含了一些注解,对应于HTTP协议中的方法: +@GetMapping对应HTTP中的GET方法; +@PostMapping对应HTTP中的POST方法; +@PutMapping对应HTTP中的PUT方法; +@DeleteMapping对应HTTP中的DELETE方法; +@PatchMapping对应HTTP中的PATCH方法。 +8** +**/ diff --git a/SpringBootSwagger2/src/main/java/com/zz/entity/User.java b/SpringBootSwagger2/src/main/java/com/zz/entity/User.java new file mode 100644 index 0000000..c7729bf --- /dev/null +++ b/SpringBootSwagger2/src/main/java/com/zz/entity/User.java @@ -0,0 +1,43 @@ +package com.zz.entity; + +public class User { + + private String id; + private String name; + private String pwd; + private String sex; + private int age; + public String getId() { + return id; + } + public void setId(String id) { + this.id = id; + } + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public String getPwd() { + return pwd; + } + public void setPwd(String pwd) { + this.pwd = pwd; + } + public String getSex() { + return sex; + } + public void setSex(String sex) { + this.sex = sex; + } + public int getAge() { + return age; + } + public void setAge(int age) { + this.age = age; + } + + + +} diff --git a/SpringBootSwagger2/src/main/resources/application.properties b/SpringBootSwagger2/src/main/resources/application.properties new file mode 100644 index 0000000..28b152a --- /dev/null +++ b/SpringBootSwagger2/src/main/resources/application.properties @@ -0,0 +1,5 @@ +//设置项目的端口号 +server.port=9080 +//设置工程名字 +// 所有路径都需要加a. http:localhost:9080/a/all +server.servlet.context-path=/a \ No newline at end of file diff --git a/SpringBootSwagger2/target/classes/META-INF/maven/zz/SpringBootSwagger2/pom.properties b/SpringBootSwagger2/target/classes/META-INF/maven/zz/SpringBootSwagger2/pom.properties new file mode 100644 index 0000000..0a7837b --- /dev/null +++ b/SpringBootSwagger2/target/classes/META-INF/maven/zz/SpringBootSwagger2/pom.properties @@ -0,0 +1,7 @@ +#Generated by Maven Integration for Eclipse +#Fri Aug 23 00:45:38 CST 2019 +version=0.0.1-SNAPSHOT +groupId=zz +m2e.projectName=SpringBootSwagger2 +m2e.projectLocation=C\:\\Users\\76582\\Documents\\workspace-sts-3.9.8.RELEASE\\SpringBoot2\\SpringBootSwagger2 +artifactId=SpringBootSwagger2 diff --git a/SpringBootSwagger2/target/classes/META-INF/maven/zz/SpringBootSwagger2/pom.xml b/SpringBootSwagger2/target/classes/META-INF/maven/zz/SpringBootSwagger2/pom.xml new file mode 100644 index 0000000..d5b97ff --- /dev/null +++ b/SpringBootSwagger2/target/classes/META-INF/maven/zz/SpringBootSwagger2/pom.xml @@ -0,0 +1,57 @@ + + 4.0.0 + + zz + SpringBoot2 + 0.0.1-SNAPSHOT + + SpringBootSwagger2 + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-devtools + true + + + + + mysql + mysql-connector-java + 8.0.15 + + + + io.springfox + springfox-swagger2 + 2.6.1 + + + io.springfox + springfox-swagger-ui + 2.6.1 + + + + + + alimaven + aliyun maven + http://maven.aliyun.com/nexus/content/groups/public/ + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + \ No newline at end of file diff --git a/SpringBootSwagger2/target/classes/application.properties b/SpringBootSwagger2/target/classes/application.properties new file mode 100644 index 0000000..28b152a --- /dev/null +++ b/SpringBootSwagger2/target/classes/application.properties @@ -0,0 +1,5 @@ +//设置项目的端口号 +server.port=9080 +//设置工程名字 +// 所有路径都需要加a. http:localhost:9080/a/all +server.servlet.context-path=/a \ No newline at end of file diff --git a/SpringBootSwagger2/target/classes/com/zz/App.class b/SpringBootSwagger2/target/classes/com/zz/App.class new file mode 100644 index 0000000..f3b606d Binary files /dev/null and b/SpringBootSwagger2/target/classes/com/zz/App.class differ diff --git a/SpringBootSwagger2/target/classes/com/zz/config/SwaggerConfig.class b/SpringBootSwagger2/target/classes/com/zz/config/SwaggerConfig.class new file mode 100644 index 0000000..ef3a903 Binary files /dev/null and b/SpringBootSwagger2/target/classes/com/zz/config/SwaggerConfig.class differ diff --git a/SpringBootSwagger2/target/classes/com/zz/controller/UserController.class b/SpringBootSwagger2/target/classes/com/zz/controller/UserController.class new file mode 100644 index 0000000..1baa9d1 Binary files /dev/null and b/SpringBootSwagger2/target/classes/com/zz/controller/UserController.class differ diff --git a/SpringBootSwagger2/target/classes/com/zz/entity/User.class b/SpringBootSwagger2/target/classes/com/zz/entity/User.class new file mode 100644 index 0000000..c532b87 Binary files /dev/null and b/SpringBootSwagger2/target/classes/com/zz/entity/User.class differ diff --git a/pom.xml b/pom.xml index 42b435f..8f41d37 100644 --- a/pom.xml +++ b/pom.xml @@ -18,6 +18,7 @@ SpringBootThymeleaf SpringBootFile SpringBootHomework + SpringBootSwagger2