Skip to content

Commit a901271

Browse files
committed
Merge pull request #19 from decebals/suffixes
Document Content-Type by Suffix
2 parents 1a56911 + 4fc16ac commit a901271

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

_posts/2015-05-05-routes.md

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,23 @@ GET("/", (routeContext) -> routeContext.send("Hello World"));
2727

2828
Routes in Pippo are created using methods named after HTTP verbs. For instance, in the previous example, we created a route to handle GET requests to the root of the website. You have a corresponding method in Application for all commonly used HTTP verbs (GET, POST, DELETE, HEAD, PUT). For a basic website, only GET and POST are likely to be used.
2929

30-
The route that is defined first takes precedence over other matching routes. So the ordering of routes is crucial to the behavior of an application.
30+
The route that is defined first takes precedence over other matching routes. So the ordering of routes is crucial to the behavior of an application.
3131

3232
Each defined route has an __urlPattern__.
3333
The route can be static or dynamic:
3434

3535
- `static` ("/", "/hello", "/contacts/1")
3636
- `dynamic` (regex: "/.*" or parameterized: "/contact/{id}", "/contact/{id: [0-9]+}")
3737

38-
As you can see, it's easy to create routes with parameters. A parameter is wrapped by curly braces `{name}` and can optionally specify a regular expression.
38+
As you can see, it's easy to create routes with parameters. A parameter is wrapped by curly braces `{name}` and can optionally specify a regular expression.
3939

4040
You can retrieve the path parameter value for a request in type safe mode using:
4141

4242
```java
4343
GET("/contact/{id}", (routeContext) -> {
44-
int id = routeContext.getParameter("id").toInt(0);
44+
int id = routeContext.getParameter("id").toInt(0);
4545
String action = routeContext.getParameter("action").toString("new");
46-
46+
4747
Map<String, Object> model = new HashMap<>();
4848
model.put("id", id);
4949
model.put("action", action)
@@ -71,3 +71,33 @@ routeContext.uriFor("blog", parameters);
7171
// or
7272
routeContext.redirect("blog", parameters);
7373
```
74+
#### Content-Type by Suffix
75+
76+
You may optionally specify the response content-type with a URI suffix by appending a special regex pattern to your Route declaration.
77+
78+
The examples below assume you have a `ContentTypeEngine` registered for JSON, XML, and YAML.
79+
80+
```java
81+
// Register a route that optionally respects a content-type suffix
82+
// e.g. /contact/54
83+
// /contact/54.json
84+
// /contact/54.xml
85+
// /contact/54.yaml
86+
GET("/contact/{id: [0-9]+}(\\.(json|xml|yaml))?", (ctx) -> ctx.send(contact));
87+
88+
// Register a route that requires a content-type suffix
89+
// e.g. /contact/54.json
90+
// /contact/54.xml
91+
// /contact/54.yaml
92+
GET("/contact/{id: [0-9]+}(\\.(json|xml|yaml))", (ctx) -> ctx.send(contact));
93+
94+
// Register a route that requires a content-type suffix
95+
// e.g. /contact/john.json
96+
// /contact/john.xml
97+
// /contact/john.yaml
98+
GET("/contact/{id}(\\.(json|xml|yaml))", (ctx) -> ctx.send(contact));
99+
```
100+
101+
**Note:**
102+
103+
If you specify your parameter <u>without</u> a regex pattern, like the third example (e.g. `{id}`), the value of *id* will include your suffix unless you require the suffix using the pattern in the second example.

0 commit comments

Comments
 (0)