Shaded
Shaded

Reputation: 17836

Using Wicket in a REST call

So I recently started using Wicket and I'm trying to make a REST call to an endpoint setup with RestEasy. I can make the call and can see it get hit successfully, but when trying to use Wicket components I'm getting the following error...

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

I'm assuming this is because I'm entering the application through a point that isn't managed by Wicket. I tried to get around the issue by ignoring Wicket, but it's so tightly coupled to our data that I can't find a way around it without rewriting tons of existing code.

Is there a way that I can either tell Wicket that it needs to manage this endpoint, or a way that I can get my application context once I'm inside my REST service?

Here's some relevant code.

@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;
}

Upvotes: 0

Views: 1322

Answers (2)

martin-g
martin-g

Reputation: 17513

Although you've found a way I'd share with you the official/designed way to accomplish this: https://github.com/apache/wicket/blob/master/wicket-core/src/main/java/org/apache/wicket/protocol/http/servlet/WicketSessionFilter.java Just make sure WicketSessionFilter is executed around RestEasy's Filter/Servlet.

The extra benefit is that you have access to Wicket's Session as well.

Upvotes: 1

Shaded
Shaded

Reputation: 17836

So after doing hours of research and not finding a clue I thought I would try something crazy. Using Spring @Autowired to wire the Application into my class. Aaaaand it worked... here's why...

In our Spring setup we have the Application defined as a bean, like so...

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

We use a custom application class so I'm not sure what other setups would look like, but it worked like a charm!

Hope this helps someone in the future!

Upvotes: 0

Related Questions