POST 請求接收JSON資料
阿新 • • 發佈:2018-12-19
一般請求介面我們使用的是GET和POST請求方式,
GET請求沒什麼好說的,
POST請求可以form表單提交請求的,也有用JSON資料請求的,而json資料在springmvc中使用@RequestBody進行接收,
下面是用HttpServletRequest接收的辦法:
@RequestMapping("/notify") public void notify(HttpServletRequest request_, HttpServletResponse response /*1. requestBody讀取*/, @RequestBody String request) { try { //LOGGER.info("JSON DATA >> " + request); /*4.二進位制讀取的工具類,下文有 String responseStrBuilder = GetRequestJsonUtils.getRequestJsonObject(request_); LOGGER.info("responseStrBuilder DATA >> " + responseStrBuilder);*/ //2,字串讀取 /*BufferedReader br = request_.getReader(); String str, wholeStr = ""; while((str = br.readLine()) != null){ wholeStr += str; } LOGGER.info("wholeStr DATA >> " + wholeStr);*/ //3.二進位制讀取 /*InputStream is= null; is = request_.getInputStream(); String bodyInfo = IOUtils.toString(is, "utf-8"); LOGGER.info("bodyInfo DATA >> " + bodyInfo);*/ } catch (Exception e) { LOGGER.error(e.getMessage()); e.printStackTrace(); } }
接下來測試一下
使用POSTMAN測試工具
放開上文的註釋4請求該介面
2018-12-19 10:56:07,793 INFO (IndexController.java:59)- JSON DATA >> {"name":"value"} 2018-12-19 10:56:07,795 INFO (GetRequestJsonUtils.java:26)- 請求方式 >>POST 2018-12-19 10:56:07,795 INFO (IndexController.java:62)- responseStrBuilder DATA >>
將@RequestBody註釋並且將註釋4,1放開再請求介面
java.lang.IllegalStateException: getInputStream() has already been called for this request at org.apache.catalina.connector.Request.getReader(Request.java:1205) at org.apache.catalina.connector.RequestFacade.getReader(RequestFacade.java:504) at com.cms.controller.index.IndexController.notify(IndexController.java:64) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:116) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) at or 2018-12-19 11:02:25,466 INFO (IndexController.java:62)- responseStrBuilder DATA >> {"name":"value"}
可以得出結論:
1.如果已經用@RequestBody接收JSON,那麼呼叫其它方法接收時將獲取不到資料。
2.如果request.getInputStream(); request.getReader();和request.getParameter(“key”);這三個函式中任何一個函式執行一次後(可正常讀取JSON資料),之後再執行就無效了,並且丟擲如上異常。
下面是一個支援POST請求接收JSON二進位制讀取的工具類
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import com.alibaba.fastjson.JSONObject;
public class GetRequestJsonUtils {
public static JSONObject getRequestJsonObject(HttpServletRequest request) throws IOException {
String json = getRequestJsonString(request);
return JSONObject.parseObject(json);
}
/***
* 獲取 request 中 json 字串的內容
*
* @param request
* @return : <code>byte[]</code>
* @throws IOException
*/
public static String getRequestJsonString(HttpServletRequest request)
throws IOException {
String submitMehtod = request.getMethod();
// GET
if (submitMehtod.equals("GET")) {
return new String(request.getQueryString().getBytes("iso-8859-1"),"utf-8").replaceAll("%22", "\"");
// POST
} else {
return getRequestPostStr(request);
}
}
/**
* 描述:獲取 post 請求的 byte[] 陣列
* <pre>
* 舉例:
* </pre>
* @param request
* @return
* @throws IOException
*/
public static byte[] getRequestPostBytes(HttpServletRequest request)
throws IOException {
int contentLength = request.getContentLength();
if(contentLength<0){
return null;
}
byte buffer[] = new byte[contentLength];
for (int i = 0; i < contentLength;) {
int readlen = request.getInputStream().read(buffer, i,
contentLength - i);
if (readlen == -1) {
break;
}
i += readlen;
}
return buffer;
}
/**
* 描述:獲取 post 請求內容
* <pre>
* 舉例:
* </pre>
* @param request
* @return
* @throws IOException
*/
public static String getRequestPostStr(HttpServletRequest request)
throws IOException {
byte buffer[] = getRequestPostBytes(request);
String charEncoding = request.getCharacterEncoding();
if (charEncoding == null) {
charEncoding = "UTF-8";
}
return new String(buffer, charEncoding);
}
}
https://blog.csdn.net/mccand1234/article/details/78013697
https://blog.csdn.net/java0311/article/details/78028702
https://blog.csdn.net/alan_waker/article/details/79477370