Java 如何在 Jersey 容器中配置欢迎文件 (HTML/JSP)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19277888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to configure welcome file (HTML/JSP) in Jersey container
提问by Murugesh
I have a Jersey RESTful web service project. I have configured the Jersey container in the web.xmland everything works fine.
我有一个 Jersey RESTful Web 服务项目。我已经在web.xml 中配置了 Jersey 容器,一切正常。
In the same project, I have introduced one HTML page and included in the <welcome-file-list>
to handle some other non-REST request. But when I am accessing the URL, the welcome file is not displayed.
在同一个项目中,我引入了一个 HTML 页面并包含在 中<welcome-file-list>
以处理其他一些非 REST 请求。但是当我访问 URL 时,没有显示欢迎文件。
After I commented the Jersey container configuration in web.xmland deployed the application, this time I am able to access the welcome file.
在我在web.xml 中注释 Jersey 容器配置并部署应用程序后,这次我可以访问欢迎文件。
Am using Tomcat 7, JDK 7, Jersey 2.2 and Eclipse Juno. How to make the welcome file working when Jersey has configured? Is there any limitations with Jersey or do I need configure in different way to achieve this?
我正在使用 Tomcat 7、JDK 7、Jersey 2.2 和 Eclipse Juno。配置 Jersey 后如何使欢迎文件工作?Jersey 是否有任何限制,或者我是否需要以不同的方式配置来实现这一点?
My web.xml:
我的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com /xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>My Service</display-name>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.my.rest.service</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
</welcome-file-list>
</web-app>
采纳答案by Anil
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>com.webservice.services</display-name>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.webservice.services</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
Try URL pattern with different path like given above (/service/*) for REST. It works and welcome file displays when server starts.
尝试使用上面给出的不同路径的 URL 模式 (/service/*) 用于 REST。它工作并在服务器启动时显示欢迎文件。
回答by Juned Ahsan
When you use jersey, all the requests are directed to jersey servlet i.e. ServletContainer. So if any request that does not match to any mapped rest class, it throws 404. But you can always add servlet filters to intercept the incoming request. Depending on the incoming HTTP request URL(defualt/welcome etc), you can take a decision to redirect it to the weclome page:
当您使用 jersey 时,所有请求都被定向到 jersey servlet,即 ServletContainer。因此,如果任何请求与任何映射的其余类不匹配,则会抛出 404。但您始终可以添加 servlet 过滤器来拦截传入的请求。根据传入的 HTTP 请求 URL(默认/欢迎等),您可以决定将其重定向到 weclome 页面:
HttpServletResponse httpResponse = (HttpServletResponse) response;
httpResponse.sendRedirect("/welcome.jsp");
回答by Samy
I am just curious to know, will the below example work?
我只是想知道,下面的例子会起作用吗?
HttpServletResponse httpResponse = (HttpServletResponse) response;
httpResponse.sendRedirect("/welcome.jsp");
if it will, where this sendRedirect()
to be called? with in a servlet, so if i am not wrong, there should be a servlet, which just redirects
the request to the first/default
page, right?
HttpServletResponse httpResponse = (HttpServletResponse) response;
httpResponse.sendRedirect("/welcome.jsp");
如果会的话,sendRedirect()
这叫什么?在一个servlet中,所以如果我没有错,应该有一个servlet,它只是redirects
对first/default
页面的请求,对吗?
回答by Sagar
your current servlet mapping is
您当前的 servlet 映射是
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/*</url-pattern>
which redirects every request to jersey. so to make welcome page visible you need to make entry like
它将每个请求重定向到球衣。所以要使欢迎页面可见,您需要像这样输入
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest</url-pattern>
this pattern will call jersey only for urls like
这种模式只会为像这样的网址调用球衣
http://localhost:8080/rest/
http://localhost:8080/rest/
and thus url
因此网址
http://localhost:8080/index.html
http://localhost:8080/index.html
will not be redirected to jersey servlet.
不会被重定向到 jersey servlet。
A project targetting same scenario is hosted on https://github.com/skohli0302/jims
一个针对相同场景的项目托管在https://github.com/skohli0302/jims
回答by Ariella
In web.xml:
在 web.xml 中:
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/somethinghere/*</url-pattern>
</servlet-mapping>
instead of
代替
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
回答by user2584951
You can have something like
你可以有类似的东西
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/index.html</url-pattern>
</servlet-mapping>
回答by Dalvan Bevilaqua
you can create the class "API", and insert an anotation in your project. Class ApplicationConfig...
您可以创建类“API”,并在您的项目中插入注释。类 ApplicationConfig...
@ApplicationPath("api") //anotation
@ApplicationPath("api") //注解
public class ApplicationConfig extends Application { }
公共类 ApplicationConfig 扩展应用程序 { }
after, i create class "Users" that stay...
之后,我创建了类“用户”,保持...
Class UserApi
类 UserApi
@Path("users")//anotation page User.
@Path("users")//注解页面User.
public class UserApi {
公共类 UserApi {
.... mycode complement page....
.... mycode 补充页....
@GET
@得到
@Path("list")
@Produces("application/json")
public String getUsuarios() throws Exception {
String json = new Gson().toJson(this.userD.listar());
return (json);
}
remember that your root project stay ... http://yourprojectpatc.com.br/api/users/list
请记住,您的根项目保持... http://yourprojectpatc.com.br/api/users/list
using the "Postman" modify for Json to to send your data
使用“邮递员”修改 Json 来发送您的数据
回答by Lokesh Mrudhul
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.webservice.services</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>