Springboot中如何自动转JSON输出


Posted in Java/Android onJune 16, 2022

Springboot是如此的暴力

在Controller中注解为@RestController类、@GetMapping或其他Mapping注解方法后

会自动转成JSON格式…

后台打印

{msg=已查询到163条数据, data_size=163, code=0, data=[Tersga{aname='万家铺村', sum=10, run=10, down=0, req=10}, Tersga{aname='万年桥', sum=4, run=4, down=0, req=4}, Tersga{aname='万龙村', sum=16, run=16, down=0, req=16}, Tersga{aname='三合村', sum=19, run=19, down=0, req=19}, Tersga{aname='上华山村', sum=6, run=6, down=0, req=6}, Tersga{aname='上杉市', sum=358, run=358, down=0, req=358}, Tersga{aname='东塘村', sum=4, run=4, down=0, req=4}, Tersga{aname='东山村', sum=17, run=17, down=0, req=17}, Tersga{aname='九木村', sum=4, run=4, down=0, req=4}, Tersga{aname='九溪源村', sum=15, run=15, down=0, req=15}, Tersga{aname='九田村', sum=3, run=3, down=0, req=3}, Tersga{aname='五福村', sum=8, run=8, down=0, req=8}, Tersga{aname='五美社区', sum=1, run=1, down=0, req=1}, Tersga{aname='五龙山村', sum=13, run=13, down=0, req=13}, Tersga{aname='仙人市村', sum=13, run=13, down=0, req=13}, Tersga{aname='光达村', sum=15, run=15, down=0, req=15}, Tersga{aname='农裕村', sum=10, run=10, down=0, req=10}, Tersga{aname='凤山村', sum=2, run=2, down=0, req=2}, Tersga{aname='北山村', sum=6, run=6, down=0, req=6}, Tersga{aname='双冲村', sum=14, run=14, down=0, req=14}, Tersga{aname='双江村', sum=13, run=13, down=0, req=13}, Tersga{aname='双起村', sum=4, run=4, down=0, req=4}, Tersga{aname='双龙村', sum=1, run=1, down=0, req=1}, Tersga{aname='古井社区', sum=24, run=22, down=2, req=24}, Tersga{aname='古华山村', sum=3, run=3, down=0, req=3}, Tersga{aname='和平村', sum=15, run=15, down=0, req=15}, Tersga{aname='唐田村', sum=32, run=32, down=0, req=32}, Tersga{aname='回龙村', sum=4, run=4, down=0, req=4}, Tersga{aname='团山村', sum=13, run=13, down=0, req=13}, Tersga{aname='大众村', sum=12, run=12, down=0, req=12}, Tersga{aname='大兴村', sum=5, run=5, down=0, req=5}, Tersga{aname='大桥村', sum=10, run=10, down=0, req=10}, Tersga{aname='大鱼塘村', sum=6, run=6, down=0, req=6}, Tersga{aname='天华村', sum=2, run=2, down=0, req=2}, Tersga{aname='太兴村', sum=19, run=19, down=0, req=19}, Tersga{aname='太平村', sum=12, run=12, down=0, req=12}, Tersga{aname='孙家桥村', sum=9, run=9, down=0, req=9}, Tersga{aname='安沙社区', sum=15, run=15, down=0, req=15}, Tersga{aname='安沙调试', sum=5, run=5, down=0, req=5}, Tersga{aname='宋家桥村', sum=55, run=55, down=0, req=55}, Tersga{aname='官塘村', sum=8, run=8, down=0, req=8}, Tersga{aname='官桥村', sum=20, run=20, down=0, req=20}, Tersga{aname='山笔村', sum=11, run=11, down=0, req=11}, Tersga{aname='崩坎村', sum=3, run=3, down=0, req=3}, Tersga{aname='常乐村', sum=135, run=135, down=0, req=135}, Tersga{aname='干杉社区', sum=12, run=12, down=0, req=12}, Tersga{aname='广福村', sum=3, run=3, down=0, req=3}, Tersga{aname='开慧村', sum=8, run=8, down=0, req=8}, Tersga{aname='影珠山村', sum=7, run=7, down=0, req=7}, Tersga{aname='惠农村', sum=12, run=12, down=0, req=12}, Tersga{aname='打卦岭村', sum=11, run=11, down=0, req=11}, Tersga{aname='拔茅田村', sum=13, run=13, down=0, req=13}, Tersga{aname='接驾岭社区', sum=6, run=6, down=0, req=6}, Tersga{aname='文家塅村', sum=10, run=10, down=0, req=10}, Tersga{aname='斗塘新村', sum=11, run=11, down=0, req=11}, Tersga{aname='新中村', sum=12, run=12, down=0, req=12}, Tersga{aname='新云村', sum=8, run=8, down=0, req=8}, Tersga{aname='新华村', sum=12, run=12, down=0, req=12}, Tersga{aname='新明村', sum=13, run=12, down=1, req=13}, Tersga{aname='新桥村', sum=7, run=7, down=0, req=7}, Tersga{aname='新桥社区', sum=4, run=4, down=0, req=4}, Tersga{aname='新沙村', sum=12, run=12, down=0, req=12}, Tersga{aname='明月', sum=3, run=3, down=0, req=3}, Tersga{aname='明月村', sum=79, run=79, down=0, req=79}, Tersga{aname='春华山村', sum=7, run=7, down=0, req=7}, Tersga{aname='李家山社区', sum=3, run=3, down=0, req=3}, Tersga{aname='杨四庙社区', sum=15, run=13, down=2, req=15}, Tersga{aname='杨梓冲村', sum=15, run=15, down=0, req=15}, Tersga{aname='松元村', sum=2, run=2, down=0, req=2}, Tersga{aname='枫林市村', sum=3, run=3, down=0, req=3}, Tersga{aname='梁坪村', sum=1, run=1, down=0, req=1}, Tersga{aname='梅塘村', sum=12, run=12, down=0, req=12}, Tersga{aname='梅花社区', sum=2, run=2, down=0, req=2}, Tersga{aname='武塘村', sum=5, run=5, down=0, req=5}, Tersga{aname='毛塘社区', sum=17, run=17, down=0, req=17}, Tersga{aname='水塘垸村', sum=21, run=21, down=0, req=21}, Tersga{aname='沙田村', sum=15, run=15, down=0, req=15}, Tersga{aname='油铺村', sum=11, run=11, down=0, req=11}, Tersga{aname='沿江山村', sum=13, run=13, down=0, req=13}, Tersga{aname='泗洲村', sum=14, run=14, down=0, req=14}, Tersga{aname='洞田村', sum=2, run=2, down=0, req=2}, Tersga{aname='洪河村', sum=3, run=3, down=0, req=3}, Tersga{aname='测试一', sum=11, run=11, down=0, req=11}, Tersga{aname='浔龙河村', sum=25, run=24, down=1, req=25}, Tersga{aname='涧山村', sum=12, run=12, down=0, req=12}, Tersga{aname='湘阴港村', sum=1, run=1, down=0, req=1}, Tersga{aname='牌楼村', sum=155, run=155, down=0, req=155}, Tersga{aname='特立村', sum=1, run=1, down=0, req=1}, Tersga{aname='狮山村', sum=20, run=20, down=0, req=20}, Tersga{aname='王公桥村', sum=2, run=1, down=1, req=2}, Tersga{aname='王梓园村', sum=15, run=15, down=0, req=15}, Tersga{aname='田汉村', sum=26, run=26, down=0, req=26}, Tersga{aname='白塔村', sum=12, run=12, down=0, req=12}, Tersga{aname='白沙村', sum=8, run=8, down=0, req=8}, Tersga{aname='白石源村', sum=3, run=3, down=0, req=3}, Tersga{aname='百录村', sum=9, run=9, down=0, req=9}, Tersga{aname='石井村', sum=12, run=12, down=0, req=12}, Tersga{aname='石塘铺村', sum=3, run=3, down=0, req=3}, Tersga{aname='石弓湾社区', sum=13, run=13, down=0, req=13}, Tersga{aname='石板村', sum=10, run=10, down=0, req=10}, Tersga{aname='石燕村', sum=2, run=2, down=0, req=2}, Tersga{aname='砖田新村', sum=1, run=1, down=0, req=1}, Tersga{aname='福临铺社区', sum=3, run=3, down=0, req=3}, Tersga{aname='福安村', sum=264, run=264, down=0, req=264}, Tersga{aname='福田村', sum=12, run=12, down=0, req=12}, Tersga{aname='福高村', sum=6, run=6, down=0, req=6}, Tersga{aname='维汉村', sum=4, run=4, down=0, req=4}, Tersga{aname='罗代社区', sum=3, run=3, down=0, req=3}, Tersga{aname='肖排村', sum=1, run=1, down=0, req=1}, Tersga{aname='脱甲村', sum=22, run=22, down=0, req=22}, Tersga{aname='脱甲社区', sum=10, run=10, down=0, req=10}, Tersga{aname='花园新村', sum=5, run=5, down=0, req=5}, Tersga{aname='花果村', sum=12, run=12, down=0, req=12}, Tersga{aname='花桥村', sum=11, run=11, down=0, req=11}, Tersga{aname='花桥湾村', sum=109, run=109, down=0, req=109}, Tersga{aname='范林村', sum=5, run=5, down=0, req=5}, Tersga{aname='荆华', sum=214, run=214, down=0, req=214}, Tersga{aname='荣合桥社区', sum=473, run=473, down=0, req=473}, Tersga{aname='荣河村', sum=15, run=15, down=0, req=15}, Tersga{aname='葛家山村', sum=7, run=7, down=0, req=7}, Tersga{aname='蒲塘村', sum=18, run=18, down=0, req=18}, Tersga{aname='蒿塘社区', sum=2, run=2, down=0, req=2}, Tersga{aname='蓝田新村', sum=17, run=17, down=0, req=17}, Tersga{aname='西山村', sum=16, run=16, down=0, req=16}, Tersga{aname='西湖村', sum=4, run=4, down=0, req=4}, Tersga{aname='观佳村', sum=8, run=8, down=0, req=8}, Tersga{aname='谭坊新村', sum=11, run=11, down=0, req=11}, Tersga{aname='谷塘村', sum=4, run=4, down=0, req=4}, Tersga{aname='赛头村', sum=4, run=4, down=0, req=4}, Tersga{aname='赤马村', sum=12, run=12, down=0, req=12}, Tersga{aname='路口社区', sum=9, run=9, down=0, req=9}, Tersga{aname='车马村', sum=15, run=15, down=0, req=15}, Tersga{aname='金井社区', sum=10, run=10, down=0, req=10}, Tersga{aname='金凤村', sum=13, run=13, down=0, req=13}, Tersga{aname='金星村', sum=6, run=6, down=0, req=6}, Tersga{aname='金桥村', sum=3, run=3, down=0, req=3}, Tersga{aname='金江新村', sum=18, run=17, down=1, req=18}, Tersga{aname='金牛村', sum=6, run=6, down=0, req=6}, Tersga{aname='金鼎山村', sum=6, run=6, down=0, req=6}, Tersga{aname='金龙村', sum=10, run=10, down=0, req=10}, Tersga{aname='银龙村', sum=2, run=2, down=0, req=2}, Tersga{aname='锡福村', sum=6, run=6, down=0, req=6}, Tersga{aname='锦绣社区', sum=10, run=10, down=0, req=10}, Tersga{aname='长安村', sum=20, run=20, down=0, req=20}, Tersga{aname='长春', sum=2, run=2, down=0, req=2}, Tersga{aname='青山村', sum=11, run=11, down=0, req=11}, Tersga{aname='青山铺社区', sum=3, run=3, down=0, req=3}, Tersga{aname='青田村', sum=5, run=5, down=0, req=5}, Tersga{aname='飘峰山村', sum=4, run=4, down=0, req=4}, Tersga{aname='高仓村', sum=9, run=9, down=0, req=9}, Tersga{aname='高塘村', sum=16, run=16, down=0, req=16}, Tersga{aname='高岸村', sum=1, run=1, down=0, req=1}, Tersga{aname='鱼塘村', sum=5, run=5, down=0, req=5}, Tersga{aname='鹿芝岭村', sum=13, run=13, down=0, req=13}, Tersga{aname='麻林', sum=322, run=322, down=0, req=322}, Tersga{aname='黄兴新村', sum=13, run=13, down=0, req=13}, Tersga{aname='黄桥村', sum=11, run=11, down=0, req=11}, Tersga{aname='鼎功桥村', sum=13, run=13, down=0, req=13}, Tersga{aname='龙华新村', sum=12, run=12, down=0, req=12}, Tersga{aname='龙华村', sum=9, run=9, down=0, req=9}, Tersga{aname='龙泉村', sum=12, run=12, down=0, req=12}, Tersga{aname='龙泉社区', sum=610, run=610, down=0, req=610}, Tersga{aname='龙王庙村', sum=8, run=8, down=0, req=8}], time=2019-02-13 01:53:27}

POSTMAN进行get获取的结果

{
    "msg": "已查询到163条数据",
    "data_size": 163,
    "code": 0,
    "data": [
        {
            "aname": "万家铺村",
            "sum": 10,
            "run": 10,
            "down": 0,
            "req": 10
        },
        {
            "aname": "万年桥",
            "sum": 4,
            "run": 4,
            "down": 0,
            "req": 4
        },
        {
            "aname": "万龙村",
            "sum": 16,
            "run": 16,
            "down": 0,
            "req": 16
        },
        {
            "aname": "三合村",
            "sum": 19,
            "run": 19,
            "down": 0,
            "req": 19
        },
        {
            "aname": "上华山村",
            "sum": 6,
            "run": 6,
            "down": 0,
            "req": 6
        }
    ],
    "time": "2019-02-13 01:55:13"
}

原因

因为@RestController是组合注解

等于SpringMVC:@Controller+@ResponstBody

package org.springframework.web.bind.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController {
    String value() default "

Springboot自动转json的出现问题

问题复现

上代码:

1、实体类

package com.example.demo;
 
/**
 * @author Mr.xiong
 * #description model
 * #date: 2022/4/8 10:08
 * @version v1.0.0
 * @since jdk8
 */
public class model {
    private String a;
    private String b;
    private boolean e;
    private Boolean f;
 
    public Boolean getF() {
        return false;
    }
 
    public boolean isE() {
        return false;
    }
 
    public boolean areC(){
        return false;
    }
 
    public Boolean isD(){
        return null;
    }
 
    public String getA() {
        return a;
    }
 
    public void setA(String a) {
        this.a = a;
    }
 
    public String getB() {
        return b;
    }
 
    public void setB(String b) {
        this.b = b;
    }
}

2、controller类

package com.example.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
 
/**
 * @author Mr.xiong
 * #description web
 * #date: 2022/4/8 10:08
 * @version v1.0.0
 * @since jdk8
 */
@RestController
public class web {
    @GetMapping
    public R demo(){
        model m1 = new model();
        m1.setA("a1");
        m1.setB("b1");
        model m2 = new model();
        m2.setA("a2");
        m2.setB("b2");
        List<model> models = new ArrayList<>();
        models.add(m1);
        models.add(m2);
        return R.success(200,"success",models);
    }
}

3、统一返回风格消息类

package com.example.demo; 
import java.util.Objects;
 
/**
 * @author Mr.xiong
 * #description R
 * #date: 2022/4/8 10:08
 * @version v1.0.0
 * @since jdk8
 */
public class R {
    private Integer code;
    private String msg;
    private Object obj; 
    public R(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }
 
    public R(Integer code, String msg, Object obj) {
        this.code = code;
        this.msg = msg;
        this.obj = obj;
    }
 
    public Integer getCode() {
        return code;
    }
 
    public void setCode(Integer code) {
        this.code = code;
    }
 
    public String getMsg() {
        return msg;
    }
 
    public void setMsg(String msg) {
        this.msg = msg;
    }
 
    public Object getObj() {
        return obj;
    }
 
    public void setObj(Object obj) {
        this.obj = obj;
    }
 
    @Override
    public String toString() {
        return "R{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", obj=" + obj +
                '}';
    }
 
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        R r = (R) o;
        return code.equals(r.code) && msg.equals(r.msg) && obj.equals(r.obj);
    }
 
    @Override
    public int hashCode() {
        return Objects.hash(code, msg, obj);
    }
 
    public static R success(Integer code,String msg){
        return new R(code, msg);
    }
 
    public static R success(Integer code,String msg,Object obj){
        return new R(code, msg,obj);
    }
}

访问接口获得的数据如下:

Springboot中如何自动转JSON输出

 问题分析及解决办法

public Boolean isD(){ return null; }

可以看到,我们有这个方法,但是没有D这个属性,但是却实实在在打印了这个值。原因是因为springboot自动转json把他当做了一个属性。这是为什么呢?是因为布尔值的getter事实上boolean的getter方法就是 isXxxx,因此出现此问题也就不奇怪了解决方法,不要使用isXxx就可以避免此类问题。当我把isD修改为D之后,就解决了。

如下:

Springboot中如何自动转JSON输出

以上为个人经验,希望能给大家一个参考,也希望大家多多支持三水点靠木。


Tags in this post...

Java/Android 相关文章推荐
详解Java实践之适配器模式
Jun 18 Java/Android
JVM钩子函数的使用场景详解
Aug 23 Java/Android
使用jpa之动态插入与修改(重写save)
Nov 23 Java/Android
Java中Quartz高可用定时任务快速入门
Apr 03 Java/Android
SpringCloud Function SpEL注入漏洞分析及环境搭建
Apr 08 Java/Android
java中为什么说子类的构造方法默认访问的是父类的无参构造方法
Apr 13 Java/Android
Java8 Stream API 提供了一种高效且易于使用的处理数据的方式
Apr 13 Java/Android
详解Flutter网络请求Dio库的使用及封装
Apr 14 Java/Android
Java Spring Boot 正确读取配置文件中的属性的值
Apr 20 Java/Android
SpringBoot全局异常处理方案分享
May 25 Java/Android
Ubuntu18.04下QT开发Android无法连接设备问题解决实现
Jun 01 Java/Android
Spring Boot优化后启动速度快到飞起技巧示例
Jul 23 Java/Android
springboot为异步任务规划自定义线程池的实现
详解Flutter自定义应用程序内键盘的实现方法
ConditionalOnProperty配置swagger不生效问题及解决
Jun 14 #Java/Android
Java异常体系非正常停止和分类
Android开发手册TextInputLayout样式使用示例
Jun 10 #Java/Android
Java实现简单小画板
Android开发EditText禁止输入监听及InputFilter字符过滤
Jun 10 #Java/Android
You might like
php checkdate、getdate等日期时间函数操作详解
2010/03/11 PHP
完美解决令人抓狂的zend studio 7代码提示(content Assist)速度慢的问题
2013/06/20 PHP
ThinkPHP使用心得分享-ThinkPHP + Ajax 实现2级联动下拉菜单
2014/05/15 PHP
ThinkPHP使用smarty模板引擎的方法
2014/07/01 PHP
php中数据库连接方式pdo和mysqli对比分析
2015/02/25 PHP
php数据序列化测试实例详解
2017/08/12 PHP
新浪的图片新闻效果
2007/01/13 Javascript
js 判断浏览器类型 去全角、半角空格 自动关闭当前窗口
2009/04/10 Javascript
input的focus方法使用
2010/03/13 Javascript
myFocus slide3D v1.1.0 使用方法与下载
2011/01/12 Javascript
基于jQuery的烟花效果(运动相关)点击屏幕出烟花
2012/06/14 Javascript
简易js代码实现计算器操作
2013/04/15 Javascript
Jil,高效的json序列化和反序列化库
2017/02/15 Javascript
jQuery实现文档树效果
2017/02/20 Javascript
jQuery控制元素隐藏和显示
2017/03/03 Javascript
JS解析url查询参数的简单代码
2017/08/06 Javascript
Nodejs 复制文件/文件夹的方法
2017/08/24 NodeJs
使用clipboard.js实现复制功能的示例代码
2017/10/16 Javascript
原生js+cookie实现购物车功能的方法分析
2017/12/21 Javascript
vue+springboot前后端分离实现单点登录跨域问题解决方法
2018/01/30 Javascript
Vue项目如何引入bootstrap、elementUI、echarts
2020/11/26 Vue.js
python生成器generator用法实例分析
2015/06/04 Python
Python时间的精准正则匹配方法分析
2017/08/17 Python
基于Python __dict__与dir()的区别详解
2017/10/30 Python
Django中自定义查询对象的具体使用
2019/10/13 Python
Django Form设置文本框为readonly操作
2020/07/03 Python
CSS3教程(10):CSS3 HSL声明设置颜色
2009/04/02 HTML / CSS
西班牙汉普顿小姐:购买帆布鞋和太阳镜
2016/10/23 全球购物
高三英语教学反思
2014/01/13 职场文书
出国留学介绍信
2014/01/13 职场文书
班主任对学生的评语
2014/04/26 职场文书
个人委托书范文
2015/01/28 职场文书
2015年护士长个人工作总结
2015/04/24 职场文书
2015年党员公开承诺事项
2015/04/27 职场文书
承诺书应该怎么写?
2019/09/10 职场文书
Python合并多张图片成PDF
2021/06/09 Python