Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: None  |  size: 1.56 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. A Java library for web services that integrates well with Jetty and Guice?
  2. Server server = new Server(port);
  3. Context root = new Context(server, "/", Context.SESSIONS);
  4. root.addEventListener(new GuiceServletConfig());
  5. root.addFilter(GuiceFilter.class, "/*", 0);
  6.        
  7. public class GuiceConfiguration extends GuiceServletContextListener {
  8.     @Override
  9.     protected Injector getInjector() {
  10.         return Guice.createInjector(new ServletModule() {
  11.             @Override
  12.             protected void configureServlets() {
  13.                 install(new RestServicesModule());    
  14.  
  15.  
  16.                 bind(MessageBodyReader.class).to(JacksonJsonProvider.class);
  17.                 bind(MessageBodyWriter.class).to(JacksonJsonProvider.class);
  18.  
  19.                 serve("*").with(GuiceContainer.class, ImmutableMap.of("com.sun.jersey.config.feature.Trace",
  20.                                 "true"));
  21.             }
  22.         });
  23.     }
  24. }
  25.        
  26. @Path("/hello")
  27. public class HelloWorld {
  28.  
  29.     @GET
  30.     @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
  31.     @Path("{name}")
  32.     public Person showPerson(@PathParam("name") String name) {
  33.         return new Person(name);
  34.     }
  35.  
  36. }
  37.        
  38. @XmlRootElement
  39. public static class Person {
  40.     private String name;
  41.  
  42.     public Person() {
  43.     }
  44.  
  45.     public Person(String name) {
  46.         this.name = name;
  47.     }
  48.  
  49.     public String getName() {
  50.         return name;
  51.     }
  52.  
  53.     public void setName(String name) {
  54.         this.name = name;
  55.     }
  56. }
  57.        
  58. class RestServicesModule extends AbstractModule {
  59.     protected void configure() {
  60.        bind(HelloWorld.class);
  61.     }
  62. }