如何在Jersey容器中配置欢迎文件(HTML / JSP)

我有一个Jersey RESTful Web服务项目。 我已经在web.xml中配置了Jersey容器,一切正常。

在同一个项目中,我引入了一个HTML页面并包含在以处理其他一些非REST请求。 但是当我访问URL时,不会显示欢迎文件。

web.xml中评论Jersey容器配置并部署应用程序后,这次我可以访问欢迎文件。

我使用的是Tomcat 7,JDK 7,Jersey 2.2和Eclipse Juno。 Jersey配置后如何使欢迎文件正常工作? Jersey有没有限制,还是需要以不同的方式配置才能实现这一目的?

我的web.xml

   My Service  Jersey REST Service org.glassfish.jersey.servlet.ServletContainer  jersey.config.server.provider.packages com.my.rest.service  1   Jersey REST Service /*   index.html index.htm index.jsp default.html default.htm   

   com.webservice.services  Jersey REST Service com.sun.jersey.spi.container.servlet.ServletContainer  com.sun.jersey.config.property.packages com.webservice.services  1   Jersey REST Service /service/*   index.html   

尝试使用不同路径的URL模式,如上面给出的(/ service / *)用于REST。 服务器启动时,它可以工作并欢迎文件显示。

您当前的servlet映射是

  Jersey REST Service /* 

它将每个请求重定向到jersey。 所以要使欢迎页面可见,你需要输入像

  Jersey REST Service /rest 

这个模式只会为url调用泽西

http://localhost:8080/rest/

因此url

http://localhost:8080/index.html

不会被重定向到泽西servlet。

针对相同方案的项目托管在https://github.com/skohli0302/jims上

在web.xml中:

  Jersey REST Service /somethinghere/*  

代替

  Jersey REST Service /*  

你可以拥有类似的东西

  Jersey REST Service /*   default /index.html  

当你使用运动衫时,所有的请求都会被引导到jersey servlet,即ServletContainer。 因此,如果任何请求与任何映射的rest类不匹配,则抛出404.但是您始终可以添加servletfilter来拦截传入的请求。 根据传入的HTTP请求URL(defualt / welcome等),您可以决定将其重定向到weclome页面:

 HttpServletResponse httpResponse = (HttpServletResponse) response; httpResponse.sendRedirect("/welcome.jsp"); 

我只是很想知道,下面的例子会起作用吗?

HttpServletResponse httpResponse = (HttpServletResponse) response; httpResponse.sendRedirect("/welcome.jsp"); 如果它会,这个sendRedirect()被调用的地方? 在servlet中,所以如果我没有错,应该有一个servlet,它只是redirects请求redirects到第first/default页面,对吧?

您可以创建类“API”,并在项目中插入anotation。 Class ApplicationConfig …

@ApplicationPath(“api”)// anotation

公共类ApplicationConfig扩展Application {}

之后,我创建留下的“用户”类……

User UserApi类

@Path(“users”)// anotation page User。

公共类UserApi {

…. mycode补充页面….

@得到

 @Path("list") @Produces("application/json") public String getUsuarios() throws Exception { String json = new Gson().toJson(this.userD.listar()); return (json); } 

记住你的根项目保持… http://yourprojectpatc.com.br/api/users/list

使用“Postman”修改Json来发送您的数据