| Since interface _IResource_ is marked as functional interface, a custom resource can also be implemented with a simple lambda expression that consumes a _IResource.Attributes_ parameter: |
| |
| [source,java] |
| ---- |
| IResource helloWorldRes = (attributes) -> |
| attributes.getResponse().write("Hello world!"); |
| ---- |
| |
| Lambda expressions come in handy also with _ResourceReference_ factory methods _of_ that accept a resource supplier as argument. Let's say we want to mount the resource of the previous example. Using lambdas the code looks like this: |
| |
| [source,java] |
| ---- |
| @Override |
| public void init() { |
| super.init(); |
| |
| IResource helloWorldRes = (attributes) -> |
| attributes.getResponse().write("Hello world!"); |
| |
| ResourceReference resRef = ResourceReference.of("helloworld", () -> helloWorldRes); |
| |
| mountResource("/helloworld", resRef); |
| } |
| ---- |
| |
| As first argument for factory methods we can specify the name of the resource reference or a key for it (an instance of _ResourceReference.Key_) |