Java – restlet server resources required to use constructors

Get this error in restlet:

ForwardUIApplication ; Exception while instantiating the target server resource.
java.lang.InstantiationException: me.unroll.forwardui.server.ForwardUIServer$UnsubscribeForwardUIResource

I know why This is because my constructor is as follows:

public UnsubscribeForwardUIResource(@R_404_1638@ConnectionPool connectionPool) {

Restlet accesses resources as follows:

router.attach(Config.unsubscribeUriPattern(),UnsubscribeForwardUIResource.class);

The problem is that I really need this ctor parameter How do I make it accessible? (note that I don't use any IOC framework, just a lot of ctor parameters, but this is actually an IOC mode)

Solution

You can use context to pass the context properties of a resource instance

From serverresource API doc:

Therefore, at the attachment time:

router.getContext().getAttributes().put(CONNECTION_POOL_KEY,connectionPool);
router.attach(Config.unsubscribeUriPattern(),UnsubscribeForwardUIResource.class);

In your unsubscribeforwarduiresource class, you must move the initialization code in the constructor to the de doinit method:

public UnsubscribeForwardUIResource() {
    //default constructor can be empty
}

protected void doInit() throws ResourceException {

     @R_404_1638@ConnectionPool connectionPool = (@R_404_1638@ConnectionPool) getContext().getAttributes().get(CONNECTION_POOL_KEY);

    // initialization code goes here
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>