quarkus项目配置模板

2020-04-16  本文已影响0人  rainmanhhh

pom.xml dependencies:

  <dependencies>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-resteasy</artifactId>
    </dependency>
    <dependency>
      <groupId>org.jetbrains.kotlin</groupId>
      <artifactId>kotlin-stdlib-jdk8</artifactId>
    </dependency>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-junit5</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>io.rest-assured</groupId>
      <artifactId>rest-assured</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-reactive-mysql-client</artifactId>
    </dependency>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-vertx</artifactId>
    </dependency>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-smallrye-openapi</artifactId>
    </dependency>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-vertx-web</artifactId>
    </dependency>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-config-yaml</artifactId>
    </dependency>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-kotlin</artifactId>
    </dependency>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-resteasy-jsonb</artifactId>
    </dependency>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-smallrye-metrics</artifactId>
    </dependency>
    <dependency>
      <groupId>io.vertx</groupId>
      <artifactId>vertx-lang-kotlin</artifactId>
    </dependency>
    <dependency>
      <groupId>io.vertx</groupId>
      <artifactId>vertx-lang-kotlin-coroutines</artifactId>
    </dependency>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-smallrye-health</artifactId>
    </dependency>
    <!--thirdparty extensions-->
    <dependency>
      <groupId>com.github.fmcejudo</groupId>
      <artifactId>quarkus-eureka</artifactId>
      <version>0.0.7</version>
    </dependency>
  </dependencies>

application.yml:

quarkus:
  # add kotlinx-coroutines-core to jandex index(for Deferred)
  index-dependency:
    kotlinx-coroutines:
      group-id: org.jetbrains.kotlinx
      artifact-id: kotlinx-coroutines-core
  eureka:
    region: default
    health-check-url: ${quarkus.smallrye-health.root-path:/health}
    status-page-url: ${quarkus.swagger-ui.path:/swagger-ui}
    service-url:
      defaultZone: http://127.0.0.1:8761/eureka/
  swagger-ui:
    always-include: true
  log:
    category:
      "io.quarkus.eureka.operation.heartbeat.HeartBeatOperation":
        level: WARNING
"%dev":
  quarkus:
    vertx:
      event-loops-pool-size: 1
      worker-pool-size: 1
      warning-exception-time: 1h
      max-event-loop-execute-time: 1h
      max-worker-execute-time: 1h

JdkDateTimeProvider.kt: (parse LocalDate/LocalDateTime web params such as @QueryParam)

@Provider
class JdkDateTimeProvider : ParamConverterProvider {
  object LocalDateConverter : ParamConverter<LocalDate> {
    override fun toString(t: LocalDate): String {
      return t.toString()
    }

    override fun fromString(str: String): LocalDate {
      return LocalDate.parse(str)
    }
  }

  object LocalDateTimeConverter : ParamConverter<LocalDateTime> {
    override fun toString(t: LocalDateTime): String {
      return t.toString()
    }

    override fun fromString(str: String): LocalDateTime {
      return LocalDateTime.parse(str)
    }
  }

  @Suppress("UNCHECKED_CAST")
  override fun <T> getConverter(clazz: Class<T>, type: Type?, antns: Array<Annotation?>?): ParamConverter<T>? =
    when (clazz) {
      LocalDate::class.java -> LocalDateConverter
      LocalDateTime::class.java -> LocalDateTimeConverter
      else -> null
    } as ParamConverter<T>?
}

VertxScope.kt:

abstract class VertxScope : CoroutineScope {
  @Inject
  lateinit var vertx: Vertx

  private val cs = ThreadLocal.withInitial {
    vertx.dispatcher()
  }

  override val coroutineContext: CoroutineContext get() = cs.get()
}

VertxEx.kt

@Suppress("EXPERIMENTAL_API_USAGE")
fun <T> Deferred<T>.toJdkFuture(): CompletableFuture<T> {
  val f = CompletableFuture<T>()
  invokeOnCompletion {
    val e = getCompletionExceptionOrNull()
    if (e==null) f.complete(getCompleted())
    else f.completeExceptionally(e)
  }
  return f
}

NotFoundExceptionMapper.kt:

@Provider
class NotFoundExceptionMapper : ExceptionMapper<NotFoundException> {
  val contentType = MediaType.TEXT_PLAIN_TYPE.withCharset(StandardCharsets.UTF_8.name()).toString()

  override fun toResponse(exception: NotFoundException): Response {
    val cause = exception.cause
    val res = if (cause==null) Response.status(Response.Status.NOT_FOUND)
    else Response.status(Response.Status.BAD_REQUEST).entity(
      cause.javaClass.name + ":" + cause.message
    )
    return res.header(HttpHeaders.CONTENT_TYPE, contentType).build()
  }
}
上一篇下一篇

猜你喜欢

热点阅读