如何跳过索引文件并直接显示 json 响应?
How do i skip the index file and directly show the json response?
我使用 Jersey 2.6
在 Java
中开发了 Rest API
,服务器是 Apache Tomcat
并在 JSON
中得到响应。当我 运行 通过右键单击项目并单击 run on server
选项时,会显示第一个 html
页面,即 index.html
下面是我的web.xml
文件
<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>Jersey Web Application</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.xyz.webservices</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/webapi/*</url-pattern>
</servlet-mapping>
</web-app>
index.jsp
文件的代码
<html>
<body>
<h2>Jersey RESTful Web Application!</h2>
<p><a href="webapi/getConfigFiles">Jersey resource</a>
</body>
</html>
这是我单击该按钮后调用的资源文件"Jersey Resource"
package com.xyz.webservices;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.json.JSONException;
import org.json.JSONObject;
import com.google.gson.Gson;
@Path("getConfigFiles")
public class Resource {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getConfigFiles()
{
Response response = null;
try {
List<String> listConfig = new ArrayList<>();
String ROOT_FILE_PATH="E:\eSamridhi\Data\ConfigData";
File f=new File(ROOT_FILE_PATH);
File[] allSubFiles=f.listFiles();
for (File file : allSubFiles)
{
listConfig.add(file.getName().replace(".xlsx", ""));
}
JSONObject object = new JSONObject();
object.put("ConfigFiles", listConfig);
System.out.println(object);
response = Response.status(Status.OK).entity(object.toString()).build();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return response;
}
}
这是我点击按钮后的回复
所以我的问题是访问 URL
后直接显示响应 JSON
而不是先显示索引文件。如何重定向或直接删除中间步骤以便我可以直接获取回应我是初学者因此没有得到。
任何帮助将不胜感激。
因为在此之后我正在开发前端,我需要通过上面的调用直接显示数据 API.
感谢您的考虑...
您可以通过多种不同的方式执行此操作。例如,您的 html 页面上可以有 javascript,它会在页面加载时执行按钮点击。另一种更好的方法是创建一个新的 servlet,并将 web.xml 中的欢迎文件列表映射到该 servlet。例如,使用 url 映射 /Test.
创建一个名为 'Test' 的 servlet
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>Test</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Jersey Web Application</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.xyz.webservices</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/webapi/*</url-pattern>
</servlet-mapping>
</web-app>
现在,当您的应用程序首次加载时,将调用 servlet 测试中的 doGet 方法。从这里我们可以调用您的资源 class
@WebServlet("/Test")
public class Test extends HttpServlet {
private static final long serialVersionUID = 1L;
public Test() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//here you should be able to call this method
Resource r = new Resource();
r.getConfigFiles();
}
}
我使用 Jersey 2.6
在 Java
中开发了 Rest API
,服务器是 Apache Tomcat
并在 JSON
中得到响应。当我 运行 通过右键单击项目并单击 run on server
选项时,会显示第一个 html
页面,即 index.html
下面是我的web.xml
文件
<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>Jersey Web Application</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.xyz.webservices</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/webapi/*</url-pattern>
</servlet-mapping>
</web-app>
index.jsp
文件的代码
<html>
<body>
<h2>Jersey RESTful Web Application!</h2>
<p><a href="webapi/getConfigFiles">Jersey resource</a>
</body>
</html>
这是我单击该按钮后调用的资源文件"Jersey Resource"
package com.xyz.webservices;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.json.JSONException;
import org.json.JSONObject;
import com.google.gson.Gson;
@Path("getConfigFiles")
public class Resource {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getConfigFiles()
{
Response response = null;
try {
List<String> listConfig = new ArrayList<>();
String ROOT_FILE_PATH="E:\eSamridhi\Data\ConfigData";
File f=new File(ROOT_FILE_PATH);
File[] allSubFiles=f.listFiles();
for (File file : allSubFiles)
{
listConfig.add(file.getName().replace(".xlsx", ""));
}
JSONObject object = new JSONObject();
object.put("ConfigFiles", listConfig);
System.out.println(object);
response = Response.status(Status.OK).entity(object.toString()).build();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return response;
}
}
这是我点击按钮后的回复
所以我的问题是访问 URL
后直接显示响应 JSON
而不是先显示索引文件。如何重定向或直接删除中间步骤以便我可以直接获取回应我是初学者因此没有得到。
任何帮助将不胜感激。
因为在此之后我正在开发前端,我需要通过上面的调用直接显示数据 API.
感谢您的考虑...
您可以通过多种不同的方式执行此操作。例如,您的 html 页面上可以有 javascript,它会在页面加载时执行按钮点击。另一种更好的方法是创建一个新的 servlet,并将 web.xml 中的欢迎文件列表映射到该 servlet。例如,使用 url 映射 /Test.
创建一个名为 'Test' 的 servletweb.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>Test</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Jersey Web Application</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.xyz.webservices</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/webapi/*</url-pattern>
</servlet-mapping>
</web-app>
现在,当您的应用程序首次加载时,将调用 servlet 测试中的 doGet 方法。从这里我们可以调用您的资源 class
@WebServlet("/Test")
public class Test extends HttpServlet {
private static final long serialVersionUID = 1L;
public Test() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//here you should be able to call this method
Resource r = new Resource();
r.getConfigFiles();
}
}