前面通過視頻已經大致瞭解了Spring的工作原理與數據的接收與傳送。
但視頻中的只講到了利用GET方式來接收數組,由於最近項目需求,數據量較多,可能會採用數組方式來進行輸出的傳遞,因此,前幾天開始研究這個,發現好坑,也沒發現比較不錯的方法。
到目前爲止沒有發現直接封裝的方式,先提出這樣一個解決方案。
/**
*
* desc:對數組的接收
*/
@RequestMapping("/toPerson3.do")
public String toPerson3(String[] name){
for(String result : name){
System.out.println(result);
}
return "jsp1/index";
}
測試方式多爲GET方式:
http://localhost:8080/Servlet_push.do?name=a&name=b
這樣spring的controller可以實現自動封裝。
首先來定義下數組:
網上的大多數的jason數組是這種格式的:
{id:"1",name:"cehshi1"},{id:"2",name:"ceshi2"}
後來發現這跟我的需求是不同的,仔細想想又有幾個需求會是這種格式的呢?
如果我們有person這個類,我們有好多個person實體,然後我們所想到的自然是封裝到一個List<Person>裏面,然後我們把這個list加入到map中,然後跟安卓或前端商量一下我們用什麼協議來查詢到這個數據呢?於是我們定義了一個key就假設是flag吧,那麼問題來了,網上的所說的數組與我們的結構是不同的!!!!
區別就是:
其實我想要的效果在不知不覺中封裝了兩次,因此spring的controller是無法直接進行兩次解析的,因此我需要自己手動解析,不過其實也很簡單,就是在spring中再定義一個類,這個類中的成員對象就是一個list<person>,那麼問題就解決了。
我的經歷:
我所做的測試就是:
Map<String, List> map = new HashMap<String, List>();
List<Integer> list = new ArrayList<Integer>();
for (int i = 1; i < 11; i++) {
list.add(i);
}
map.put("test", list);
String s = new Gson().toJson(map);
把這個轉成json發過去。接收方式就是:
@Controller
//父親頁面
@RequestMapping("/")
public class ArrayListController {
@RequestMapping(value = "/arrayListController.do", method = RequestMethod.POST/*,produces="application/json"*/)
public String arrayListController(@RequestBody Integer[] test) {
System.out.println(test[2]);
return "hello";
}
}
當然我也沒有一開始就想清楚,一開始還是按照網上的方法來處理,於是怎麼也得不到(這是肯定的)死活都是這個問題:
客戶端發過來的請求格式不對啊,那是肯定的,明明是一個對象,我卻要得到一個數組。(其實key是test,剩下的value是一個對象,這個對象沒有定義啊,後來纔想明白,並不是數組,不懂的同學可以再看看我最開始畫的圖)。
剛開始還沒想通,於是就想,那麼我得到這個json的全部字節自己解析,總不會有問題吧,於是代碼就改成了這樣:
@Controller
//父親頁面
@RequestMapping("/")
public class ArrayListController {
@RequestMapping(value = "/arrayListController.do", method = RequestMethod.POST/*,produces="application/json"*/)
public String arrayListController(@RequestBody String test) {
// System.out.println(test[2]);
// return "hello";
Integer[] weekDays = new Gson().fromJson(test, Integer[].class);
System.out.println(weekDays);
return "hello";
}
}
於是,就出現了下面的錯誤:
作爲一名英語差等生,根本讀不懂,但是機智的我果斷把錯誤提示敲到了百度上,於是:
那麼,上面的提示就很清晰了,你寫的代碼要得到的是數組,其實它是個對象,你這不是難爲老子嗎?
對的就是這個意思,那麼大家應該知道了,問題出在什麼地方也就很清楚了。那麼解決方案如下:
但是我們在實際使用中,大多是:
- 創建數組
- 加入map
- 利用Gson轉換成json包
- 發送出去^_^
使用postman:
傳送方式選擇:POST,數據格式選擇:JSON,然後在內容只鍵入json數據。
使用HttpURLConnection模擬:
package seu.xinci.servlet;
import com.google.gson.Gson;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by Administrator on 2015-12-27.
*/
@WebServlet(name = "Servlet_push")
public class Servlet_push extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Map<String, List> map = new HashMap<String, List>();
List<Integer> list = new ArrayList<Integer>();
for (int i = 1; i < 11; i++) {
list.add(i);
}
map.put("test", list);
String s = new Gson().toJson(map);
System.out.println(s);
//提交http請求給服務器
URL url = new URL("http://localhost:8080/arrayListController.do");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//寫請求方法
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-type", "application/json");
//寫header
// conn.setRequestProperty("test", "1");
//打開寫數據
conn.setDoOutput(true);
// conn.setUseCaches(false);
OutputStream out = conn.getOutputStream();
out.write(s.getBytes());
out.flush();
out.close();
conn.getResponseCode();
}
}
其中:
conn.setRequestProperty("Content-type", "application/json");
是一定要存在的。
解決方案:
首先,新建一個類,類裏面有一個對應類型的list數組,這裏拿Integer爲例:
package seu.xinci.pojo;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Administrator on 2015-12-30.
*/
public class Interger_u {
private List<Integer> test= new ArrayList<Integer>();
public List<Integer> getTest() {
return test;
}
public void setTest(List<Integer> test) {
this.test = test;
}
@Override
public String toString() {
return "Interger_u{" +
"test=" + test +
'}';
}
}
</pre><pre name="code" class="java">控制器:
<pre name="code" class="java">@Controller
//父親頁面
@RequestMapping("/")
public class ArrayListController {
@RequestMapping(value = "/arrayListController.do", method = RequestMethod.POST/*,produces="application/json"*/)
public String arrayListController(@RequestBody Interger_u test) {
System.out.println(test.getTest().get(4));
return "hello";
}
}
測試一發:這樣就可以了!!!
經過測試對於複雜對象也是可以的。