[英]Jersey RESTful Service 500 Error (request is null)
我是REST服務的新手,我想使用Jersey測試框架進行測試。 配置似乎可以(不是null),但是調試器顯示請求為null。 更改target()方法中的路徑沒有任何效果。 找到了很多答案,但沒有幫助。 那么,為什么請求可以為空?
TestReportResource.java
public class TestReportResource extends JerseyTest {
private ETS tmEts;
private ETS suEts;
private ReportService mockReportService;
private ReportResource reportResource;
private ReportResource reportResourceNew;
private MapperFacade mapper;
private ReportServiceJerseyMockTest mockReportServiceJersey;
@Before
public void init() {
this.tmEts = TestUtils.loginAs(TestUtils.PURE_TM, TestUtils.PASSWORD_COMMON);
this.suEts = TestUtils.loginAs(TestUtils.PURE_SU, TestUtils.PASSWORD_COMMON);
this.mapper = EntityMapperFactory.getMapper();
this.mockReportService = mock(ReportService.class);
this.reportResource = new ReportResource(this.mockReportService, this.mapper);
}
@Override
protected TestContainerFactory getTestContainerFactory() throws TestContainerException {
return new JdkHttpServerTestContainerFactory();
}
@Override
protected Application configure() {
this.mockReportServiceJersey = new ReportServiceJerseyMockTest(); **//fields are here, because init() initiates later than configure()**
this.reportResource = new ReportResource(this.mockReportServiceJersey, this.mapper);
ResourceConfig config = new ResourceConfig(ReportResource.class);
AbstractBinder binder = new AbstractBinder() {
@Override
protected void configure() {
bindFactory(ReportServiceJerseyMockTest.class).to(IReportService.class);
}
};
config.register(binder);
return config;
}
@Test
public void testGetProjectManagersRestJersey() {
List<ManagerUserEntity> testList = (List<ManagerUserEntity>) new ReportServiceJerseyMockTest().getProjectManagers(tmEts, "login");
ManagerUserEntity managerUserEntity = target("/reports/project-managers").request().get(ManagerUserEntity.class); **// request equals to null**
assertEquals(testList, mapper.mapAsList(List of entities received from ReportResource.getProjectManagers, ManagerUserEntity.class));
(not ended)
}
}
ReportResource.java-正在測試的資源類
@Path("/reports/")
public class ReportResource {
@Inject
private IReportService reportService;
private MapperFacade mapper;
@GET
@Path("project-managers")
@Produces(MediaType.APPLICATION_JSON)
public Collection<ManagerUserDTO> getProjectManagers(@Context HttpServletRequest request,
@DefaultValue("All")
@QueryParam("lmLogin") String lmLogin) {
ETS ets = (ETS) request.getAttribute("ets"); **// request equals null**
Collection<ManagerUserEntity> entities = this.reportService.getProjectManagers(ets, lmLogin);
return mapper.mapAsList(entities, ManagerUserDTO.class);
}
}
堆棧跟蹤
javax.ws.rs.InternalServerErrorException: HTTP 500 Internal Server Error
at org.glassfish.jersey.client.JerseyInvocation.convertToException(JerseyInvocation.java:1020)
at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:816)
at org.glassfish.jersey.client.JerseyInvocation.access$700(JerseyInvocation.java:92)
at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:700)
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:444)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:696)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:420)
at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:316)
at test.rest.TestReportResource.testGetProjectManagersRestJersey(TestReportResource.java:104)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
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:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
所以有幾個問題
首先, JerseyTest
已經有一個@Before
方法 ,它在其中調用您的configure
方法創建Jersey應用程序。 這@Before
是您之前稱為@Before
,因此任何應用程序配置不應該在這個方法來完成。 可以將@Test
方法中使用的東西使用。
其次,您尚未配置MapperFacade
。 因此,您的資源類永遠都不會得到它。 我想這是500的原因(主要原因是NPE)。 所以做兩件事
在JerseyTest
的configure
方法中初始化MapperFacade
,然后在AbstractBinder
的configure
方法中對其進行configure
final MapperFacade mapper = EntityMapper.getMapper(); AbstractBinder binder = new AbstractBinder() { @Override protected void configure() { ... bind(mapper).to(MapperFacade.class); } };
在資源類中向其添加@Inject
批注
public class ReportResource { @Inject private MapperFacade mapper;
順便說一句,當出現500服務器錯誤,並且沒有堆棧跟蹤(針對真正的異常)時,通常會被框架吞噬並轉換為特定於框架的異常,這對您沒有太大幫助,就像您遇到的情況一樣。 在這種情況下,我只需要添加一個調試ExceptionMapper
打印堆棧跟蹤。 這應該給你一些信息
@Provider
public static class DebugMapper implements ExceptionMapper<Throwable> {
@Overrride
public Response toResponse(Throwable e) {
e.printStackTrace();
return Response.serverError().build();
}
}
@Override
public Application configure() {
ResourceConfig config = new ResourceConfig(..);
config.register(DebugMapper.class);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.