Jersey RESTful Service 500错误(请求为空)

时间:2015-10-28 13:34:13

标签: java rest jersey

我是REST服务的新手,我想用Jersey Test Framework进行测试。 配置似乎没问题(非空)但调试器已显示请求为空。更改 target() -method中的路径没有任何结果。 找到了很多答案,但他们没有帮助。那么为什么请求可以为null?

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)

1 个答案:

答案 0 :(得分:0)

所以有几个问题

首先,JerseyTest already has a @Before method调用你configure方法创建Jersey应用程序。在您的 @Before之前调用此@Before,因此任何应用程序配置都不应该在此方法中完成。可以使用@Test方法中使用的内容。

其次,您尚未配置MapperFacade。所以你的资源类永远不会得到它。我想这是500的原因(主要原因是NPE)。做两件事

  1. MapperFacade的{​​{1}}方法中初始化configure,然后在JerseyTest configure方法中对其进行配置>

    AbstractBinder
  2. 在资源类

    中为其添加final MapperFacade mapper = EntityMapper.getMapper(); AbstractBinder binder = new AbstractBinder() { @Override protected void configure() { ... bind(mapper).to(MapperFacade.class); } }; 注释
    @Inject
  3. 顺便说一句,当获得500服务器错误,并且没有堆栈跟踪(对于真实的异常)时,框架经常吞下并转换为特定于框架的异常,没有多大帮助,就像你的情况一样。在这种情况下,我只需添加一个调试 public class ReportResource { @Inject private MapperFacade mapper; ,它打印堆栈跟踪。这应该给你一些信息

    ExceptionMapper