Skip to content

Commit aea7e85

Browse files
author
Alexander Tarnowski
committed
Added files for a simple Restlet/Jackson example
1 parent 5c73733 commit aea7e85

File tree

9 files changed

+91
-0
lines changed

9 files changed

+91
-0
lines changed

java/restlet/build.gradle

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
apply plugin: 'java'
2+
3+
repositories {
4+
mavenCentral()
5+
}
6+
7+
dependencies {
8+
compile files(fileTree(dir: 'lib', includes: ['*.jar']))
9+
compile group: 'org.codehaus.jackson', name: 'jackson-core-lgpl', version: '1.9.11'
10+
compile group: 'org.codehaus.jackson', name: 'jackson-mapper-lgpl', version: '1.9.11'
11+
}
5.11 KB
Binary file not shown.

java/restlet/lib/org.restlet.jar

709 KB
Binary file not shown.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package se.tarnowski.bootstrap.restlet;
2+
3+
import org.restlet.Component;
4+
import org.restlet.data.Protocol;
5+
import se.tarnowski.bootstrap.restlet.resource.JsonResource;
6+
import se.tarnowski.bootstrap.restlet.resource.TextResource;
7+
8+
public class Main {
9+
public static void main(String[] args) throws Exception {
10+
Component component = new Component();
11+
component.getServers().add(Protocol.HTTP, 8182);
12+
component.getDefaultHost().attach("/text", TextResource.class);
13+
component.getDefaultHost().attach("/json", JsonResource.class);
14+
component.start();
15+
}
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package se.tarnowski.bootstrap.restlet.representation;
2+
3+
public class SimpleResponse {
4+
public String response;
5+
6+
public SimpleResponse() {
7+
this("Default response");
8+
}
9+
10+
public SimpleResponse(String response) {
11+
this.response = response;
12+
}
13+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package se.tarnowski.bootstrap.restlet.request;
2+
3+
public class SimpleRequest {
4+
public String parameter;
5+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package se.tarnowski.bootstrap.restlet.resource;
2+
3+
import org.codehaus.jackson.map.ObjectMapper;
4+
import org.restlet.ext.jackson.JacksonRepresentation;
5+
import org.restlet.representation.Representation;
6+
import org.restlet.resource.Get;
7+
import org.restlet.resource.Post;
8+
import org.restlet.resource.ServerResource;
9+
import se.tarnowski.bootstrap.restlet.representation.SimpleResponse;
10+
import se.tarnowski.bootstrap.restlet.request.SimpleRequest;
11+
12+
public class JsonResource extends ServerResource {
13+
14+
private static ObjectMapper objectMapper = new ObjectMapper();
15+
16+
@Get("json")
17+
public Representation produceResponse() {
18+
return new JacksonRepresentation<SimpleResponse>(new SimpleResponse());
19+
}
20+
21+
// Gotcha: the Representation argument is crucial. Otherwise HTTP error code 415 will be returned.
22+
@Post("json")
23+
public Representation echoString(Representation representation) {
24+
try {
25+
SimpleRequest request = objectMapper.readValue(representation.getStream(), SimpleRequest.class);
26+
return new JacksonRepresentation<SimpleResponse>(new SimpleResponse(request.parameter));
27+
} catch (Exception e) {
28+
throw new IllegalStateException(e);
29+
}
30+
}
31+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package se.tarnowski.bootstrap.restlet.resource;
2+
3+
import org.restlet.resource.Get;
4+
import org.restlet.resource.ServerResource;
5+
6+
public class TextResource extends ServerResource {
7+
8+
@Get
9+
public String produceResponse() {
10+
return "A text response";
11+
}
12+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"parameter":"value from request"
3+
}

0 commit comments

Comments
 (0)