Created
August 29, 2012 12:10
-
-
Save mgenov/3511616 to your computer and use it in GitHub Desktop.
Sitebricks Sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Injector injector = Guice.createInjector(new AbstractModule() { | |
@Override | |
protected void configure() { | |
install(new SitebricksModule() { | |
@Override | |
protected void configureSitebricks() { | |
at("/r/customer").serve(CustomerService.class); | |
} | |
}); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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