Ninja Web Framework Documentation
repository·develop·Indexed 23 days ago
https://github.com/ninjaframework/ninjaA full-stack web framework for Java focusing on speed, reliability, and developer productivity. Supports Java 8, 11, 17, and 19/21. Documentation covers core concepts like the @Start annotation for startup logic, argument extractors for controller parameter injection, servlet integration testing with Docker, and developer guidelines for contributions and releases.
What's inside Ninja
- Ninja is a full-stack web framework for Java designed to be rock solid, fast, and highly productive. It supports multiple Java versions including Java 8, 11, 17, and 19/21.
Available testing tools in Ninja
developNinja provides several specialized testing approaches depending on your needs:
- Mocked Tests: For testing parts of your application in isolation.
- NinjaTest: For testing a running server at the HTTP level.
- NinjaDocTester: Ideal for documenting and testing JSON APIs.
- NinjaFluentLeniumTest: The recommended way to test HTML elements via Selenium on your Ninja application.
Ninja technology stack overview
developNinja is an integrated software stack that provides the following capabilities out of the box:
Frontend
- HTML rendering: Freemarker
- JSON/XML rendering & parsing: Jackson
Stateful RESTful features
- Session & Authentication:
ninja-session - Flash scope:
ninja-flash
Core Libraries
- Dependency Injection: Guice
- Configuration: Multiple environment configuration (Ninja)
- Internationalization (i18n): Support for templates and controllers (Ninja)
- Lifecycle Management: Ninja lifecycle
- Mail: Mail sending support
- Scheduling: Scheduler support
- Validation: JSR 303 object validation (Hibernate-validation)
- Utilities: Google Guava
- Logging: slf4j and logback
Data Persistence & Caching
- Relational Data: JPA (Hibernate) and Database migrations (Flyway)
- Cache Layer: Memcached and EhCache
Testing Support
- Mocking: Mockito
- Framework Testing:
NinjaTest - Documentation Testing:
NinjaDocTester - Browser/UI Testing:
NinjaFluentLeniumTest
Available Ninja modules by category
developNinja is an extensible framework. You can add functionality by integrating various modules for runtime platforms, server engines, template engines, databases, authentication, and more.
Runtime Platforms
- Google AppEngine Support:
https://github.com/ninjaframework/ninja-appengine
Server Engines
- Undertow standalone: An alternative to Jetty via
https://github.com/fizzed/ninja-undertow
Template Engines
- Rythm templates:
https://github.com/ninjaframework/ninja-rythm - Mustache templates:
https://github.com/kpacha/ninja-mustache - Jade4Ninja (Jade) templates:
https://github.com/mysu/jade4ninja - Rocker templates:
https://github.com/fizzed/ninja-rocker - Pebble templates:
https://github.com/jjfidalgo/ninja-pebbleorhttps://github.com/bordereast/ninja-pebble-module
Databases and ORM
- EBean RDBMS ORM (for EBean <= 7.2.3):
https://github.com/ninjaframework/ninja-ebean - EBean RDBMS ORM (for EBean > 7.2.3):
https://github.com/jfendler/ninja-ebean-ng - MongoDB/Morphia Integration:
https://github.com/bihe/ninja-mongodb - Cassandra:
https://github.com/fizzed/cassandra-plus
Authentication
- Auth0 (SaaS):
https://github.com/zileo-net/ninja-auth0
Process Engines
- Activity (Workflow/BPM):
http://mortezaadi.github.io/ninja-activiti-module/ - Camunda BPMN Integration:
http://github.com/FendlerConsulting/ninja-camunda - Executors (Long lived tasks):
https://github.com/fizzed/executors
Miscellaneous
- Prometheus (Metrics):
https://github.com/fizzed/prometheus-plus - Redis (Cache/Pooling):
https://github.com/fizzed/redis-plus - RabbitMQ (Pooling/Sessions):
https://github.com/fizzed/rabbitmq-plus - Hazelcast Cache:
https://github.com/raptaml/ninja-hazelcast-embedded - Quartz Scheduler:
https://github.com/FendlerConsulting/ninja-quartz - Sitemap Generator:
https://github.com/FendlerConsulting/ninja-sitemap
- Google AppEngine Support:
Compare Ninja support plans
developNinja offers three yearly support plans to assist with development best practices, deployment options, and bug reporting. Plans differ by maximum monthly support hours, response times for regular and critical incidents, and eligibility for priority bugfixes.
Feature Standard Premier Mission critical Email support Yes Yes Yes Phone support Yes Yes Yes Max support time/month 3h 10h 20h Response (Regular) 24h 18h 12h Response (Critical) 24h 12h 1h Priority bugfixes No Yes Yes If your plan includes priority bugfixes, reported bugs are guaranteed to be fixed in the next release.
What is Flash scope?
developThe Flash scope is a mechanism for transporting success and error messages between stateless web applications. It is implemented as a client-side cookie, similar to a Session, but it is not signed.
Flash messages have two primary lifecycles:
- Current request only: The message is available only for the immediate request.
- Current and next request: The message is available for the current request and the subsequent request, after which it is automatically deleted.
Understand JSON and XML error representations
developNinja uses content negotiation to return errors in JSON or XML formats if the client sends the appropriate
Acceptheader (application/jsonorapplication/xml).Errors are rendered as a
ninja.util.Messageobject containing atextfield and anerrorfield.JSON Format Example:
{ "text": "Oops. The requested route cannot be found.", "error": "My exception localized message." }XML Format Example:
<Message> <text>Oops. The requested route cannot be found.</text> <error>My exception localized message.</error> </Message>Important considerations when using the Servlet bridge
developWhen combining Ninja with Servlet-based components, keep the following architectural constraints in mind:
- Scalability and Sessions: Ninja is a stateless framework and does not use Servlet sessions. If you introduce Servlets or Filters that rely on
HttpSession, you lose the ability to scale your Ninja application easily. For session management, it is recommended to use Ninja's native session mechanism instead of Servlet sessions. - Runtime Compatibility: The
ServletModuleapproach only works when Ninja is running inside a Servlet container (e.g., Tomcat, Jetty). It will not work if you are running Ninja inside a Netty application, as Netty does not implement the Servlet specification.
- Scalability and Sessions: Ninja is a stateless framework and does not use Servlet sessions. If you introduce Servlets or Filters that rely on
Automatic object parsing and Context injection
developNinja can automatically inject the
Contextobject, which holds all information about the current request (parameters, headers, etc.).Additionally, Ninja can automatically parse arbitrary objects passed as method arguments. The parsing format (JSON, XML, or POST form) is determined by the
Content-Typerequest header.For POST forms, Ninja supports nested objects using dot notation. For example, if a
Userobject contains anAddressobject with astreetfield, a form field with the keyaddress.streetwill be correctly mapped.package controllers; @Singleton public class ApplicationController { public Result index( @PathParam("id") String id, @PathParam("email") String email, @Param("debug") Optional<String> debug, @Param("isAdmin") Boolean isAdmin, Context context, MyObject myObject) { // do something with the parameters... } }Switch between RecycledNinjaServerTester and FreshNinjaServerTester
developNinja provides two primary testing base classes depending on your isolation needs:
RecycledNinjaServerTester: Starts a single Ninja test server for all tests in a JUnit test class. Use this for high-performance testing when tests do not rely on a fresh server state.FreshNinjaServerTester: Starts a new Ninja test server for every individual test method. Use this if your tests require complete isolation or a clean server state for every execution.
Scaling Ninja applications
developNinja follows a 'share nothing' principle where sessions are managed client-side. This makes scaling horizontally very simple: you can add multiple Ninja server instances and distribute traffic among them using a reverse proxy. No complex session synchronization is required between instances.Define application messages with .properties files
developMessages are stored in files following the naming convention
messages_LANGUAGE.propertiesormessages_LANGUAGE-COUNTRY.properties.Ninja uses a hierarchical lookup for messages (from most specific to least specific):
messages_en-US.properties(if requested)messages_en.properties(fallback)messages.properties(global fallback)
Formatting: Ninja uses
java.text.MessageFormat. To use a literal apostrophe'in your message, you must escape it by using two apostrophes ('').Example
conf/messages_en.properties:# registration.ftl.html casinoRegistrationTitle=Register casinoYourUsername=Your username is: {0}