您的位置:首页 > 编程语言 > Java开发

SpringMVC 将复杂对象以json格式返回前端

2017-07-04 20:35 387 查看

环境

SpringMVC 4.3.5

Jackson 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.java

public 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的设计原则哒~

所以还是推荐第一做法哈~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring mvc 对象 json