前面通过视频已经大致了解了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";
}
}
测试一发:这样就可以了!!!
经过测试对于复杂对象也是可以的。