From a7856e4f8419a6711da637c8f662e12b7dd581e8 Mon Sep 17 00:00:00 2001 From: Gary Date: Fri, 12 Jan 2024 12:26:09 +0800 Subject: [PATCH] 1.add swagger for music --- luoo_common/pom.xml | 54 ++++++++++------- .../java/com/luoo/music/MusicApplication.java | 27 ++++++++- .../com/luoo/music/config/SwaggerConfig.java | 27 +++++++++ .../com/luoo/music/config/WebMvcConfig.java | 15 +++++ luoo_user/pom.xml | 11 ---- .../{swagger => config}/SwaggerConfig.java | 4 +- .../luoo/user/swagger/BaseSwaggerConfig.java | 58 ------------------- .../luoo/user/swagger/SwaggerProperties.java | 47 --------------- pom.xml | 54 +++++++++++------ 9 files changed, 136 insertions(+), 161 deletions(-) create mode 100644 luoo_music/src/main/java/com/luoo/music/config/SwaggerConfig.java create mode 100644 luoo_music/src/main/java/com/luoo/music/config/WebMvcConfig.java rename luoo_user/src/main/java/com/luoo/user/{swagger => config}/SwaggerConfig.java (88%) delete mode 100644 luoo_user/src/main/java/com/luoo/user/swagger/BaseSwaggerConfig.java delete mode 100644 luoo_user/src/main/java/com/luoo/user/swagger/SwaggerProperties.java diff --git a/luoo_common/pom.xml b/luoo_common/pom.xml index 5ae7f32..c4a4784 100644 --- a/luoo_common/pom.xml +++ b/luoo_common/pom.xml @@ -1,23 +1,33 @@ - - 4.0.0 - - com.luoo - luoo_parent - 1.0-SNAPSHOT - - luoo_common - - - io.jsonwebtoken - jjwt - 0.9.1 - - - - org.apache.commons - commons-lang3 - 3.14.0 - - - + + 4.0.0 + + com.luoo + luoo_parent + 1.0-SNAPSHOT + + luoo_common + + + io.jsonwebtoken + jjwt + 0.9.1 + + + + org.apache.commons + commons-lang3 + 3.14.0 + + + + io.springfox + springfox-swagger2 + + + com.github.xiaoymin + swagger-bootstrap-ui + + diff --git a/luoo_music/src/main/java/com/luoo/music/MusicApplication.java b/luoo_music/src/main/java/com/luoo/music/MusicApplication.java index e99a463..2a5bcfd 100644 --- a/luoo_music/src/main/java/com/luoo/music/MusicApplication.java +++ b/luoo_music/src/main/java/com/luoo/music/MusicApplication.java @@ -1,11 +1,19 @@ package com.luoo.music; +import java.net.InetAddress; +import java.net.UnknownHostException; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.openfeign.EnableFeignClients; +import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Bean; +import org.springframework.core.env.Environment; + import util.IdWorker; @SpringBootApplication @EnableEurekaClient @@ -13,9 +21,22 @@ import util.IdWorker; @EnableFeignClients @EnableCaching public class MusicApplication { - - public static void main(String[] args) { - SpringApplication.run(MusicApplication.class, args); + static Logger logger= LoggerFactory.getLogger(MusicApplication.class); + public static void main(String[] args) throws UnknownHostException { + ConfigurableApplicationContext application=SpringApplication.run(MusicApplication.class, args); + Environment env = application.getEnvironment(); + logger.info("\n----------------------------------------------------------\n\t" + + "Application '{}' is running! Access URLs:\n\t" + + "Local: \t\thttp://localhost:{}\n\t" + + "External: \thttp://{}:{}\n\t"+ + "Doc: \thttp://{}:{}/doc.html\n"+ + "----------------------------------------------------------", + env.getProperty("spring.application.name"), + env.getProperty("server.port"), + InetAddress.getLocalHost().getHostAddress(), + env.getProperty("server.port"), + InetAddress.getLocalHost().getHostAddress(), + env.getProperty("server.port")); } @Bean diff --git a/luoo_music/src/main/java/com/luoo/music/config/SwaggerConfig.java b/luoo_music/src/main/java/com/luoo/music/config/SwaggerConfig.java new file mode 100644 index 0000000..f9310c8 --- /dev/null +++ b/luoo_music/src/main/java/com/luoo/music/config/SwaggerConfig.java @@ -0,0 +1,27 @@ +package com.luoo.music.config; + +import org.springframework.context.annotation.Configuration; + +import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI; + +import springfox.documentation.swagger2.annotations.EnableSwagger2; +import swagger.BaseSwaggerConfig; +import swagger.SwaggerProperties; + +@Configuration +@EnableSwagger2 +@EnableSwaggerBootstrapUI +public class SwaggerConfig extends BaseSwaggerConfig { + + @Override + public SwaggerProperties swaggerProperties() { + return SwaggerProperties.builder() + .apiBasePackage("com.luoo.music.controller") + .title("luoo-music API") + .description("luoo-music 后端接口文档") + .contactName("locust") + .version("1.0") + .enableSecurity(false) + .build(); + } +} diff --git a/luoo_music/src/main/java/com/luoo/music/config/WebMvcConfig.java b/luoo_music/src/main/java/com/luoo/music/config/WebMvcConfig.java new file mode 100644 index 0000000..4069851 --- /dev/null +++ b/luoo_music/src/main/java/com/luoo/music/config/WebMvcConfig.java @@ -0,0 +1,15 @@ +package com.luoo.music.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; + +@Configuration +public class WebMvcConfig extends WebMvcConfigurationSupport { + @Override + protected void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/"); + registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/"); + super.addResourceHandlers(registry); + } +} diff --git a/luoo_user/pom.xml b/luoo_user/pom.xml index 3b65228..ab9fab6 100644 --- a/luoo_user/pom.xml +++ b/luoo_user/pom.xml @@ -61,17 +61,6 @@ lombok true - - - io.springfox - springfox-swagger2 - 2.8.0 - - - com.github.xiaoymin - swagger-bootstrap-ui - 1.9.3 - com.apifan.common common-random diff --git a/luoo_user/src/main/java/com/luoo/user/swagger/SwaggerConfig.java b/luoo_user/src/main/java/com/luoo/user/config/SwaggerConfig.java similarity index 88% rename from luoo_user/src/main/java/com/luoo/user/swagger/SwaggerConfig.java rename to luoo_user/src/main/java/com/luoo/user/config/SwaggerConfig.java index 7023726..31a8825 100644 --- a/luoo_user/src/main/java/com/luoo/user/swagger/SwaggerConfig.java +++ b/luoo_user/src/main/java/com/luoo/user/config/SwaggerConfig.java @@ -1,10 +1,12 @@ -package com.luoo.user.swagger; +package com.luoo.user.config; import org.springframework.context.annotation.Configuration; import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI; import springfox.documentation.swagger2.annotations.EnableSwagger2; +import swagger.BaseSwaggerConfig; +import swagger.SwaggerProperties; @Configuration @EnableSwagger2 diff --git a/luoo_user/src/main/java/com/luoo/user/swagger/BaseSwaggerConfig.java b/luoo_user/src/main/java/com/luoo/user/swagger/BaseSwaggerConfig.java deleted file mode 100644 index 2ed11b5..0000000 --- a/luoo_user/src/main/java/com/luoo/user/swagger/BaseSwaggerConfig.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.luoo.user.swagger; - -import org.springframework.context.annotation.Bean; -import org.springframework.core.annotation.Order; -import org.springframework.web.bind.annotation.RequestMethod; - -import io.swagger.annotations.ApiOperation; -import springfox.documentation.builders.ApiInfoBuilder; -import springfox.documentation.builders.PathSelectors; -import springfox.documentation.builders.RequestHandlerSelectors; -import springfox.documentation.builders.ResponseMessageBuilder; -import springfox.documentation.schema.ModelRef; -import springfox.documentation.service.*; -import springfox.documentation.spi.DocumentationType; -import springfox.documentation.spring.web.plugins.Docket; -import java.util.ArrayList; -import java.util.List; - -public abstract class BaseSwaggerConfig { - - @Bean - @Order(value = 1) - public Docket createRestApi() { - List responseMessageList =new ArrayList<>(); - responseMessageList.add(new ResponseMessageBuilder().code(200).message("成功").responseModel(new ModelRef("成功")).build()); - responseMessageList.add(new ResponseMessageBuilder().code(20001).message("失败").responseModel(new ModelRef("失败")).build()); - responseMessageList.add(new ResponseMessageBuilder().code(20002).message("用户名或密码错误").responseModel(new ModelRef("用户名或密码错误")).build()); - responseMessageList.add(new ResponseMessageBuilder().code(20003).message("无权访问").responseModel(new ModelRef("无权访问")).build()); - responseMessageList.add(new ResponseMessageBuilder().code(20009).message("重复提交").responseModel(new ModelRef("重复提交")).build()); - SwaggerProperties swaggerProperties = swaggerProperties(); - return new Docket(DocumentationType.SWAGGER_2) - .globalResponseMessage(RequestMethod.GET, responseMessageList) - .globalResponseMessage(RequestMethod.POST, responseMessageList) - .globalResponseMessage(RequestMethod.PUT, responseMessageList) - .globalResponseMessage(RequestMethod.DELETE, responseMessageList) - .apiInfo(apiInfo(swaggerProperties)) - .select() - .apis(RequestHandlerSelectors.basePackage(swaggerProperties.getApiBasePackage())) - //.apis(RequestHandlerSelectors.withClassAnnotation(ApiOperation.class)) - .paths(PathSelectors.any()) - .build(); - } - - private ApiInfo apiInfo(SwaggerProperties swaggerProperties) { - return new ApiInfoBuilder() - .title(swaggerProperties.getTitle()) - .description(swaggerProperties.getDescription()) - .contact(new Contact(swaggerProperties.getContactName(), swaggerProperties.getContactUrl(), swaggerProperties.getContactEmail())) - .version(swaggerProperties.getVersion()) - .build(); - } - - /** - * 自定义Swagger配置 - */ - public abstract SwaggerProperties swaggerProperties(); - -} diff --git a/luoo_user/src/main/java/com/luoo/user/swagger/SwaggerProperties.java b/luoo_user/src/main/java/com/luoo/user/swagger/SwaggerProperties.java deleted file mode 100644 index 3535e0b..0000000 --- a/luoo_user/src/main/java/com/luoo/user/swagger/SwaggerProperties.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.luoo.user.swagger; - -import lombok.Builder; -import lombok.Data; -import lombok.EqualsAndHashCode; - -/** - * Swagger自定义配置 - * Created by macro on 2020/7/16. - */ -@Data -@EqualsAndHashCode(callSuper = false) -@Builder -public class SwaggerProperties { - /** - * API文档生成基础路径 - */ - private String apiBasePackage; - /** - * 是否要启用登录认证 - */ - private boolean enableSecurity; - /** - * 文档标题 - */ - private String title; - /** - * 文档描述 - */ - private String description; - /** - * 文档版本 - */ - private String version; - /** - * 文档联系人姓名 - */ - private String contactName; - /** - * 文档联系人网址 - */ - private String contactUrl; - /** - * 文档联系人邮箱 - */ - private String contactEmail; -} diff --git a/pom.xml b/pom.xml index deb5c97..fe2da0b 100644 --- a/pom.xml +++ b/pom.xml @@ -1,40 +1,43 @@ - 4.0.0 com.luoo luoo_parent 1.0-SNAPSHOT - - luoo_common - luoo_recruit - luoo_user - luoo_music - luoo_gathering - luoo_comment + + luoo_common + luoo_recruit + luoo_user + luoo_music + luoo_gathering + luoo_comment luoo_eureka - luoo_friend - luoo_manage - luoo_web - luoo_sms - luoo_config - luoo_tag - - pom + luoo_friend + luoo_manage + luoo_web + luoo_sms + luoo_config + luoo_tag + + pom luoo_parent org.springframework.boot spring-boot-starter-parent 2.0.1.RELEASE - + UTF-8 UTF-8 1.8 + 2.8.0 + 1.9.3 @@ -46,6 +49,17 @@ pom import + + + io.springfox + springfox-swagger2 + ${springfox-swagger2.version} + + + com.github.xiaoymin + swagger-bootstrap-ui + ${swagger-bootstrap-ui.version} + @@ -64,11 +78,13 @@ org.springframework.cloud spring-cloud-starter-config - + + org.projectlombok + lombok + - spring-snapshots