Loading escheduler-api/pom.xml +28 −8 Original line number Diff line number Diff line <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"> <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> <parent> <groupId>cn.analysys</groupId> Loading @@ -10,13 +11,10 @@ <dependencies> <dependency> <groupId>cn.analysys</groupId> <artifactId>escheduler-dao</artifactId> </dependency> <dependency> <groupId>cn.analysys</groupId> <artifactId>escheduler-common</artifactId> <artifactId>escheduler-server</artifactId> <exclusions> <exclusion> <groupId>io.netty</groupId> Loading @@ -38,7 +36,6 @@ </dependency> <!--springboot--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> Loading Loading @@ -142,6 +139,24 @@ <artifactId>quartz-jobs</artifactId> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>swagger-bootstrap-ui</artifactId> <version>1.9.3</version> </dependency> <dependency> <groupId>cn.analysys</groupId> <artifactId>escheduler-rpc</artifactId> Loading @@ -153,6 +168,11 @@ <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>io.swagger</groupId> <artifactId>swagger-jaxrs</artifactId> <version>1.5.12</version> </dependency> </dependencies> <build> Loading escheduler-api/src/main/java/cn/escheduler/api/ApiApplicationServer.java +7 −1 Original line number Diff line number Diff line Loading @@ -19,13 +19,19 @@ package cn.escheduler.api; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; import org.springframework.context.annotation.ComponentScan; import springfox.documentation.swagger2.annotations.EnableSwagger2; @SpringBootApplication @ServletComponentScan @ComponentScan("cn.escheduler") public class ApiApplicationServer { @EnableSwagger2 public class ApiApplicationServer extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(ApiApplicationServer.class, args); } } escheduler-api/src/main/java/cn/escheduler/api/configuration/AppConfiguration.java +46 −4 Original line number Diff line number Diff line Loading @@ -20,6 +20,12 @@ import cn.escheduler.api.interceptor.LoginHandlerInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.*; import org.springframework.web.servlet.LocaleResolver; import org.springframework.web.servlet.i18n.CookieLocaleResolver; import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; import java.util.Locale; /** * application configuration Loading @@ -31,16 +37,48 @@ public class AppConfiguration implements WebMvcConfigurer { public static final String LOGIN_PATH_PATTERN = "/login"; public static final String PATH_PATTERN = "/**"; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(loginInterceptor()).addPathPatterns(LOGIN_INTERCEPTOR_PATH_PATTERN).excludePathPatterns(LOGIN_PATH_PATTERN); } @Bean public LoginHandlerInterceptor loginInterceptor() { return new LoginHandlerInterceptor(); } //Cookie @Bean public LocaleResolver localeResolver() { CookieLocaleResolver localeResolver = new CookieLocaleResolver(); localeResolver.setCookieName("localeCookie"); //设置默认区域 localeResolver.setDefaultLocale(Locale.ENGLISH); localeResolver.setCookieMaxAge(3600);//设置cookie有效期. return localeResolver; } @Bean public LocaleChangeInterceptor localeChangeInterceptor() { LocaleChangeInterceptor lci = new LocaleChangeInterceptor(); // 参数名 lci.setParamName("lang"); return lci; } @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(loginInterceptor()).addPathPatterns(LOGIN_INTERCEPTOR_PATH_PATTERN).excludePathPatterns(LOGIN_PATH_PATTERN,"/swagger-resources/**", "/webjars/**", "/v2/**", "/doc.html", "*.html"); //i18n registry.addInterceptor(localeChangeInterceptor()); } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/"); } @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping(PATH_PATTERN).allowedOrigins("*").allowedMethods("*"); Loading @@ -56,4 +94,8 @@ public class AppConfiguration implements WebMvcConfigurer { public void configureContentNegotiation(final ContentNegotiationConfigurer configurer) { configurer.favorPathExtension(false); } } escheduler-api/src/main/java/cn/escheduler/api/configuration/SwaggerConfig.java 0 → 100644 +55 −0 Original line number Diff line number Diff line /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package cn.escheduler.api.configuration; import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; 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; /** * * swager2 config class <br/> * */ @Configuration @EnableSwagger2 @EnableSwaggerBootstrapUI public class SwaggerConfig implements WebMvcConfigurer { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select() .apis(RequestHandlerSelectors.basePackage("cn.escheduler.api.controller")).paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder().title("Easy Scheduler Api Docs").description("Easy Scheduler Api Docs") .version("1.0.0").build(); } } escheduler-api/src/main/java/cn/escheduler/api/configuration/SwaggerI18nPlugin.java 0 → 100644 +52 −0 Original line number Diff line number Diff line package cn.escheduler.api.configuration; import java.util.List; import java.util.Locale; import com.fasterxml.classmate.TypeResolver; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.MessageSource; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; import io.swagger.annotations.ApiOperation; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spi.service.OperationBuilderPlugin; import springfox.documentation.spi.service.contexts.OperationContext; @Component @Order(Ordered.HIGHEST_PRECEDENCE - 10) public class SwaggerI18nPlugin implements OperationBuilderPlugin { private static final Logger logger = LoggerFactory.getLogger(SwaggerI18nPlugin.class); @Autowired private MessageSource messageSource; @Override public void apply(OperationContext context) { Locale locale = LocaleContextHolder.getLocale(); List<ApiOperation> list = context.findAllAnnotations(ApiOperation.class); if (list.size() > 0) { for(ApiOperation api : list){ context.operationBuilder().summary(messageSource.getMessage(api.value(), null, locale)); context.operationBuilder().notes(messageSource.getMessage(api.notes(), null, locale)); } } } @Override public boolean supports(DocumentationType delimiter) { return true; } } Loading
escheduler-api/pom.xml +28 −8 Original line number Diff line number Diff line <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"> <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> <parent> <groupId>cn.analysys</groupId> Loading @@ -10,13 +11,10 @@ <dependencies> <dependency> <groupId>cn.analysys</groupId> <artifactId>escheduler-dao</artifactId> </dependency> <dependency> <groupId>cn.analysys</groupId> <artifactId>escheduler-common</artifactId> <artifactId>escheduler-server</artifactId> <exclusions> <exclusion> <groupId>io.netty</groupId> Loading @@ -38,7 +36,6 @@ </dependency> <!--springboot--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> Loading Loading @@ -142,6 +139,24 @@ <artifactId>quartz-jobs</artifactId> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>swagger-bootstrap-ui</artifactId> <version>1.9.3</version> </dependency> <dependency> <groupId>cn.analysys</groupId> <artifactId>escheduler-rpc</artifactId> Loading @@ -153,6 +168,11 @@ <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>io.swagger</groupId> <artifactId>swagger-jaxrs</artifactId> <version>1.5.12</version> </dependency> </dependencies> <build> Loading
escheduler-api/src/main/java/cn/escheduler/api/ApiApplicationServer.java +7 −1 Original line number Diff line number Diff line Loading @@ -19,13 +19,19 @@ package cn.escheduler.api; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; import org.springframework.context.annotation.ComponentScan; import springfox.documentation.swagger2.annotations.EnableSwagger2; @SpringBootApplication @ServletComponentScan @ComponentScan("cn.escheduler") public class ApiApplicationServer { @EnableSwagger2 public class ApiApplicationServer extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(ApiApplicationServer.class, args); } }
escheduler-api/src/main/java/cn/escheduler/api/configuration/AppConfiguration.java +46 −4 Original line number Diff line number Diff line Loading @@ -20,6 +20,12 @@ import cn.escheduler.api.interceptor.LoginHandlerInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.*; import org.springframework.web.servlet.LocaleResolver; import org.springframework.web.servlet.i18n.CookieLocaleResolver; import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; import java.util.Locale; /** * application configuration Loading @@ -31,16 +37,48 @@ public class AppConfiguration implements WebMvcConfigurer { public static final String LOGIN_PATH_PATTERN = "/login"; public static final String PATH_PATTERN = "/**"; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(loginInterceptor()).addPathPatterns(LOGIN_INTERCEPTOR_PATH_PATTERN).excludePathPatterns(LOGIN_PATH_PATTERN); } @Bean public LoginHandlerInterceptor loginInterceptor() { return new LoginHandlerInterceptor(); } //Cookie @Bean public LocaleResolver localeResolver() { CookieLocaleResolver localeResolver = new CookieLocaleResolver(); localeResolver.setCookieName("localeCookie"); //设置默认区域 localeResolver.setDefaultLocale(Locale.ENGLISH); localeResolver.setCookieMaxAge(3600);//设置cookie有效期. return localeResolver; } @Bean public LocaleChangeInterceptor localeChangeInterceptor() { LocaleChangeInterceptor lci = new LocaleChangeInterceptor(); // 参数名 lci.setParamName("lang"); return lci; } @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(loginInterceptor()).addPathPatterns(LOGIN_INTERCEPTOR_PATH_PATTERN).excludePathPatterns(LOGIN_PATH_PATTERN,"/swagger-resources/**", "/webjars/**", "/v2/**", "/doc.html", "*.html"); //i18n registry.addInterceptor(localeChangeInterceptor()); } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/"); } @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping(PATH_PATTERN).allowedOrigins("*").allowedMethods("*"); Loading @@ -56,4 +94,8 @@ public class AppConfiguration implements WebMvcConfigurer { public void configureContentNegotiation(final ContentNegotiationConfigurer configurer) { configurer.favorPathExtension(false); } }
escheduler-api/src/main/java/cn/escheduler/api/configuration/SwaggerConfig.java 0 → 100644 +55 −0 Original line number Diff line number Diff line /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package cn.escheduler.api.configuration; import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; 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; /** * * swager2 config class <br/> * */ @Configuration @EnableSwagger2 @EnableSwaggerBootstrapUI public class SwaggerConfig implements WebMvcConfigurer { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select() .apis(RequestHandlerSelectors.basePackage("cn.escheduler.api.controller")).paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder().title("Easy Scheduler Api Docs").description("Easy Scheduler Api Docs") .version("1.0.0").build(); } }
escheduler-api/src/main/java/cn/escheduler/api/configuration/SwaggerI18nPlugin.java 0 → 100644 +52 −0 Original line number Diff line number Diff line package cn.escheduler.api.configuration; import java.util.List; import java.util.Locale; import com.fasterxml.classmate.TypeResolver; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.MessageSource; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; import io.swagger.annotations.ApiOperation; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spi.service.OperationBuilderPlugin; import springfox.documentation.spi.service.contexts.OperationContext; @Component @Order(Ordered.HIGHEST_PRECEDENCE - 10) public class SwaggerI18nPlugin implements OperationBuilderPlugin { private static final Logger logger = LoggerFactory.getLogger(SwaggerI18nPlugin.class); @Autowired private MessageSource messageSource; @Override public void apply(OperationContext context) { Locale locale = LocaleContextHolder.getLocale(); List<ApiOperation> list = context.findAllAnnotations(ApiOperation.class); if (list.size() > 0) { for(ApiOperation api : list){ context.operationBuilder().summary(messageSource.getMessage(api.value(), null, locale)); context.operationBuilder().notes(messageSource.getMessage(api.notes(), null, locale)); } } } @Override public boolean supports(DocumentationType delimiter) { return true; } }