小编Sey*_*bol的帖子

Kotlin 中的 ::class 和 ::class.java 有什么区别?

在 Java 中,我们编写.class(例如:)String.class来获取有关给定类的信息。在 Kotlin 中,您可以编写::class::class.java. 它们之间有什么区别?

kotlin

24
推荐指数
3
解决办法
4066
查看次数

如何使用 springdoc-openapi-webflux-ui 显示应用程序 API 文档?

我阅读了这个https://springdoc.github.io/springdoc-openapi-demos/文档以使用 springdoc-openapi-webflux-ui。正如文档所说,我刚刚springdoc-openapi-webflux-ui在我的应用程序中添加了库:implementation('org.springdoc:springdoc-openapi-webflux-ui:1.2.26')

此外,我在 application.yml 中自定义了 API 的路径:

springdoc:
  swagger-ui:
    path: /swagger-ui.html
Run Code Online (Sandbox Code Playgroud)

当我启动应用程序并转到http://localhost:8080/swagger-ui.html 时,它会将我重定向到http://localhost:8080/webjars/swagger-ui/index.html?configUrl=/v3/api -docs/swagger-config。在该页面中,我收到一个错误:

Whitelabel Error Page
This application has no configured error view, so you are seeing this as a fallback.
Mon Jan 20 05:16:10 UTC 2020
[7192d9dc] There was an unexpected error (type=Not Found, status=404).
No matching handler
Run Code Online (Sandbox Code Playgroud)

问题是:我应该向我的应用程序添加其他配置以显示 API 文档吗?

PS:我使用 spring.boot 2.2.2:RELEASE

java spring spring-boot springdoc

12
推荐指数
1
解决办法
6671
查看次数

如何将自定义数据类设置为 StatefulRedisConnection 值类型?

Lettuce Core API 有 RedisClient 类,其中包含以下方法:

public StatefulRedisConnection<String, String> connect() {
      return this.connect(this.newStringStringCodec());
}

public <K, V> StatefulRedisConnection<K, V> connect(RedisCodec<K, V> codec) {
      this.checkForRedisURI();
      return (StatefulRedisConnection)this.getConnection(this.connectStandaloneAsync(codec, this.redisURI, this.timeout));
}
Run Code Online (Sandbox Code Playgroud)

我想调用 method connect(RedisCodec<K, V> codec),但我不知道如何配置我应该作为参数传递给该方法的编解码器对象。

我当前的代码:

val redisClient = RedisClient.create("redis://password@localhost:6379/0");
val connection = redisClient.connect();
// in this case type of connection is StatefulRedisConnection<String, String>
val redisCommands = connection.sync()
Run Code Online (Sandbox Code Playgroud)

我也有我的自定义数据类。

data class MyCustomDataClassName {
  val id: UUID,
  val something: String,
  val foo: String,
  val bar: String
}
Run Code Online (Sandbox Code Playgroud)

我想写这段代码:

val redisClient = …
Run Code Online (Sandbox Code Playgroud)

java redis lettuce kotlin

7
推荐指数
1
解决办法
413
查看次数

如何摆脱使用 Spring Boot 时 Detekt 给出的 SpreadOperator 性能警告?

最近,我在我的应用程序中添加了Detekt分析器。

在我运行 detekt (./gradlew detekt) 后,我在我的主应用程序类中收到了 SpreadOperator 警告。

代码警告: runApplication<MessCallsApplication>(*args)

您可以在此处阅读有关 SpreadOperator 警告的信息: [SpreadOperator Warning][2]

我的主要课程:

@SpringBootApplication(exclude = [RedisAutoConfiguration::class])
@EnableCircuitBreaker
@EnableScheduling
class MyApplication {

    companion object : KLogging()
}

fun main(args: Array<String>) {
    Thread.setDefaultUncaughtExceptionHandler { _, exception ->
        MessCallsApplication.logger.error(exception) { "Uncaught exception" }
    }

    runApplication<MessCallsApplication>(*args)
}
Run Code Online (Sandbox Code Playgroud)

问题是,摆脱 SpreadOperator 警告的最佳做法是什么?或者是不可能的?

java spring kotlin detekt

5
推荐指数
2
解决办法
1754
查看次数

如何覆盖 testcontainers 中的 jna.tmpdir 和 java.io.tmpdir 属性?

TestContainers 有 Native.java 类,它从“jna.tmpdir”键获取 JNA 属性。如何覆盖该键的值?喜欢:

\n\n
jna.tmpdir=/data/builds/compose-tmp \njava.io.tmpdir=/data/builds/compose-tmp\n
Run Code Online (Sandbox Code Playgroud)\n\n

获取 jna 属性的部分代码:\xc2\xa0

\n\n
\npackage com.sun.jna;\n\npublic final class Native implements Version {\n\n...\n\n   static File getTempDir() throws IOException {\n       File jnatmp;\n       String prop = System.getProperty("jna.tmpdir");\n       if (prop != null) {\n           jnatmp = new File(prop);\n           jnatmp.mkdirs();\n       }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

testcontainers

5
推荐指数
0
解决办法
1260
查看次数

如何通过 io.mockk 测试使用不同参数多次调用的方法?

PS:代码将在 Koltin

例如,我的服务类可以执行某些操作并注入一些其他服务。

class MyService(
   private val someOtherService: OtherService
) {
   fun doSomething() {
       someOtherService.someMethod("foo")
       someOtherService.someMethod("bar")
       someOtherService.someMethod("baz")
   }
}
Run Code Online (Sandbox Code Playgroud)

这是我对模拟 OtherService 的 MyService 类的测试:

internal class MyServiceTest {
    @MockkBean(relaxed = true)
    private lateinit var someOtherService: OtherService

    @Test
    fun `my test description`() {
        every { someOtherService.someMethod(any()) } just Runs

        verify(exactly = 1) {
            someOtherService.someMethod(
                    match {
                        it shouldBe "bar"
                        true
                    }
            )
        }
    }
Run Code Online (Sandbox Code Playgroud)

结果,"bar"参数将是预期的,但将是"foo"参数,并且测试将失败。

原因:someOtherService.someMethod("foo")之前会调用someOtherService.someMethod("bar")

但是,我想验证每个方法是否只调用了一次。我怎么能做到这一点?

testing unit-testing kotlin mockk

3
推荐指数
1
解决办法
4827
查看次数