diff --git a/springboot-jpa-demo/src/main/java/com/cunyu/springbootjpademo/controller/UserController.java b/springboot-jpa-demo/src/main/java/com/cunyu/springbootjpademo/controller/UserController.java index 98fa0638207becc192881441b15723d654da5d1d..ac27d6d37cb27c0de494c5469917f17bb554244b 100644 --- a/springboot-jpa-demo/src/main/java/com/cunyu/springbootjpademo/controller/UserController.java +++ b/springboot-jpa-demo/src/main/java/com/cunyu/springbootjpademo/controller/UserController.java @@ -59,6 +59,11 @@ public class UserController { userRepository.save(user); } + /** + * 查询所有用户 + * + * @return + */ @PostMapping("/query") public List queryUsers() { List userList = userRepository.findAll(); diff --git a/springboot-swagger3-demo/pom.xml b/springboot-swagger3-demo/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..c41cc4843631456a04f889188fd21f68e612ceac --- /dev/null +++ b/springboot-swagger3-demo/pom.xml @@ -0,0 +1,70 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.5.6 + + + com.cunyu + springboot-swagger3-demo + 0.0.1-SNAPSHOT + springboot-swagger3-demo + springboot-swagger3-demo + + 1.8 + + + + io.springfox + springfox-boot-starter + 3.0.0 + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-devtools + runtime + true + + + org.springframework.boot + spring-boot-configuration-processor + true + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.projectlombok + lombok + + + + + + + + diff --git a/springboot-swagger3-demo/src/main/java/com/cunyu/springbootswagger3demo/SpringbootSwagger3DemoApplication.java b/springboot-swagger3-demo/src/main/java/com/cunyu/springbootswagger3demo/SpringbootSwagger3DemoApplication.java new file mode 100644 index 0000000000000000000000000000000000000000..db40dac925186b18c48eace5718f0ff03aac0d80 --- /dev/null +++ b/springboot-swagger3-demo/src/main/java/com/cunyu/springbootswagger3demo/SpringbootSwagger3DemoApplication.java @@ -0,0 +1,15 @@ +package com.cunyu.springbootswagger3demo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import springfox.documentation.oas.annotations.EnableOpenApi; + +@EnableOpenApi +@SpringBootApplication +public class SpringbootSwagger3DemoApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringbootSwagger3DemoApplication.class, args); + } + +} diff --git a/springboot-swagger3-demo/src/main/java/com/cunyu/springbootswagger3demo/config/SwaggerConfig.java b/springboot-swagger3-demo/src/main/java/com/cunyu/springbootswagger3demo/config/SwaggerConfig.java new file mode 100644 index 0000000000000000000000000000000000000000..e826d2123f5e66376efe1e350f430cf471324534 --- /dev/null +++ b/springboot-swagger3-demo/src/main/java/com/cunyu/springbootswagger3demo/config/SwaggerConfig.java @@ -0,0 +1,71 @@ +package com.cunyu.springbootswagger3demo.config; + +import org.springframework.beans.factory.annotation.Value; +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.oas.annotations.EnableOpenApi; +import springfox.documentation.service.ApiInfo; +import springfox.documentation.service.Contact; +import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spring.web.plugins.Docket; + +import java.util.ArrayList; + +/** + * Created with IntelliJ IDEA. + * + * @author : 村雨遥 + * @version : 1.0 + * @project : springboot-swagger3-demo + * @package : com.cunyu.springbootswagger3demo.config + * @className : SwaggerConfig + * @createTime : 2022/1/6 14:19 + * @email : 747731461@qq.com + * @微信 : cunyu1024 + * @公众号 : 村雨遥 + * @网站 : https://cunyu1943.github.io + * @description : + */ + +@Configuration +@EnableOpenApi +public class SwaggerConfig { + /** + * 用于读取配置文件 application.properties 中 swagger 属性是否开启 + */ + @Value("${swagger.enabled}") + Boolean swaggerEnabled; + + @Bean + public Docket docket() { + return new Docket(DocumentationType.OAS_30) + .apiInfo(apiInfo()) + // 是否开启swagger + .enable(swaggerEnabled) + .select() + // 过滤条件,扫描指定路径下的文件 + .apis(RequestHandlerSelectors.basePackage("com.cunyu.springbootswagger3demo.controller")) + // 指定路径处理,PathSelectors.any()代表不过滤任何路径 + //.paths(PathSelectors.any()) + .build(); + } + + private ApiInfo apiInfo() { + /*作者信息*/ + Contact contact = new Contact("村雨遥", "https://cunyu1943.github.io", "747731461@qq.com"); + return new ApiInfo( + "Spring Boot 集成 Swagger3 测试", + "Spring Boot 集成 Swagger3 测试接口文档", + "v1.0", + "https://cunyu1943.github.io", + contact, + "Apache 2.0", + "http://www.apache.org/licenses/LICENSE-2.0", + new ArrayList() + ); + } +} + diff --git a/springboot-swagger3-demo/src/main/java/com/cunyu/springbootswagger3demo/controller/UserController.java b/springboot-swagger3-demo/src/main/java/com/cunyu/springbootswagger3demo/controller/UserController.java new file mode 100644 index 0000000000000000000000000000000000000000..a1ad8e2d10e182404922a5f32d3d3ac9b5ee6ab1 --- /dev/null +++ b/springboot-swagger3-demo/src/main/java/com/cunyu/springbootswagger3demo/controller/UserController.java @@ -0,0 +1,37 @@ +package com.cunyu.springbootswagger3demo.controller; + +import com.cunyu.springbootswagger3demo.entity.User; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * Created with IntelliJ IDEA. + * + * @author : 村雨遥 + * @version : 1.0 + * @project : springboot-swagger3-demo + * @package : com.cunyu.springbootswagger3demo.controller + * @className : UserController + * @createTime : 2022/1/6 11:02 + * @email : 747731461@qq.com + * @微信 : cunyu1024 + * @公众号 : 村雨遥 + * @网站 : https://cunyu1943.github.io + * @description : + */ + +@Api(tags = "测试") +@RestController +@RequestMapping("/user") +public class UserController { + + @ApiOperation("测试接口") + @PostMapping("/show") + public String show(@RequestBody User user) { + return "hello," + user.getName() + ",welcome to springboot swagger3!"; + } +} diff --git a/springboot-swagger3-demo/src/main/java/com/cunyu/springbootswagger3demo/entity/User.java b/springboot-swagger3-demo/src/main/java/com/cunyu/springbootswagger3demo/entity/User.java new file mode 100644 index 0000000000000000000000000000000000000000..a5507fcdfd1b02b3c438b20ef60c2110ff0ab104 --- /dev/null +++ b/springboot-swagger3-demo/src/main/java/com/cunyu/springbootswagger3demo/entity/User.java @@ -0,0 +1,32 @@ +package com.cunyu.springbootswagger3demo.entity; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * Created with IntelliJ IDEA. + * + * @author : 村雨遥 + * @version : 1.0 + * @project : springboot-swagger3-demo + * @package : com.cunyu.springbootswagger3demo.entity + * @className : User + * @createTime : 2022/1/6 11:17 + * @email : 747731461@qq.com + * @微信 : cunyu1024 + * @公众号 : 村雨遥 + * @网站 : https://cunyu1943.github.io + * @description : + */ + +@Data +@AllArgsConstructor +@NoArgsConstructor +@ApiModel("用户实体类") +public class User { + @ApiModelProperty(value = "姓名", required = true, example = "村雨遥") + private String name; +} diff --git a/springboot-swagger3-demo/src/main/resources/application.yml b/springboot-swagger3-demo/src/main/resources/application.yml new file mode 100644 index 0000000000000000000000000000000000000000..7a8e6e1a51c4976f595ed297194adf83213addd9 --- /dev/null +++ b/springboot-swagger3-demo/src/main/resources/application.yml @@ -0,0 +1,9 @@ +server: + port: 8080 + +swagger: + enabled: true + +spring: + application: + name: springbooot-swagger3-demo diff --git a/springboot-swagger3-demo/src/test/java/com/cunyu/springbootswagger3demo/SpringbootSwagger3DemoApplicationTests.java b/springboot-swagger3-demo/src/test/java/com/cunyu/springbootswagger3demo/SpringbootSwagger3DemoApplicationTests.java new file mode 100644 index 0000000000000000000000000000000000000000..ccc628613ef47f7eda873202b7383f7223be98c8 --- /dev/null +++ b/springboot-swagger3-demo/src/test/java/com/cunyu/springbootswagger3demo/SpringbootSwagger3DemoApplicationTests.java @@ -0,0 +1,13 @@ +package com.cunyu.springbootswagger3demo; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class SpringbootSwagger3DemoApplicationTests { + + @Test + void contextLoads() { + } + +}