gpt4 book ai didi

java - 处理 HttpServletResponse 时运行 JerseyTest 的问题

转载 作者:太空狗 更新时间:2023-10-29 22:43:32 25 4
gpt4 key购买 nike

这是一个示例资源类:

@Path("/resource") 
public class SomeResource {
@GET
@Produces({MediaType.APPLICATION_XML})
public String someMethod(@QueryParam("param1") String param1, ..., @Context HttpServletRequest request) {
String remoteUser = request.getRemoteAddr();
// Business logic here.
return response;
}
}

以及资源的 JerseyTest:

public class TestSomeResource extends JerseyTest    { 
@Override
protected Application configure() {
enable(TestProperties.LOG_TRAFFIC);
return new ResourceConfig(SomeResource.class);
}

@Test
public void testXMLResponse() {
String response = target("resource")
.queryParam("param1", param1)
// More parameters here.
.request()
.accept(MediaType.APPLICATION_XML)
.get(String.class);
// Some assertions on response.
}
}

除了使用 @Context HttpServletRequest 作为输入参数的资源之外,我能够为所有其他资源运行 Jersey 测试。它给出一个 InternalServerErrorException: HTTP 500 Internal Server Error。

以下是堆栈跟踪:

javax.ws.rs.InternalServerErrorException: HTTP 500 Internal Server Error 
at org.glassfish.jersey.client.JerseyInvocation.convertToException(JerseyInvocation.java:904)
at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:749)
at org.glassfish.jersey.client.JerseyInvocation.access$500(JerseyInvocation.java:88)
at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:650)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:421)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:646)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:375)
at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:275)
at com.mysample.TestSomeResource.testXMLResponse(TestSomeResource.java:15)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

最佳答案

您的异常与 HttpServletRequest 的事实有关是null .

Jersey 文档说:

3.6. Use of @Context

Previous sections have introduced the use of @Context. Chapter 5 of the JAX-RS specification presents all the standard JAX-RS Java types that may be used with @Context.

When deploying a JAX-RS application using servlet then ServletConfig, ServletContext, HttpServletRequest and HttpServletResponse are available using @Context.

我猜你用的是 jersey-test-framework-provider-grizzly2不支持它。

如果您想访问 HttpServletResponse删除该依赖项并添加:

<dependency>
<groupId>org.glassfish.jersey.test-framework</groupId>
<artifactId>jersey-test-framework-core</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-grizzly2-servlet</artifactId>
<version>2.1</version>
</dependency>

现在您实际上想要告诉 JerseyTest 启动正确的测试服务器,为此您必须覆盖一个方法 protected TestContainerFactory getTestContainerFactory() . 请务必更换<your-java-package>使用您包裹的实际名称

@Override
protected TestContainerFactory getTestContainerFactory() throws TestContainerException {
return new TestContainerFactory() {
@Override
public TestContainer create(final URI baseUri, final ApplicationHandler application) throws IllegalArgumentException {
return new TestContainer() {
private HttpServer server;

@Override
public ClientConfig getClientConfig() {
return null;
}

@Override
public URI getBaseUri() {
return baseUri;
}

@Override
public void start() {
try {
this.server = GrizzlyWebContainerFactory.create(
baseUri, Collections.singletonMap("jersey.config.server.provider.packages", "<your-java-package>")
);
} catch (ProcessingException e) {
throw new TestContainerException(e);
} catch (IOException e) {
throw new TestContainerException(e);
}
}

@Override
public void stop() {
this.server.stop();
}
};

}
};
}

您还可以查看 org.glassfish.jersey.test.grizzly.GrizzlyTestContainerFactory以便更好地实现工厂。

关于java - 处理 HttpServletResponse 时运行 JerseyTest 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17973277/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com