Java读取数据POST内容

我有一个API,我正在测试。 API接收POST请求并像这样读取它

      StringBuffer jb = new StringBuffer();
      String line = null;
      try {
        BufferedReader reader = request.getReader();
        while ((line = reader.readLine()) != null)
            jb.append(line);

        System.out.println("jb: "+jb);
        System.out.println("request.getHeader('content-type'): "+request.getHeader("content-type"));

      } catch (Exception e) { /*report an error*/ }

当我在“application / json; charset = utf-8”中发送POST请求时,

httpPost.setHeader("content-type", "application/json;charset=utf-8");

它打印这个:

jb: {"client_domain":"=....); //proper Json data
request.getHeader('content-type'): application/json;charset=utf-8

我可以正确读取数据。

然而,我的问题是,当我发送数据的方式相同,但我设置了内容类型“application / x-www-form-urlencoded; charset = utf-8”

httpPost.setHeader("content-type", "application/x-www-form-urlencoded;charset=utf-8");

测试是相同的,只是内容类型不同,但似乎我没有收到任何数据了:

jb: 
request.getHeader('content-type'): application/x-www-form-urlencoded;charset=utf-8

任何想法?

///更新

这是Spring控制器

@RequestMapping(value = {"user/add"}, method = RequestMethod.POST, produces="application/json; charset=utf-8")
@ResponseBody
public ResponseEntity<String> getNewUserApi(HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    Map<String, Object> jsonObj = new HashMap<String, Object>();

    StringBuffer jb = new StringBuffer();
      String line = null;
      try {
        BufferedReader reader = request.getReader();
        while ((line = reader.readLine()) != null)
            jb.append(line);

        System.out.println("jb: "+jb);
        System.out.println("request.getHeader('content-type'): "+request.getHeader("content-type"));

      } catch (Exception e) { /*report an error*/ }
    ///I create my JSon that will be sent back
    return JsonUtils.createJson(jsonObj);

//更新2这是我如何发送数据

public static void main(String[] args) throws Exception {

    String url = "http://localhost:8080/child/apiv1/user/add";
    CloseableHttpClient client = HttpClientBuilder.create().build();

    HttpPost httpPost = new HttpPost(url);
    httpPost.setHeader("content-type", "application/x-www-form-urlencoded;charset=utf-8");

    try {
        //we had to the parameters to the post request
        JSONObject json = new JSONObject();

        json.put("client_id", "fashfksajfhjsakfaskljhflakj");
        json.put("client_secret", "9435798243750923470925709348509275092");
        json.put("client_domain", "dummy.localhost.com");

        //create the user json object
        JSONObject userObj = new JSONObject();
        userObj.put("email", "johnsmith42@yopmail.com");
        userObj.put("name", "Anna Sax");

        JSONArray childrenArray = new JSONArray();

        JSONObject child1 = new JSONObject();
        child1.put("name", "Iphone 6");
        child1.put("age", "2");
        childrenArray.put(child1);
        userObj.put("children", childrenArray);
        json.put("user", childObj);

        StringEntity params = new StringEntity(json.toString());
        httpPost.setEntity(params);

        System.out.println("executing request: " + httpPost.getRequestLine());
        HttpResponse response;
        response = client.execute(httpPost);

   //[...]       

} //End main

我知道创建Json并将其发送到“application / x-www-form-urlencoded”并没有什么意义,但这只是我们的用户无法解决他的问题,它只会发送“应用程序/的X WWW窗体-urlencoded”。


@RequestMapping的'produce'属性表明客户端只能接受application / json数据,因此您可以将其删除或将其更改为'application / x-www-form-urlencoded'。

链接地址: http://www.djcxy.com/p/22227.html

上一篇: Java read data POST content

下一篇: File Upload with jquery AJAX and FormData