Skip to content

Commit 074b30b

Browse files
authored
Merge pull request #47 from carrypann/develop
feature: 开发web公共组件 #46
2 parents eec01ad + 2848d91 commit 074b30b

24 files changed

Lines changed: 472 additions & 7 deletions

File tree

devops-boot-project/devops-boot-core/devops-api/src/main/kotlin/com/tencent/devops/api/http/MediaTypes.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ object MediaTypes {
99
const val TEXT_HTML = "text/html"
1010
const val APPLICATION_YAML = "application/x-yaml"
1111
const val APPLICATION_TGZ = "application/x-tar"
12-
const val APPLICATION_ICO= "image/x-icon"
12+
const val APPLICATION_ICO = "image/x-icon"
1313
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
description = "DevOps Boot Service"
2+
3+
dependencies {
4+
api(project(":devops-boot-project:devops-boot-core:devops-web"))
5+
api("org.springframework.boot:spring-boot-starter-actuator")
6+
api("io.github.openfeign:feign-okhttp")
7+
api("org.springframework.cloud:spring-cloud-starter-openfeign")
8+
api("org.springframework.cloud:spring-cloud-starter-consul-discovery")
9+
api("org.springframework.cloud:spring-cloud-starter-consul-config")
10+
api("org.springframework.cloud:spring-cloud-starter-netflix-hystrix")
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.tencent.devops.service.feign
2+
3+
import org.springframework.core.annotation.AnnotatedElementUtils
4+
import org.springframework.stereotype.Controller
5+
import org.springframework.web.bind.annotation.RestController
6+
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping
7+
8+
/**
9+
* 重写RequestMappingHandlerMapping的isHandler方法,避免声明Feign Client Api接口的RequestMapping注解
10+
* 与Feign Client Api实现类的Controller注解重复,造成HandlerMapping以及swagger重复扫描的问题
11+
*/
12+
class FeignFilterRequestMappingHandlerMapping : RequestMappingHandlerMapping() {
13+
14+
override fun isHandler(beanType: Class<*>): Boolean {
15+
return AnnotatedElementUtils.hasAnnotation(beanType, Controller::class.java) ||
16+
AnnotatedElementUtils.hasAnnotation(beanType, RestController::class.java)
17+
}
18+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
description = "DevOps Boot Utils"
2+
3+
dependencies {
4+
api("com.fasterxml.jackson.module:jackson-module-kotlin")
5+
api("com.fasterxml.jackson.datatype:jackson-datatype-jdk8")
6+
api("com.fasterxml.jackson.datatype:jackson-datatype-jsr310")
7+
api("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml")
8+
api("com.fasterxml.jackson.dataformat:jackson-dataformat-xml")
9+
api("com.fasterxml.jackson.module:jackson-module-parameter-names")
10+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.tencent.devops.utils.jackson
2+
3+
import com.fasterxml.jackson.databind.DeserializationFeature
4+
import com.fasterxml.jackson.databind.ObjectMapper
5+
import com.fasterxml.jackson.databind.SerializationFeature
6+
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module
7+
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
8+
import com.fasterxml.jackson.module.kotlin.KotlinModule
9+
import com.fasterxml.jackson.module.kotlin.jacksonTypeRef
10+
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule
11+
import java.io.InputStream
12+
13+
/**
14+
* Json工具类
15+
*/
16+
object JsonUtils {
17+
val objectMapper = ObjectMapper().apply {
18+
registerModule(KotlinModule())
19+
registerModule(JavaTimeModule())
20+
registerModule(ParameterNamesModule())
21+
registerModule(Jdk8Module())
22+
23+
enable(SerializationFeature.INDENT_OUTPUT)
24+
disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
25+
disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
26+
}
27+
}
28+
29+
/**
30+
* 将对象序列化为json字符串
31+
*/
32+
fun Any.toJsonString() = JsonUtils.objectMapper.writeValueAsString(this).orEmpty()
33+
34+
/**
35+
* 将json字符串反序列化为对象
36+
*/
37+
inline fun <reified T> String.readJsonString(): T = JsonUtils.objectMapper.readValue(this, jacksonTypeRef<T>())
38+
39+
/**
40+
* 将json字符串流反序列化为对象
41+
*/
42+
inline fun <reified T> InputStream.readJsonString(): T = JsonUtils.objectMapper.readValue(this, jacksonTypeRef<T>())
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.tencent.devops.utils.jackson
2+
3+
import com.fasterxml.jackson.databind.DeserializationFeature
4+
import com.fasterxml.jackson.databind.ObjectMapper
5+
import com.fasterxml.jackson.databind.SerializationFeature
6+
import com.fasterxml.jackson.dataformat.xml.XmlMapper
7+
import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator
8+
import com.fasterxml.jackson.module.kotlin.jacksonTypeRef
9+
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
10+
import java.io.InputStream
11+
12+
/**
13+
* xml工具类
14+
*/
15+
object XmlUtils {
16+
val objectMapper: ObjectMapper = XmlMapper().apply {
17+
registerKotlinModule()
18+
enable(ToXmlGenerator.Feature.WRITE_XML_DECLARATION)
19+
enable(SerializationFeature.INDENT_OUTPUT)
20+
disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
21+
}
22+
}
23+
24+
/**
25+
* 将对象序列化为xml字符串
26+
*/
27+
fun Any.toXmlString() = XmlUtils.objectMapper.writeValueAsString(this).orEmpty()
28+
29+
/**
30+
* 将xml字符串反序列化为对象
31+
*/
32+
inline fun <reified T> String.readXmlString(): T = XmlUtils.objectMapper.readValue(this, jacksonTypeRef<T>())
33+
34+
/**
35+
* 将xml字符串流反序列化为对象
36+
*/
37+
inline fun <reified T> InputStream.readXmlString(): T = XmlUtils.objectMapper.readValue(this, jacksonTypeRef<T>())
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.tencent.devops.utils.jackson
2+
3+
import com.fasterxml.jackson.databind.DeserializationFeature
4+
import com.fasterxml.jackson.databind.ObjectMapper
5+
import com.fasterxml.jackson.databind.SerializationFeature
6+
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator
7+
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper
8+
import com.fasterxml.jackson.module.kotlin.jacksonTypeRef
9+
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
10+
import java.io.InputStream
11+
12+
/**
13+
* yaml工具类
14+
*/
15+
object YamlUtils {
16+
val objectMapper: ObjectMapper = YAMLMapper().apply {
17+
registerKotlinModule()
18+
enable(SerializationFeature.INDENT_OUTPUT)
19+
// 缺省文件以三个横杠开头 禁用该属性
20+
disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER)
21+
disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
22+
}
23+
}
24+
25+
/**
26+
* 将对象序列化为yaml字符串
27+
*/
28+
fun Any.toYamlString() = YamlUtils.objectMapper.writeValueAsString(this).orEmpty()
29+
30+
/**
31+
* 将yaml字符串反序列化为对象
32+
*/
33+
inline fun <reified T> String.readYamlString(): T = YamlUtils.objectMapper.readValue(this, jacksonTypeRef<T>())
34+
35+
/**
36+
* 将yaml字符串流反序列化为对象
37+
*/
38+
inline fun <reified T> InputStream.readYamlString(): T = YamlUtils.objectMapper.readValue(this, jacksonTypeRef<T>())
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
description = "DevOps Boot Web"
2+
3+
dependencies {
4+
api(project(":devops-boot-project:devops-boot-core:devops-boot"))
5+
api(project(":devops-boot-project:devops-boot-core:devops-logging"))
6+
api(project(":devops-boot-project:devops-boot-core:devops-utils"))
7+
api("org.springframework.boot:spring-boot-starter-web") {
8+
exclude(module = "spring-boot-starter-tomcat")
9+
}
10+
api("org.springframework.boot:spring-boot-starter-undertow")
11+
api("org.springframework.boot:spring-boot-starter-actuator")
12+
api("io.springfox:springfox-boot-starter")
13+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.tencent.devops.web.util
2+
3+
import org.springframework.beans.BeansException
4+
import org.springframework.context.ApplicationContext
5+
import org.springframework.context.ApplicationContextAware
6+
7+
/**
8+
* Spring上下文工具类
9+
*/
10+
class SpringContextUtils : ApplicationContextAware {
11+
12+
/**
13+
* 实现ApplicationContextAware接口的回调方法,设置上下文环境
14+
*/
15+
@Throws(BeansException::class)
16+
override fun setApplicationContext(applicationContext: ApplicationContext) {
17+
Companion.applicationContext = applicationContext
18+
}
19+
20+
companion object {
21+
22+
@Suppress("LateinitUsage")
23+
private lateinit var applicationContext: ApplicationContext
24+
25+
/**
26+
* 获取对象
27+
* @param <T> Bean
28+
* @return 实例
29+
* @throws BeansException 异常
30+
*/
31+
@Throws(BeansException::class)
32+
inline fun <reified T> getBean(): T {
33+
return getBean(T::class.java)
34+
}
35+
36+
/**
37+
* 获取对象 这里重写了bean方法,起主要作用
38+
* @param clazz 类名
39+
* @param <T> Bean
40+
* @return 实例
41+
* @throws BeansException 异常
42+
*/
43+
@Throws(BeansException::class)
44+
fun <T> getBean(clazz: Class<T>): T {
45+
return applicationContext.getBean(clazz)
46+
}
47+
48+
/**
49+
* 取指定类的指定名称的类的实例对象
50+
* @param clazz 类名
51+
* @param beanName 实例对象名称
52+
* @param <T> Bean
53+
* @return 实例
54+
* @throws BeansException 异常
55+
*/
56+
@Throws(BeansException::class)
57+
fun <T> getBean(clazz: Class<T>, beanName: String): T {
58+
return applicationContext.getBean(beanName, clazz)
59+
}
60+
61+
/**
62+
* 获取对象列表
63+
* @param clazz 注解类名
64+
* @param <T: Annotation> 注解
65+
* @return 实例列表
66+
* @throws BeansException 异常
67+
*/
68+
@Throws(BeansException::class)
69+
fun <T : Annotation> getBeansWithAnnotation(clazz: Class<T>): List<Any> {
70+
return applicationContext.getBeansWithAnnotation(clazz).values.toList()
71+
}
72+
73+
/**
74+
* 发送事件
75+
* @param event – the event to publish
76+
*/
77+
fun publishEvent(event: Any) {
78+
applicationContext.publishEvent(event)
79+
}
80+
}
81+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
description = "Starter for DevOps Boot Service"
2+
3+
dependencies {
4+
api(project(":devops-boot-project:devops-boot-core:devops-service"))
5+
api(project(":devops-boot-project:devops-boot-starters:devops-boot-starter-web"))
6+
}

0 commit comments

Comments
 (0)