我正在尝试使用sitebricks创建一个动态Web模块,而不使用web.xml。 (仅限注释。)我一直在网上搜索好的例子,但所有这些都是包含嵌入式jetty Web服务器的简单java应用程序。
我已经创建了一个上下文监听器,仅用于测试:
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.servlet.GuiceServletContextListener;
import com.google.sitebricks.SitebricksModule;
public class AppConfig extends GuiceServletContextListener {
@Override
public Injector getInjector() {
return Guice.createInjector(new SitebricksModule() {
@Override
protected void configureSitebricks() {
at("/api/version").serve(APIVersion.class);
}
});
}
}
APIVersion课程:
@Service
public class APIVersion {
public class APIVersionResult {
public int major = 1;
public int minor = 0;
}
@Get
Reply<APIVersionResult> get() {
return Reply.with(new APIVersionResult()).as(Json.class);
}
}
还创建了一个Filter,但也许我不需要它:
import com.google.inject.servlet.GuiceFilter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
@WebFilter("/*")
public class AppFilter extends GuiceFilter implements Filter {
public AppFilter() {
super();
}
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
chain.doFilter(request, response);
}
public void init(FilterConfig fConfig) throws ServletException {
}
}
该项目具有动态Web模块方面。我可以把它编译成war文件。但我总是得到http://localhost/api/version的404错误
我错过了什么?
这是答案!我错过了一件事:@WebListener注释没有添加到我的GuiceServletContextListener中!
@WebListener
public class AppConfig extends GuiceServletContextListener {
@Override
public Injector getInjector() {
return Guice.createInjector(new SitebricksModule() {
@Override
protected void configureSitebricks() {
at("/api/version").serve(APIVersion.class);
}
});
}
}