在 Java Servlet 中发送 JSON/GSON 请求会导致“错误请求”

我有想要获得有效响应的 servlet 代码。这是请求的原始布局


 {

  "function": "Check",

  "teamId": "<teamId>",

  "teamKey": "<teamKey>",

  "requestId": "<request-id>",

  "firstName": "<FirstName>",

  "lastName": "<LastName>",

  "ticketNumber": "<ticket-num>"

}

我在 Intellij 的 servlet 中有这个。


import com.google.gson.*;

import com.google.*;

import org.apache.*;

import org.apache.http.*;




@WebServlet(name = "Logincheck", urlPatterns = {"/Logincheck"})

public class Servlet extends HttpServlet {



String teamID = "IC106-2";

String teamKey = "1b3741ccf6d9ec5245055370125d901e";

String url="http://fys.securidoc.nl:11111/Ticket";

int Min = 1;

int Max = 100;

int REQ_ID =  Min + (int)(Math.random() *((Max - Min)+1));


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



    }

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


//index.html form user input

     String fname = request.getParameter("firstName");

    String lastName = request.getParameter("lname");

    String ticketNummer = request.getParameter("ticketnr");


    JsonParser parser = new JsonParser();


    URL object=new URL(url);


    String ticketCheck = "{\"function:\"Check\",\"teamId\":\"IC106-2\",\"teamKey\":\"1b3741ccf6d9ec5245055370125d901e\",\"requestId\":\""+REQ_ID+"\",\"firstName\":\""+fname+"\",\"lastName\":\""+lastName+"\",\"ticketNumber\":\""+ticketNummer+"\"}";


    }

}

现在,当我点击运行时,它会打开我的 index.html,当我单击登录按钮到 /Logincheck 时,它会响应(在 intellij 内):


Bad Request

Teamkey 和 TeamID 100% 正确,但我可能忽略了与 Json 相关的一些事情。一般来说,我对 Json 和 servlet 的经验很少。就像我制作硬编码的登录和票证凭据一样,还是用户的输入正确已经正确?我应该期待这样的回应:


{

  "ticketStatus": "<ticket-status>",

  "requestId": "<request-id>",

  "result": "<result-code>",

  "resultText": "<result-text>"

}


红颜莎娜
浏览 191回答 2
2回答

Cats萌萌

您应该将 json 数据写入请求正文。为此,您可以使用OutputStreamWriter类写入如下输出流HttpURLConnection:String ticketCheck = "{ \"function\":\"Check\",\"teamId\":IC106-2,\"teamKey\":1b3741ccf6d9ec5245055370125d901e,\"requestId\":1,\"firstName\":\"" + fname + "\" ,\"lastName\":\""+lastName+"\",\"ticketNumber\":\"\"" + ticketnummer + "\"}";HttpURLConnection con = (HttpURLConnection) object.openConnection();con.setDoOutput(true);con.setDoInput(true);con.setRequestProperty("Content-Type", "application/json");con.setRequestProperty("Accept", "application/json");con.setRequestMethod("POST");OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream());writer.write(ticketCheck);//this line closes the outputstream and actually makes the http request is sentwriter.flush();

holdtom

解决了。简单的报价错字。“错误请求”意味着语法错误,我忘记在函数旁边添加 \" 。&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;ticketCheck&nbsp;=&nbsp;"{\"function\":\"Check\",\"teamId\":\"IC106-2\",\"teamKey\":\"1b3741ccf6d9ec5245055370125d901e\",\"requestId\":\""+REQ_ID+"\",\"firstName\":\""+fname+"\",\"lastName\":\""+lastName+"\",\"ticketNumber\":\""+ticketNummer+"\"}";上面正确的json
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java