import com.alibaba.fastjson.JSONObject; import java.io.Serializable;
import java.util.Map; public class ResultEntity implements Serializable { JSONObject jsonObject; public JSONObject getJsonObject() {
return jsonObject;
} public void setJsonObject(JSONObject jsonObject) {
this.jsonObject = jsonObject;
} public ResultEntity() {
this.jsonObject = new JSONObject();
} public ResultEntity(JSONObject object) {
this.jsonObject = object;
} public ResultEntity(int code, String message) {
this();
this.jsonObject.put("code", code);
this.jsonObject.put("message", message);
} public ResultEntity(int code, String message, String description) {
this();
this.jsonObject.put("code", code);
this.jsonObject.put("message", message);
if (description != null) {
this.jsonObject.put("description", description);
}
} public ResultEntity put(String key, Object value) {
this.jsonObject.put(key, value);
return this;
} public ResultEntity put(Map<String, Object> map) {
for (String key : map.keySet()) {
this.jsonObject.put(key, map.get(key));
}
return this;
} public ResultEntity putNest(String key, Object value) {
if (!jsonObject.containsKey("data")) {
JSONObject dataObj = new JSONObject();
jsonObject.put("data", dataObj);
}
this.jsonObject.getJSONObject("data").put(key, value);
return this;
} public ResultEntity putNest(Map<String, Object> map) {
if (!jsonObject.containsKey("data")) {
JSONObject dataObj = new JSONObject();
jsonObject.put("data", dataObj);
}
for (String key : map.keySet()) {
this.jsonObject.getJSONObject("data").put(key, map.get(key));
}
return this;
} public String toJsonString() {
return this.jsonObject.toString();
} public Map<String, Object> totMap() {
return this.jsonObject;
}
}

调用示例:

方式一:

return new ResultEntity(0,"保存成功").putNest("name","张三").putNest("sex","男");

方式二:

Map<String,Object> map = new HashMap<String,Object>();
map.putNest("name","张三");
map.putNest("sex","男"); return new ResultEntity(0,"保存成功").putNest(map);

结果:

{
"code":0,
"message":"保存成功",
"data":{
"name":"张三",
"sex":"男"
}
}

restful返回 json数据的JavaBean设计01的更多相关文章

  1. Spring MVC 4.1.4 RESTFUL风格返回JSON数据406错误处理

    Spring MVC 4.1.4 RESTFUL风格返回JSON数据406错误处理 今天在使用spring4.1.4,使用ResponseBody注解返回JSON格式的数据的时候遇到406错误. 解决 ...

  2. J2EE Web开发入门—通过action是以传统方式返回JSON数据

    关键字:maven.m2eclipse.JSON.Struts2.Log4j2.tomcat.jdk7.Config Browser Plugin Created by Bob 20131031 l ...

  3. Query通过Ajax向PHP服务端发送请求并返回JSON数据

    Query通过Ajax向PHP服务端发送请求并返回JSON数据 服务端PHP读取MYSQL数据,并转换成JSON数据,传递给前端Javascript,并操作JSON数据.本文将通过实例演示了jQuer ...

  4. spingmvc 返回json数据日期格式化方法

    第一种: json 用的是这个依赖 <!-- JSON lib 开发包 以及它的依赖包 --> <dependency> <groupId>com.fasterxm ...

  5. jQuery通过Ajax向PHP服务端发送请求并返回JSON数据

    SON(JavaScript Object Notation) 是一种轻量级的数据交换格式.易于人阅读和编写,同时也易于机器解析和生成.JSON在前后台交互的过程中发挥着相当出色的作用.请接着往下看教 ...

  6. 2. Spring Boot返回json数据【从零开始学Spring Boot】

    在做如下操作之前,我们对之前的Hello进行简单的修改,我们新建一个包com.kfit.test.web然后新建一个类HelloControoler,然后修改App.Java类,主要是的这个类就是一个 ...

  7. (2)Spring Boot返回json数据【从零开始学Spring Boot】

    在做如下操作之前,我们对之前的Hello进行简单的修改,我们新建一个包com.kfit.test.web 然后新建一个类HelloControoler, 然后修改App.java类,主要是的这个类就是 ...

  8. SpringMVC返回JSON数据时日期格式化问题

    https://dannywei.iteye.com/blog/2022929 SpringMVC返回JSON数据时日期格式化问题 博客分类: Spring   在运用SpringMVC框架开发时,可 ...

  9. Web API返回JSON数据

    对Web API新手来说,不要忽略了ApiController 在web API中,方法的返回值如果是实体的话实际上是自动返回JSON数据的例如: 他的返回值就是这样的: { "Conten ...

随机推荐

  1. python,java操作mysql数据库,数据引擎设置为myisam时能够插入数据,转为innodb时无法插入数据

    今天想给数据库换一个数据引擎,mysiam转为 innodb 结果 python 插入数据时失败,但是自增id值是存在的, 换回mysiam后,又可以插入了~~ 想换php插入试试,结果php数据引擎 ...

  2. 《DSP using MATLAB》示例Example 6.26

    代码: % r = 0.9; theta = (pi/180)*[-55:5:-35, 35:5:55]'; p = r*exp(j*theta); a = poly(p); b = 1; % Dir ...

  3. 百度地图API秘钥生成步骤

    百度API

  4. Windows下安装Redis服务,修改查看密码,修改端口,常用命令

    一.安装 出自:https://jingyan.baidu.com/article/0f5fb099045b056d8334ea97.html 1.要安装Redis,首先要获取安装包.Windows的 ...

  5. java 乐观锁CAS

    乐观锁是一种思想,本身代码里并没有lock或synchronized关键字进行修饰.而是采用一种version. 即先从数据库中查询一条记录得到version值,在更新这条记录时在where条件中对这 ...

  6. jdk、jre、JVM的简单区别与联系

    2015-10-20 23:08:52 (1)jdk Java development toolkit(开发工具包),JDK是整个JAVA的核心,包括了Java运行环境jre(Java Runtime ...

  7. 【转】理解Linux 配置文件

    原文网址:http://www.mike.org.cn/articles/understanding-linux-configuration-files-linux/ 介绍每个 Linux 程序都是一 ...

  8. python自动发送邮件

    Python 的 smtplib 模块提供了发送电子邮件的功能.测试报告出来后,然后就把报告发送到邮箱. 一.先来看简单的列子 使用QQ邮箱发送邮件,使用的是授权码,需要先到QQ邮箱申请授权码. 邮箱 ...

  9. configure: error: jpeglib.h not found.

    编译出现错误: configure: error: jpeglib.h not found. 解决方法:yum install libjpeg libjpeg-devel -y libjpeg-dev ...

  10. json对象转换

    String modules =...; //json格式的String对象 //String对象转换为JSON格式数组 JSONArray moduleArr=JSONObject.parseArr ...