首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Spring Boot中的处理程序方法,但在测试中未找到Jersey

Spring Boot中的处理程序方法,但在测试中未找到Jersey
EN

Stack Overflow用户
提问于 2018-06-18 23:29:48
回答 1查看 749关注 0票数 2

当应用程序启动并从测试访问时,Spring Boot / Jersey找不到处理程序方法。如果我单独启动应用程序并使用浏览器访问http://localhost:8080/demo,一切都很好。

日志显示:“未找到/demo的处理程序方法”。相关的日志记录输出:

代码语言:javascript
复制
2018-06-18 17:04:31.071 DEBUG 7628 --- [nio-8080-exec-1] o.s.web.reactive.DispatcherHandler       : Processing GET request for [http://localhost:8080/demo]
2018-06-18 17:04:31.083 DEBUG 7628 --- [nio-8080-exec-1] s.w.r.r.m.a.RequestMappingHandlerMapping : Looking up handler method for path /demo
2018-06-18 17:04:31.085 DEBUG 7628 --- [nio-8080-exec-1] s.w.r.r.m.a.RequestMappingHandlerMapping : Did not find handler method for [/demo]
2018-06-18 17:04:31.087 DEBUG 7628 --- [nio-8080-exec-1] o.s.w.r.handler.SimpleUrlHandlerMapping  : Matching pattern for request [[path='/demo']] is /**

应用程序由以下类组成(用Kotlin编写):

资源

代码语言:javascript
复制
import org.springframework.stereotype.Component
import javax.ws.rs.GET
import javax.ws.rs.Path
import javax.ws.rs.core.Response

@Component
@Path("/")
class Resource {

    @GET
    @Path("demo")
    fun test() = Response.ok("Hi!").encoding("UTF-8").build()
}

JerseyConfig

代码语言:javascript
复制
import org.glassfish.jersey.server.ResourceConfig
import org.springframework.stereotype.Component

@Component
class JerseyConfig : ResourceConfig() {

    init {
        register(Resource::class.java)
    }
}

应用程序:

代码语言:javascript
复制
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication

@SpringBootApplication
class App

fun main(args: Array<String>) {
    runApplication<App>(*args)
}

失败的测试:

代码语言:javascript
复制
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.junit.jupiter.SpringExtension
import org.springframework.test.web.reactive.server.WebTestClient

@ExtendWith(SpringExtension::class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
class ResourceTest {

    @Autowired
    lateinit var client: WebTestClient

    @Test
    fun getTest() {
        client.get().uri("demo").exchange().expectStatus().isOk
    }
}

如果我使用Jersey客户端进行测试,我会得到相同的错误:

代码语言:javascript
复制
@Test
fun testWithJersey() {
    val client = ClientBuilder.newClient()
    val response = client.target("http://localhost:8080/demo").request().get()
    assertThat(response.status).isEqualTo(200)
}

build.gradle:

代码语言:javascript
复制
buildscript {
    ext {
        kotlinVersion = '1.2.50'
        springBootVersion = '2.0.3.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
        classpath("org.jetbrains.kotlin:kotlin-allopen:${kotlinVersion}")
    }
}

apply plugin: 'kotlin'
apply plugin: 'kotlin-spring'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

compileKotlin {
    kotlinOptions {
        freeCompilerArgs = ["-Xjsr305=strict"]
        jvmTarget = "1.8"
    }
}
compileTestKotlin {
    kotlinOptions {
        freeCompilerArgs = ["-Xjsr305=strict"]
        jvmTarget = "1.8"
    }
}

repositories {
    mavenCentral()
}

dependencies {
    compile("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    compile("org.jetbrains.kotlin:kotlin-reflect")
    compile('org.springframework.boot:spring-boot-starter-jersey')
    testCompile("org.springframework.boot:spring-boot-starter-test") {
        exclude group: "junit", module: "junit"
    }
    testCompile('org.springframework.boot:spring-boot-starter-webflux')
    testCompile("org.junit.jupiter:junit-jupiter-api")
    testRuntime("org.junit.jupiter:junit-jupiter-engine")
}

测试代码本身似乎没有问题,因为当我用Thread.sleep(...)替换测试方法的主体,然后从浏览器访问服务器时,我得到了相同的错误(由于“没有找到/demo的处理程序方法”)。

为什么在测试中找不到处理程序方法?我有什么需要改变的?

EN

回答 1

Stack Overflow用户

发布于 2018-06-19 03:16:45

你确定WebTestClient是一个通用的客户端(发出实际的网络请求),而不是像MockMvc那样只用于Spring MVC吗?该错误听起来像是在寻找Spring MVC处理程序方法。如果它是一个通用客户端,那么错误消息不会显示任何有关处理程序方法的内容,而可能会显示有关URL的内容。

我假设您需要向一个真实的客户端发出一个实际的网络请求。例如,如果你使用Jersey客户端,你可以这样做

代码语言:javascript
复制
@LocalServerPort
private int port;

private Client client = ClientBuilder.newClient();

@Test
public void testCustomerLocationOnPost() {
    URI resourceUri = UriBuilder.fromUri("http://localhost")
            .port(port).path("demo").build();

    Respons response = client.target(resourcrUri).request().get();

    assertThat(response.getStatus()).isEqualTo(200);

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50912879

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档