如何在上下文中设置ServletMapping路径模式(Tomcat上下文)

时间:2017-06-09 08:05:36

标签: java tomcat

我创建一个上下文并使用urlpattern /api指定为tomcat上下文。 现在,我将DefaultServlet添加到Tomcat并为jersey resourceConfig创建过滤器和地图。

Context context = tomcat.addContext("/api", base.getAbsolutePath());
Tomcat.addServlet(context, "default", new DefaultServlet());
context.addServletMapping("/*", "default");
final FilterDef def = new FilterDef();
final FilterMap map = new FilterMap();

def.setFilterName("jerseyFilter");
def.setFilter(getJerseyFilter());
context.addFilterDef(def);
map.setFilterName("jerseyFilter");
map.addURLPattern("/*");
context.addFilterMap(map);
tomcat.start();
private static Filter getJerseyFilter(){

    final ResourceConfig config = new ResourceConfig()
            .packages(Main.class.getPackage().getName())

            // create instance of Resource and dynamically configure to ResourceConfig
            .register(new Resource(new Core(), configuration))

            .register(JspMvcFeature.class) // register jspMVC
            .property(ServletProperties.FILTER_FORWARD_ON_404, true);

    return new ServletContainer(config);
}

现在,我已成功访问localhost:PORT/api/<end_point>

现在我的问题是,我想在ServletMapping中分配路径/serve/*,如

Tomcat.addServlet(context, "default", new DefaultServlet());
context.addServletMapping("/serve/*", "default");

然后我想访问localhost:PORT/api/serve/<end_point>

上的资源

但它提示错误

HTTP Status 404 -    
type Status report   
message    
description The requested resource is not available.    
Apache Tomcat/8.0.26

0 个答案:

没有答案
相关问题