仅使用JSP页面创建JSON WebService

时间:2016-09-28 11:33:55

标签: java jsp java-api

我想仅使用JSP Page创建JSON WebService。 为此,我想在一个JSP文件中进行翻转。

我按照以下步骤操作:

档案>新>动态Web项目,名称为“test”

在“WebContent”文件夹中创建名为“test.jsp”

的新文件

下面提到的“test.jsp”文件代码

<%@ page import="javax.ws.rs.POST"%>
<%@ page import="javax.ws.rs.Path"%>
<%@ page import="javax.ws.rs.core.Response"%>
<%@ page import="javax.ws.rs.Consumes"%>
<%@ page import="javax.ws.rs.Produces"%>
<%@ page import="org.json.JSONObject"%>
<%@ page import="java.util.Date"%>
<%@ page import="java.util.TimeZone"%>
<%@ page import="java.text.DateFormat"%>
<%@ page import="java.text.SimpleDateFormat"%>

@Path("/")
<%!
    DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    SimpleDateFormat returnDateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm");
    @POST
    @Consumes("application/x-www-form-urlencoded")
    @Path("/Register")
    public Response Register(String json) {
        Date dt = new Date();
        formatter.setTimeZone(TimeZone.getTimeZone("IST"));
        String currentTime = formatter.format(dt);
        JSONObject returnJson = new JSONObject();
        try {
            JSONObject innerJsonObj = new JSONObject(json);
            String email = innerJsonObj.getString("email");

            JSONObject jsonData = new JSONObject();
            jsonData.put("id", email);
            returnJson.put("success", true);
        } catch (Exception e) {
            JSONObject errorJson = new JSONObject();
            errorJson.put("success", false);
            return Response.ok(errorJson.toString()).header("Access-Control-Allow-Origin", "*").build();
        }
        return Response.ok(returnJson.toString()).header("Access-Control-Allow-Origin", "*").build();
    }
%>

我的“web.xml”文件代码如下所示

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>test</display-name>
</web-app>

当我右键单击“test”项目并在服务器上运行时,它会出现以下错误

HTTP Status 404 - /test/

当我使用Chrome Rest Web服务客户端插件发布JSON时 带有“http://localhost:8080/test/Register”网址

的JSON数据
{"email": "test@xyz.com"}

它给出了以下回复

404, Not Found
Date: Wed, 28 Sep 2016 12:06:00 GMT
Server: Apache-Coyote/1.1
Content-Length: 1036
Content-Language: en
Content-Type: text/html;charset=utf-8

我的Tomcat 8服务器日志:

Sep 28, 2016 5:01:46 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:test' did not find a matching property.
Sep 28, 2016 5:01:46 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version:        Apache Tomcat/8.0.33
Sep 28, 2016 5:01:46 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built:          Mar 18 2016 20:31:49 UTC
Sep 28, 2016 5:01:46 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number:         8.0.33.0
Sep 28, 2016 5:01:46 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name:               Windows 8.1
Sep 28, 2016 5:01:46 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version:            6.3
Sep 28, 2016 5:01:46 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture:          amd64
Sep 28, 2016 5:01:46 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home:             C:\Program Files\Java\jdk1.8.0_45\jre
Sep 28, 2016 5:01:46 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version:           1.8.0_45-b15
Sep 28, 2016 5:01:46 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor:            Oracle Corporation
Sep 28, 2016 5:01:46 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE:         D:\Documents\Eclipse_Workspace\test\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Sep 28, 2016 5:01:46 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME:         C:\Program Files\Apache Software Foundation\Tomcat 8.0
Sep 28, 2016 5:01:46 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=D:\Documents\Eclipse_Workspace\test\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Sep 28, 2016 5:01:46 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=C:\Program Files\Apache Software Foundation\Tomcat 8.0
Sep 28, 2016 5:01:46 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=D:\Documents\Eclipse_Workspace\test\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps
Sep 28, 2016 5:01:46 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=C:\Program Files\Apache Software Foundation\Tomcat 8.0\endorsed
Sep 28, 2016 5:01:46 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=Cp1252
Sep 28, 2016 5:01:46 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jdk1.8.0_45\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/Java/jre1.8.0_45/bin/server;C:/Program Files/Java/jre1.8.0_45/bin;C:/Program Files/Java/jre1.8.0_45/lib/amd64;C:\ProgramData\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Skype\Phone\;C:\Program Files\TortoiseSVN\bin;C:\WINDOWS\System32;;.
Sep 28, 2016 5:01:47 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
Sep 28, 2016 5:01:47 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Sep 28, 2016 5:01:47 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-nio-8009"]
Sep 28, 2016 5:01:47 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Sep 28, 2016 5:01:47 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 2795 ms
Sep 28, 2016 5:01:47 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Sep 28, 2016 5:01:47 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/8.0.33
Sep 28, 2016 5:01:49 PM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Sep 28, 2016 5:01:49 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
Sep 28, 2016 5:01:49 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
Sep 28, 2016 5:01:49 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 2356 ms

我正在使用Eclipse Mars1,Tomcat 8和jdk1.8.0_45。

1 个答案:

答案 0 :(得分:1)

这是一个奇怪的结构。这取决于如何设置tomcat来编译jsp。很多时候,这是懒惰的,即第一次必要时。

在应用程序启动时(据我所知,只有这样),扫描类路径以找到JAX-RS注释并绑定它们。

所以你明白为什么它(并且可能永远不会)工作。