之前遇到一個問題,如果是get方法,
我可以很簡單的構(gòu)造強制鏈接結(jié)構(gòu)
/function/{userId}
或是參數(shù)結(jié)構(gòu)
/function?userId=123
@RequestMapping("/function")
@ResponseBody
public String function(@RequestParam("userId") String userId){
return "result";
}
然后就可以
get方法調(diào)試很簡單,在網(wǎng)頁中請求就完成了,如果是POST怎么辦?
本文將介紹一個使用java代碼發(fā)送POST請求的方式:
class A{
String a;
String b;
//省略getter,setter
...
}
@RequestMapping(value = "/functionPost", method = RequestMethod.POST)
@ResponseBody
public String functionPost(@RequestBody A a) {
return "result";
}
@ResponseBody
@Test
public void jsonPost() {
String strURL = "http://localhost:8080/newBsCard";
try {
URL url = new URL(strURL);// 創(chuàng)建連接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestMethod("POST"); // 設(shè)置請求方式
connection.setRequestProperty("Accept", "application/json"); // 設(shè)置接收數(shù)據(jù)的格式
connection.setRequestProperty("Content-Type", "application/json;charset=utf-8"); // 設(shè)置發(fā)送數(shù)據(jù)的格式
connection.connect();
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); // utf-8編碼
A bean = new A();
bean.setA("23");
bean.setB("23");
String a = JSONObject.toJSONString(bean);
out.append(a);
out.flush();
out.close();
int code = connection.getResponseCode();
InputStream is = null;
if (code == 200) {
is = connection.getInputStream();
} else {
is = connection.getErrorStream();
}
// 讀取響應(yīng)
int length = (int) connection.getContentLength();// 獲取長度
if (length != -1) {
byte[] data = new byte[length];
byte[] temp = new byte[512];
int readLen = 0;
int destPos = 0;
while ((readLen = is.read(temp)) > 0) {
System.arraycopy(temp, 0, data, destPos, readLen);
destPos += readLen;
}
String result = new String(data, "UTF-8"); // utf-8編碼
System.out.println(result);
//return result;
}
} catch (IOException e) {
//LOG.error("Exception occur when send http post request!", e);
}
}
上面的代碼,改自:
https://blog.csdn.net/java173842219/article/details/54020168
參考
https://blog.csdn.net/ff906317011/article/details/78552426
該文章簡單介紹了@RequestMapping @ResponseBody 和 @RequestBody 注解的用法與區(qū)別
參考
https://www.cnblogs.com/dflmg/p/6364141.html
該文章,使用ajax請求。不過里面說明了請求的時候,發(fā)送到服務(wù)端的內(nèi)容被解析的方式。
https://www.cnblogs.com/qiankun-site/p/5774300.html
參考
https://blog.csdn.net/ljxbbss/article/details/74452326
該文章用實例說明了400錯誤的情況,以及用postman進行測試的方式
400錯誤:表示數(shù)據(jù)的參數(shù)類型錯誤
https://www.cnblogs.com/beppezhang/p/5824986.html
415錯誤:表示格式錯誤,比如content-type 參數(shù)搞錯了。
https://blog.csdn.net/majinggogogo/article/details/78383772
500系統(tǒng)錯誤。https://blog.csdn.net/xiejunna/article/details/70049094
POSTMan的使用
https://blog.csdn.net/afterlife_qiye/article/details/62218572
參考
這個文章,對接受的參數(shù)類型聊得更深刻。
https://blog.csdn.net/m0_37499059/article/details/78798077
參考
使用mock測試的方式,沒有實踐過,記下
https://www.cnblogs.com/knyel/p/7901034.html
文章中給了三種傳入方式
https://blog.csdn.net/lx_yoyo/article/details/72871091
springboot的測試方式
http://somefuture.iteye.com/blog/2247207