Skip to content

Commit 737771f

Browse files
committed
Doc + Handlebars and Jackson cleanup
1 parent 8b040e6 commit 737771f

File tree

7 files changed

+217
-32
lines changed

7 files changed

+217
-32
lines changed

docs/asciidoc/modules.adoc

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Modules (unlike in other frameworks) are thin and do a lot of work to bootstrap
66
external library, but they DO NOT provide a new level of abstraction nor [do] they provide a custom
77
API to access functionality in that library. Instead they expose the library components as they are.
88

9-
Modules are shipped as separated jar/dependency and usually implement the javadoc:Extension[] API.
9+
Modules are distributed as separated jar/dependency and usually implement the javadoc:Extension[] API.
1010

1111
In general they provide a `builder` class to create the and configure the external library from
1212
configuration properties.
@@ -17,12 +17,16 @@ same name. To avoid name collisions we choose one of following convention:
1717
- An abbreviation of the name/library. For example: `Hibernate` -> `Hbm`, `Handlebars -> Hbs`, `Freemarker` -> `Ftl`.
1818
- Add a `by` suffix. For example: `Hibernate` -> `Hibernateby`, `Guice` -> `Guiceby`, `Handelbars` -> `Handlebarsby`
1919
20+
Available modules are listed next.
21+
2022
=== Data
23+
* link:modules/hikari[HikariCP]: A high-performance JDBC connection pool.
2124

22-
- link:modules/hikari[Hikari Connection Pool]: A high-performance JDBC connection pool.
25+
=== JSON
26+
* link:modules/jackson[Jackson]: Jackson module for Jooby.
2327

2428
=== Template Engine
25-
26-
- link:modules/freemarker[Freemarker Template Engine]: Freemarker templates for Jooby.
29+
* link:modules/handlebars[Handlebars]: Handlebars templates for Jooby.
30+
* link:modules/freemarker[Freemarker]: Freemarker templates for Jooby.
2731

2832
.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
== Handlebars
2+
3+
https://github.com/jknack/handlebars.java[Handlebars templates] for Jooby.
4+
5+
=== Usage
6+
7+
1) Add the dependency:
8+
9+
[dependency, artifactId="jooby-handlebars"]
10+
.
11+
12+
2) Write your templates inside the `views` folder
13+
14+
.views/index.hbs
15+
[source, html]
16+
----
17+
<p> Hello {{name}}! </p>
18+
----
19+
20+
2) Install and use freemarker templates
21+
22+
.Java
23+
[source, java, role="primary"]
24+
----
25+
import io.jooby.handlebars.Hbs;
26+
27+
{
28+
install(new Hbs());
29+
30+
get("/", ctx -> {
31+
return new ModelAndView("index.hbs")
32+
.put("name", "Jooby");
33+
});
34+
}
35+
----
36+
37+
.Kotlin
38+
[source, kt, role="secondary"]
39+
----
40+
import io.jooby.handlebars.Hbs
41+
42+
{
43+
install(Hbs())
44+
45+
get("/") { ctx ->
46+
ModelAndView("index.hbs")
47+
.put("name", "Jooby")
48+
}
49+
}
50+
----
51+
52+
=== Templates Location
53+
54+
Template location is set to `views`. The `views` folder/location is expected to be at the current
55+
user directory or at root of classpath.
56+
57+
You can override the default location by setting the `handlebars.templatePath` property.
58+
59+
=== Template Cache
60+
61+
The handlebars module turn off cache while running in `dev` or `test` environment. For any other
62+
environment it use https://github.com/jknack/handlebars.java#the-cache-system[HighConcurrencyTemplateCache]
63+
64+
=== Custom configuration
65+
66+
Custom Handlebars object can be provided it programmatically:
67+
68+
.Java
69+
[source, java, role="primary"]
70+
----
71+
import io.jooby.handlebars.hbs;
72+
73+
{
74+
Handlebars handlebars = new Handlebars();
75+
76+
install(new Hbs(handlebars));
77+
}
78+
----
79+
80+
.Kotlin
81+
[source, kt, role="secondary"]
82+
----
83+
import io.jooby.freemarker.Freemarker
84+
85+
{
86+
val handlebars = Handlebars();
87+
88+
install(Hbs(handlebars))
89+
}
90+
----

docs/asciidoc/modules/jackson.adoc

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
== Jackson
2+
3+
JSON support using https://github.com/FasterXML/jackson[Jackson] library.
4+
5+
=== Usage
6+
7+
1) Add the dependency:
8+
9+
[dependency, artifactId="jooby-jackson"]
10+
.
11+
12+
2) Install and encode/decode JSON
13+
14+
.Java
15+
[source, java, role="primary"]
16+
----
17+
import io.jooby.json.Jackson;
18+
19+
{
20+
install(new Jackson()); <1>
21+
22+
get("/", ctx -> {
23+
MyObject myObject = ...;
24+
return myObject; <2>
25+
});
26+
27+
post("/", ctx -> {
28+
MyObject myObject = ctx.body(MyObject.class); <3>
29+
...
30+
});
31+
}
32+
----
33+
34+
.Kotlin
35+
[source, kt, role="secondary"]
36+
----
37+
import io.jooby.json.Jackson;
38+
39+
{
40+
install(Jackson()) <1>
41+
42+
get("/") { ctx ->
43+
val myObject = ...;
44+
myObject <2>
45+
}
46+
47+
post("/") { ctx ->
48+
val myObject = ctx.body<MyObject>() <3>
49+
...
50+
}
51+
}
52+
----
53+
54+
<1> Install Jackson
55+
<2> Use Jackson to encode arbitrary object as JSON
56+
<2> Use Jackson to decode JSON to Java object
57+
58+
=== Working with ObjectMapper
59+
60+
Access to default object mapper is available via require call:
61+
62+
.Default object mapper
63+
[source, java, role="primary"]
64+
----
65+
import io.jooby.json.Jackson;
66+
67+
{
68+
install(new Jackson());
69+
70+
ObjectMapper mapper = require(ObjectMapper.class);
71+
72+
...
73+
}
74+
----
75+
76+
.Kotlin
77+
[source, kt, role="secondary"]
78+
----
79+
import io.jooby.json.Jackson;
80+
81+
{
82+
install(Jackson())
83+
84+
val mapper = require<ObjectMapper>()
85+
}
86+
----

docs/src/main/java/io/jooby/adoc/DocApp.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public static void main(String[] args) throws Exception {
4646

4747
log.info("doc ready");
4848

49-
runApp(new String[]{"server.join=false"}, DocApp::new);
49+
runApp(new String[]{"server.join=false", "server.port=4000"}, DocApp::new);
5050

5151
DirectoryWatcher watcher = DirectoryWatcher.builder()
5252
.path(basedir.resolve("asciidoc"))

jooby/src/main/kotlin/io/jooby/Kooby.kt

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ import kotlin.reflect.KClass
1414
annotation class RouterDsl
1515

1616
/** Registry: */
17+
inline fun <reified T> Registry.require(): T {
18+
return this.require(T::class.java)
19+
}
20+
21+
inline fun <reified T> Registry.require(name: String): T {
22+
return this.require(T::class.java, name)
23+
}
24+
1725
fun <T:Any> Registry.require(klass: KClass<T>): T {
1826
return this.require(klass.java)
1927
}
@@ -43,30 +51,26 @@ operator fun Value.get(index: Int): Value {
4351
return this.get(index)
4452
}
4553

46-
inline fun <reified T : Any> Value.to(): T {
47-
val reified = object : Reified<T>() {}
48-
return this.to(reified)
54+
inline fun <reified T> Value.to(): T {
55+
return this.to(object : Reified<T>() {})
4956
}
5057

5158
/** Context: */
52-
inline fun <reified T : Any> Context.query(): T {
59+
inline fun <reified T> Context.query(): T {
5360
val reified = object : Reified<T>() {}
5461
return this.query(reified)
5562
}
5663

57-
inline fun <reified T : Any> Context.form(): T {
58-
val reified = object : Reified<T>() {}
59-
return this.form(reified)
64+
inline fun <reified T> Context.form(): T {
65+
return this.form(object : Reified<T>() {})
6066
}
6167

62-
inline fun <reified T : Any> Context.multipart(): T {
63-
val reified = object : Reified<T>() {}
64-
return this.multipart(reified)
68+
inline fun <reified T> Context.multipart(): T {
69+
return this.multipart(object : Reified<T>() {})
6570
}
6671

67-
inline fun <reified T : Any> Context.body(): T {
68-
val reified = object : Reified<T>() {}
69-
return this.body(reified)
72+
inline fun <reified T> Context.body(): T {
73+
return this.body(object : Reified<T>() {})
7074
}
7175

7276
/** Handler context: */

modules/jooby-handlebars/src/main/java/io/jooby/handlebars/Hbs.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,17 @@
1818
import com.github.jknack.handlebars.Decorator;
1919
import com.github.jknack.handlebars.Handlebars;
2020
import com.github.jknack.handlebars.Helper;
21-
import com.github.jknack.handlebars.Template;
2221
import com.github.jknack.handlebars.cache.HighConcurrencyTemplateCache;
2322
import com.github.jknack.handlebars.cache.NullTemplateCache;
2423
import com.github.jknack.handlebars.cache.TemplateCache;
2524
import com.github.jknack.handlebars.io.ClassPathTemplateLoader;
2625
import com.github.jknack.handlebars.io.FileTemplateLoader;
2726
import com.github.jknack.handlebars.io.TemplateLoader;
28-
import io.jooby.Context;
27+
import com.typesafe.config.Config;
2928
import io.jooby.Environment;
3029
import io.jooby.Extension;
3130
import io.jooby.Jooby;
3231
import io.jooby.MediaType;
33-
import io.jooby.ModelAndView;
34-
import io.jooby.TemplateEngine;
3532

3633
import javax.annotation.Nonnull;
3734
import java.io.File;
@@ -44,8 +41,7 @@
4441
import java.nio.file.Files;
4542
import java.nio.file.Path;
4643
import java.nio.file.Paths;
47-
48-
import static java.util.Optional.ofNullable;
44+
import java.util.Optional;
4945

5046
public class Hbs implements Extension {
5147

@@ -57,7 +53,7 @@ public static class Builder {
5753

5854
private TemplateCache cache;
5955

60-
private String templatePath = "views";
56+
private String templatePath;
6157

6258
public Builder() {
6359
handlebars = new Handlebars();
@@ -134,7 +130,14 @@ public Builder() {
134130

135131
public @Nonnull Handlebars build(@Nonnull Environment env) {
136132
if (loader == null) {
137-
loader = defaultTemplateLoader(env, templatePath);
133+
Config config = env.getConfig();
134+
String defaultTemplatePath = config.hasPath("handlebars.templatePath")
135+
? config.getString("handlebars.templatePath")
136+
: "views";
137+
String templatePath = Optional.ofNullable(this.templatePath)
138+
.orElse(defaultTemplatePath);
139+
setTemplatePath(templatePath);
140+
loader = defaultTemplateLoader(env, this.templatePath);
138141
}
139142
handlebars.with(loader);
140143

modules/jooby-jackson/src/main/java/io/jooby/json/Jackson.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,13 @@
2828
import io.jooby.MediaType;
2929
import io.jooby.Parser;
3030
import io.jooby.Renderer;
31+
import io.jooby.ServiceRegistry;
3132

3233
import javax.annotation.Nonnull;
3334
import java.io.InputStream;
3435
import java.lang.reflect.Type;
3536

3637
public class Jackson implements Extension, Parser, Renderer {
37-
private static final byte[] ARRAY_START = {'['};
38-
39-
private static final byte[] ARRAY_SEPARATOR = {','};
40-
41-
private static final byte[] ARRAY_END = {']'};
42-
4338
private final ObjectMapper mapper;
4439

4540
public Jackson(ObjectMapper mapper) {
@@ -64,6 +59,9 @@ public static final ObjectMapper defaultObjectMapper() {
6459
@Override public void install(Jooby application) {
6560
application.parser(MediaType.json, this);
6661
application.renderer(MediaType.json, this);
62+
63+
ServiceRegistry services = application.getServices();
64+
services.put(ObjectMapper.class, mapper);
6765
}
6866

6967
@Override public byte[] render(@Nonnull Context ctx, @Nonnull Object value) throws Exception {

0 commit comments

Comments
 (0)