2014-01-29 40 views
6

在我的项目中,在客户端工作的团队要求我编写一个样本测试页面,并为他们提供一个他们可以打开并返回200的工作URL。他们有请我也给一个样品申请机构。 这是我将要给他们的请求主体。创建一个返回JSON响应的简单页面

{ 
"MyApp": { 
    "mAppHeader": { 
     "appId": "", 
     "personId": "", 
     "serviceName": "", 
     "clientIP": "", 
     "requestTimeStamp": "",  
     "encoding": "", 
     "inputDataFormat": "", 
     "outputDataFormat": "", 
     "environment": "" 
    }, 
    "requestPayload": { 
     "header": { 
      "element": "" 
     }, 
     "body": { 
      "request": { 
       "MyTestApp": { 
        "data": { 
         "AuthenticationRequestData": { 
          "appId": "", 
          "appPwd": "" 
         } 
        } 
       } 
      } 
     } 
    } 
    } 
} 

要开发样本页面,我不知道从哪里开始。请在你的downvotes(如果这个问题似乎不相关)上简单,因为我没有JSP Servlets的工作经验。

这是我现在所拥有的。它是一种简单的登录页面 -

<%@ page language="java" 
    contentType="text/html; charset=windows-1256" 
    pageEncoding="windows-1256" 
%> 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd"> 

<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=windows-1256"> 
     <title>Login Page</title> 
    </head> 

    <body> 
     <form action="TestClient"> 

      AppId  
      <input type="text" name="appID"/><br>  

      AppPassword 
      <input type="text" name="appPwd"/> 

      <input type="submit" value="submit">    

     </form> 
    </body> 
</html> 

这是我的servlet类 -

package com.test; 

import java.io.IOException; 

import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

public class TestClient extends HttpServlet { 
    private static final long serialVersionUID = 1L; 

    /** 
    * @see HttpServlet#HttpServlet() 
    */ 
    public TestClient() { 
     super(); 
     // TODO Auto-generated constructor stub 
    } 

    /** 
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 
    */ 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     response.setContentType("text/json"); 
     App app = new App(); 
     app.setAppID(request.getParameter("appID")); 
     app.setAppPassword(request.getParameter("appPwd"));  
    } 

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 

    } 

} 

我的应用是具有应用标识,并appPassword的getter和setter一个简单的Java类,所以我不打算在这里发布。

我的问题是 - 我是这样做的还是100%的错?请给我你的建议。

+0

如果你可以点我的链接在那里我能得到什么,需要一个更好的想法做这很好太。或者如果你能举一些例子,那也没关系。我只是在这里澄清我的方法。我觉得我没有把握,所以需要一些建议。 – rickygrimes

+0

如果他们要求使用JSON数据进行200响应,他们可能打算使用AJAX请求。这意味着可能有用户已经登录的期望,可能是你想要检查的东西。 –

+0

我没有任何答案,因为我面临类似的问题(虽然这是为了个人学习);我可以告诉你最近我学到的一件事:servlet是无状态的(所以如果你要检查那些_appID_和_appPwd_,那么你将无法区分两个不同的应用程序用户)。我想你会更好地将支票和json代移交给另一个bean。 – watery

回答

1

在doGet方法使用GSON jarLibrary生成JSON响应

像下面

Gson gson = new Gson(); 
HashMap map = new HashMap(); 

map.put("MyApp",object); 

String jsonString = gson.toJson(map); 
PrintWriter writer = response.getWriter(); 
writer.print(jsonString); 
0

你说你想从客户端向服务器传递一些参数,但同时显示一个JSON消息,然后你的servlet代码看起来好像参数通过请求传递。

如果您打算在请求正文中传递JSON,服务器将不会自动为您解析,因此您的调用request.getParameter("appID")request.getParameter("appPwd")将不起作用。

相反,你将需要解析的身体像这样的(基于示例消息以上):

JsonObject jsonRequest = Json.createReader(request.getInputStream()).readObject(); 

JsonObject authenticationRequestData = jsonRequest 
    .getJsonObject("MyApp") 
    .getJsonObject("requestPayload") 
    .getJsonObject("body") 
    .getJsonObject("request") 
    .getJsonObject("MyTestApp") 
    .getJsonObject("data") 
    .getJsonObject("AuthenticationRequestData"); 

App app = new App(); 
app.setAppID(authenticationRequestData.getJsonString("appID")); 
app.setAppPassword(authenticationRequestData.getJsonString("appPwd"));  

一旦你的servlet有你要用来创建响应的对象,你可以简单地生成在JSON并将其写入使用HttpServletResponse.getWriter()这样的:

SomeObject someObject = app.doSomething(); 

JsonObject jsonObject = Json.createObjectBuilder() 
.add("someValue", someObject.getSomeValue()) 
.add("someOtherValue", someObject.getSomeOtherValue()) 
.build(); 

response.setContentType("application/json"); 
Json.createJsonWriter(response.getWriter()).writeObject(jsonObject); 
2

你可以创建一个相同的格式格式化,然后只用内置的方法的Java对象

@WebServlet(urlPatterns = {"/BasicWebServices"}) 
public class BasicWebServices extends HttpServlet{ 

    private static final long serialVersionUID = 1L; 
    private static GsonBuilder gson_builder = new GsonBuilder().serializeNulls().setDateFormat("MM/dd/yyyy"); 
    public BasicWebServices(){ 
     super(); 
    } 
    @Override 
    public void destroy(){ 
      super.destroy(); 
    } 
    @Override 
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ 
     doPost(request, response); 
    } 
    @Override 
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ 
     try{ 
      Gson gson = BasicWebServices.gson_builder.create(); 
      MyObject myObject = new MyObject(); 
      response.getWriter().write(gson.toJson(myObject)); 
     } 
     catch(Exception e){ 
      response.getWriter().write("ERROR"); 
     } 
    } 
} 

然后,你只需要让你的MyObject与你的检索值相同的设置。 要么让一个对象设置相同,要么可以使用与发送它相同的Java类,如果可能的话。

为了您的你就必须有一个类MyApp的,然后有对象mAppHeader和requestPayload

public class mAppHeader(){ 
    String appId = ""; 
    String personId = ""; 
    String serviceName = ""; 
    String clientIP = ""; 
    String requestTimeStamp = "";  
    String encoding = ""; 
    String inputDataFormat = ""; 
    String outputDataFormat = ""; 
    String environment = ""; 
}