SpringMVC 将复杂对象以json格式返回前端
2017-07-04 20:35
387 查看
环境
SpringMVC 4.3.5Jackson 2.6.5
复杂对象描述
涵盖列表, 而列表里面的每一个也都是对象返回json数据如下:
{ "nodes": [ {"id": "Myriel", "group": 1}, {"id": "Napoleon", "group": 1}, {"id": "Mlle.Baptistine", "group": 1}, {"id": "Mme.Magloire", "group": 1}, {"id": "CountessdeLo", "group": 1}, {"id": "Geborand", "group": 1}, {"id": "Champtercier", "group": 1} ], "links": [ {"source": "Napoleon", "target": "Myriel", "value": 1}, {"source": "Mlle.Baptistine", "target": "Myriel", "value": 8}, {"source": "Mme.Magloire", "target": "Myriel", "value": 10}, {"source": "Mme.Magloire", "target": "Mlle.Baptistine", "value": 6} ] }
步骤
1. json中的nodes列表里的是的为GNode对象, links列表里的对象为GLink对象
[Tips] 对于对象里的每一个属性, 均需要编写getter方法, 否则会出现如下错误HTTP Status 500 - Could not write content: No serializer found for class com.iaso.antibiotic.json.GLink and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: java.util.HashMap[“links”]->java.util.ArrayList[0]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class com.iaso.antibiotic.json.GLink and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: java.util.HashMap[“links”]->java.util.ArrayList[0])
GNode.java
public class GNode{ /** * 节点类 * 涵盖节点基本信息: * @id:初步定为药物名称 * @infos:所涵盖信息因节点类型而定 * - Antibiotic(name, type, description, drug_indication) * - Bacteria(name, type, description) * - Situtation(name) * @group:决定节点颜色,不同类别应该有不同的颜色 */ private String id; private String infos; private int group; public GNode(String id, int group) { this.id = id; this.group = group; } public String getInfos() { return infos; } public void setInfos(String infos) { this.infos = infos; } public String getId() { return id; } public int getGroup() { return group; } }
GLink.java
public class GLink { /** * 连接线类 * 涵盖连线的基本信息: * @source:只有一个,即为查询的节点 * @target * @value=1 */ private String source; private String target; private int value; public GLink(String source, String target, int value) { this.source = source; this.target = target; this.value = value; } public GLink(String source, String target) { this.source = source; this.target = target; this.value = 1; } public String getSource() { return source; } public String getTarget() { return target; } public int getValue() { return value; } }
2. 设置前端数据请求格式
这里以提交表格为例, 注意这一句:datatype: "application/json
<form id="searchForm"> <input type="text" id="keywords" name="keywords" class="form-control input-lg" placeholder="搜索......" value="AmBisome"> <input type="button" class="btn btn-default btn-lg" onclick="clickButton()" value="search"> <input type="hidden" id="graph" name="graph" value="IASO"> </form> <script type="text/javascript"> function clickButton() { $.ajax({ type: "POST", url: "/search", data: $("#searchForm").serialize(), datatype: "application/json", error: function(error) { console.log(error.toString()); }, success: function(data){ console.log("Successfully accept."); console.log(data.toString()); }); }; </script>
3. Controller的编写
注意这一句@ResponseBody
@Controller public class SearchController { // 搜索栏:搜索领域graph,搜索关键词keywords @RequestMapping(name = "/search", method = RequestMethod.POST) @ResponseBody public HashMap<String, Object> testGraph(String keywords, String graph) { HashMap<String, Object> map = new HashMap<String, Object>(); GNode gNode1 = new GNode("One", 1); GNode gNode2 = new GNode("Two", 2); GNode gNode = new GNode("Source", 0); ArrayList<GNode> node_list = new ArrayList<GNode>(); node_list.add(gNode); node_list.add(gNode1); node_list.add(gNode2); map.put("nodes", node_list); GLink gLink1 = new GLink("Source", "One"); GLink gLink2 = new GLink("Source", "Two"); ArrayList<GLink> link_list = new ArrayList<GLink>(); link_list.add(gLink1); link_list.add(gLink2); map.put("links", link_list); return map; }
思考
其实按照本次博客的题目的话, 按道理返回的应该是把整个json格式的数据看成一个对象, 这里假设是Graph, Graph含有两个性质: nodes和 links, 前者的数据类型应该是ArrayList<GNode>, 后者的数据格式为
ArrayList<GLink>. 那么就真的是”将复杂对象转为json返回给前端了”.
添加Graph类
Graph.javapublic class Graph { private ArrayList<GNode> nodes = new ArrayList<GNode>(); private ArrayList<GLink> links = new ArrayList<GLink>(); private String keyword; private String Graph; public Graph(String keyword, String graph) { this.keyword = keyword; Graph = graph; } public Graph(String keyword) { this.keyword = keyword; } public Graph() { } public void setKeyword(String keyword) { this.keyword = keyword; } public void setGraph(String graph) { Graph = graph; } public ArrayList<GLink> getLinks() { return links; } public void setLinks(ArrayList<GLink> links) { this.links = links; } public String getKeyword() { return keyword; } public String getGraph() { return Graph; } public ArrayList<GNode> getNodes() { return nodes; } public void setNodes(ArrayList<GNode> nodes) { this.nodes = nodes; } }
修改Controller中的testGraph
注意此时的返回对象是Graph
// parse 复杂对象(内含ArrayList<对象>) into json, 但是这种做法不符合POJO原则(对象套对象) @RequestMapping(name = "/search", method = RequestMethod.POST) @ResponseBody public Graph testGraph(String keywords, String graph) { HashMap<String, Object> map = new HashMap<String, Object>(); Graph test_graph = new Graph(); GNode gNode1 = new GNode("One", 1); GNode gNode2 = new GNode("Two", 2); GNode gNode = new GNode("Source", 0); ArrayList<GNode> node_list = new ArrayList<GNode>(); node_list.add(gNode); node_list.add(gNode1); node_list.add(gNode2); test_graph.setNodes(node_list); GLink gLink1 = new GLink("Source", "One"); GLink gLink2 = new GLink("Source", "Two"); ArrayList<GLink> link_list = new ArrayList<GLink>(); link_list.add(gLink1); link_list.add(gLink2); test_graph.setLinks(link_list); return test_graph; }
正如我在注释中所言, 这么做是不符合POJO的设计原则哒~
所以还是推荐第一做法哈~
相关文章推荐
- springMVC返回复杂的json格式数据方法
- 后台对象,使用google json组件用json格式返回给前端的问题
- springMVC框架下返回json格式的对象,list,map
- 如何利用fastjson将JSON格式的字符串转换为Map,再返回至前端成为js对象
- springMVC传对象参数、返回JSON格式数据
- springMVC框架下返回json格式的对象,list,map
- Springmvc统一设置返回到前端的json时间格式
- SpringMVC3中复杂对象返回json出现 500 error的解决
- springMVC框架下返回json格式的对象,list,map
- SpringMVC 统一返回JSON格式数据到前端
- springMVC传对象参数、返回JSON格式数据
- 解决SpringMVC返回Json数据格式不严谨报异常的问题(草稿未完成)
- 面试题-前端-问答题-javascript中字符串格式json如何转化成json对象
- struts2 + ajax(从后台获取json格式的数据返回到前端,然后前端用jquery对json数据进行解析)
- SpringMVC返回Json,自定义Json中Date类型格式
- 用json格式返回自定义对象。(jQuery asp.net)
- WCF以Json格式返回对象,客户端以JS调用显示
- springMVC返回hibernate多对多对象的json时 出现无限循环
- js将json格式的对象拼接成复杂的url参数
- ajax处理请求,以JSON数据格式返回,(json时间日期返回时对象---》日期格式)