您可以使用jettyplus guiceplus jersey作为您的平台.
要做到这一点,你必须Bootstrap用main方法创建一个类.在main方法configure中jetty
Server server = new Server(port);
Context root = new Context(server, "/", Context.SESSIONS);
root.addEventListener(new GuiceServletConfig());
root.addFilter(GuiceFilter.class, "/*", 0);
Run Code Online (Sandbox Code Playgroud)
GuiceConfiguration负责jersey配置
public class GuiceConfiguration extends GuiceServletContextListener {
@Override
protected Injector getInjector() {
return Guice.createInjector(new ServletModule() {
@Override
protected void configureServlets() {
install(new RestServicesModule());
bind(MessageBodyReader.class).to(JacksonJsonProvider.class);
bind(MessageBodyWriter.class).to(JacksonJsonProvider.class);
serve("*").with(GuiceContainer.class, ImmutableMap.of("com.sun.jersey.config.feature.Trace",
"true"));
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
接下来,您必须创建您的休息服务并将其绑定RestServicesModule.
例如,您可以创建HelloWorld服务:
@Path("/hello")
public class HelloWorld {
@GET
@Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("{name}")
public Person showPerson(@PathParam("name") String name) {
return new Person(name);
}
}
Run Code Online (Sandbox Code Playgroud)
一个人是一个简单的POJO.
@XmlRootElement
public static class Person {
private String name;
public Person() {
}
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Run Code Online (Sandbox Code Playgroud)
最后一步是注册服务 RestServicesModule
class RestServicesModule extends AbstractModule {
protected void configure() {
bind(HelloWorld.class);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
286 次 |
| 最近记录: |