在 REST 调用中使用 Wicket

Using Wicket in a REST call

所以我最近开始使用 Wicket,并且我正在尝试使用 RestEasy 对端点设置进行 REST 调用。我可以拨打电话并可以看到它成功命中,但是在尝试使用 Wicket 组件时出现以下错误...

org.apache.wicket.WicketRuntimeException: There is no application attached to current thread

我假设这是因为我通过一个不受 Wicket 管理的点进入应用程序。我试图通过忽略 Wicket 来解决这个问题,但它与我们的数据紧密耦合,以至于我无法在不重写大量现有代码的情况下找到解决它的方法。

有什么方法可以告诉 Wicket 它需要管理这个端点,或者有什么方法可以在我进入 REST 服务后获取我的应用程序上下文吗?

这是一些相关代码。

@POST
@Path("/generate/{num}")
@Produces("text/plain")
@RolesAllowed({
        AuthorizeRole.ROLE
})
public Response generate(@PathParam("num") String num) throws Exception {
    Response response;
    Type incomingType = getType(reportBn);

    if(!incomingType.equals(Type.type)) {
        response = Response.status(Response.Status.BAD_REQUEST).entity("Could not process request").build();
        return response;
    }

    SomeObject newObj = new SomeObject(num);

    //This will cause the error, but it's actually 
    //getting called later, this is just to show why it's thrown.
    Application.get();

    response = Response.ok(newObj.getNum()).build();
    return response;
}

因此,经过数小时的研究并没有找到线索后,我想我会尝试一些疯狂的事情。使用 Spring @AutowiredApplication 连接到我的 class。 Aaaa 成功了……这就是为什么……

在我们的 Spring 设置中,我们将应用程序定义为一个 bean,就像这样...

<bean id="wicketApplication" class="... class that extends WebApplication">
    ....
</bean>

我们使用自定义应用程序 class 所以我不确定其他设置会是什么样子,但它的效果非常好!

希望这对以后的人有所帮助!

尽管您已经找到了一种方法,但我会与您分享 official/designed 实现此目的的方法:https://github.com/apache/wicket/blob/master/wicket-core/src/main/java/org/apache/wicket/protocol/http/servlet/WicketSessionFilter.java 只要确保 WicketSessionFilter 是围绕 RestEasy 的 Filter/Servlet.

执行的

额外的好处是您还可以访问 Wicket 的会话。