Skip to content

Instantly share code, notes, and snippets.

@mgenov
Created August 29, 2012 12:10
Show Gist options
  • Save mgenov/3511616 to your computer and use it in GitHub Desktop.
Save mgenov/3511616 to your computer and use it in GitHub Desktop.
Sitebricks Sample
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
install(new SitebricksModule() {
@Override
protected void configureSitebricks() {
at("/r/customer").serve(CustomerService.class);
}
});
}
});
Injector injector = Guice.createInjector();
// JS !!@@
Web web = injector.getInstance(Web.class);
WebClient<Customer> customerWebClient = web.clientOf("http://localhost:8899/r/customer").transports(Customer.class).over(Json.class);
Customer customer = new Customer("john Smith",25, Lists.newArrayList("first friend", "second friend"));
WebResponse response = customerWebClient.post(customer);
String responseString = response.to(String.class).using(Text.class);
System.out.println(responseString);
public class Customer {
private String name;
private int age;
private List<String> friendNames;
Customer() {
}
public Customer(String name, int age, List<String> friendNames) {
this.name = name;
this.age = age;
this.friendNames = friendNames;
}
public String getName() {
return name;
}
}
@Service
@At("/r/customer")
public class CustomerService {
@Get
@At("/:name")
public Reply<?> displayCustomer(@Named("name") String name) {
return Reply.with(new Customer(name, 20, Lists.newArrayList("Samantha","Smith", "Peter"))).as(Json.class);
}
@Post
public Reply<?> createCustomer(Request request) {
Customer customer = request.read(Customer.class).as(Json.class);
return Reply.saying().ok();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment