Fork me on GitHub

Custom routing

在许多项目中,Ninja的默认路由都很好. 以及忍者处理错误和异常的方式.

但是有时您只需要更多的灵活性和控制力即可. 假设您要向应用程序添加大量报告. 实际上,您想报告和记录所有请求,所有错误-应用程序内部正在进行的所有操作.

您可以尝试使用过滤器解决此问题. 但是,还有一种更强大的方法.

忍者conf的中心舞台.

Simple and straightforward

默认情况下,整个请求周期-以及对错误和异常的响应均由Ninja自己处理. 但是,通过在conf.Ninja位置创建一个类,您可以修改,扩展和调整默认处理.

最好的开始方法是扩展ninja.NinjaDefault . NinjaDefault是处理所有事件的默认类-因此,您无需编写太多代码即可简单地覆盖要调整的行为.

package conf;

public class Ninja extends NinjaDefault {

    @Inject
    CustomReportingMachine customReportingMachine;
    
    @Override
    public void onRouteRequest(Context.Impl context) {
        customReportingMachine.report(context);
        super.onRouteRequest(context);
    }
    
}

在上述情况下,您可以看到我们正在注入一个可记录每个请求的customReportingMachine. 然后,我们简单地调用super.onRouteRequest(context);. 并照常继续处理路线.

当然,您可以覆盖Ninja在请求生命周期中使用的所有基本方法. 从onRouteRequest(...)到onError(...)到onFrameworkStop(...)等.

All extension points of ninja.Ninja

NinjaDefault确实很棒,但是您也可以从头开始并自行定制所有内容. 唯一要记住的是,您的conf.Ninja类必须扩展ninja.Ninja (这是Ninja框架内的接口).

package ninja;

public interface Ninja {

	/**
	 * When a route is requested this method is called.
	 */
	void onRouteRequest(Context.Impl context);
    
    /**
     * This result should be used when an error occurs.
     * 
     * @param context The context for this request
     * @param exception The exception to handle. Can be used to customize error message.
     * @return a result you can use to render the error.
     */
    Result onException(Context context, Exception exception);
    
    /**
     * Should handle cases where an exception is thrown
     * when handling a route that let to an internal server error.
     * 
     * Should lead to a html error 500 - internal sever error
     * (and be used with the same mindset).
     * 
     * Usually used by onRouteRequest(...).
     */
    Result getInternalServerErrorResult(Context context, Exception exception);
    
    /**
     * Should handle cases where the client sent strange date that
     * led to an error.
     * 
     * Should lead to a html error 400 - bad request
     * (and be used with the same mindset).
     * 
     * Usually used by onRouteRequest(...).
     */
    Result getBadRequestResult(Context context, Exception exception);
    
    /**
     * Should handle cases where no route can be found for a given request.
     * 
     * Should lead to a html error 404 - not found
     * (and be used with the same mindset).
     * 
     * Usually used by onRouteRequest(...).
     */
    Result getNotFoundResult(Context context);
    
    /**
     * Should handle cases where access is forbidden
     * 
     * Should lead to a html error 403 - not found
     * (and be used with the same mindset).
     * 
     * Usually used by SecureFilter for instance(...).
     */
    Result getForbiddenResult(Context context);

    /**
     * Invoked when the framework starts. Usually inits stuff like the scheduler
     * and so on.
     */
    void onFrameworkStart();

    /**
     * Invoked when the server hosting Ninja is being stopped. Usually
     * shuts down the guice injector and stopps all services.
     */
    void onFrameworkShutdown();
    
    /**
     * Should be used to render an error. Any errors should be catched
     * and not reported in any way to the request.
     * 
     * For instance if your application catches a sever internal computation
     * error use this method and its implementations to render out
     * an error html page.
     */
    void renderErrorResultAndCatchAndLogExceptions(Result result, Context context);

}

ninja.Ninja很好地显示了您可以调整的有关请求生命周期的所有选项. 无论您自己实现ninja.Ninja还是直接扩展ninja.NinjaDefault都没有关系 . 但总的来说,扩展ninja.NinjaDefault是一个很好的起点.


by  ICOPY.SITE