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 相关文章推荐
SpringAop日志找不到方法的处理
Jun 21 Java/Android
Maven学习----Maven安装与环境变量配置教程
Jun 29 Java/Android
Jackson 反序列化时实现大小写不敏感设置
Jun 29 Java/Android
解决SpringBoot文件上传临时目录找不到的问题
Jul 01 Java/Android
Java 语言中Object 类和System 类详解
Jul 07 Java/Android
IDEA2021.2配置docker如何将springboot项目打成镜像一键发布部署
Sep 25 Java/Android
Jpa Specification如何实现and和or同时使用查询
Nov 23 Java/Android
SSM项目使用拦截器实现登录验证功能
Jan 22 Java/Android
springboot 自定义配置 解决Boolean属性不生效
Mar 18 Java/Android
SpringBoot中获取profile的方法详解
Apr 08 Java/Android
Java实现添加条码或二维码到Word文档
Jun 01 Java/Android
HttpClient实现文件上传功能
Aug 14 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中SESSION使用中的一点经验总结
2012/03/30 PHP
php+mysqli预处理技术实现添加、修改及删除多条数据的方法
2015/01/30 PHP
PHPCMS V9 添加二级导航的思路详解
2016/10/20 PHP
经典海量jQuery插件 大家可以收藏一下
2010/02/07 Javascript
也说JavaScript中String类的replace函数
2011/09/22 Javascript
node.js中的fs.appendFileSync方法使用说明
2014/12/17 Javascript
基于JavaScript创建动态Dom
2015/12/08 Javascript
玩转JavaScript OOP - 类的实现详解
2016/06/08 Javascript
Bootstrap中文本框的宽度变窄并且加入一副验证码图片的实现方法
2016/06/23 Javascript
JavaScript中定时控制Throttle、Debounce和Immediate详解
2016/11/17 Javascript
JavaScript实现定时页面跳转功能示例
2017/02/14 Javascript
javascript设计模式之单体模式学习笔记
2017/02/15 Javascript
基于iScroll实现下拉刷新和上滑加载效果
2017/07/18 Javascript
完美实现js拖拽效果 return false用法详解
2017/07/28 Javascript
解决JQuery全选/反选第二次失效的问题
2017/10/11 jQuery
echarts实现地图定时切换散点与多图表级联联动详解
2018/08/07 Javascript
微信小程序异步API为Promise简化异步编程的操作方法
2018/08/14 Javascript
微信小程序在线客服自动回复功能(基于node)
2019/07/03 Javascript
vue组件创建的三种方式小结
2020/02/03 Javascript
通过angular CDK实现页面元素拖放的步骤详解
2020/07/01 Javascript
不同版本中Python matplotlib.pyplot.draw()界面绘制异常问题的解决
2017/09/24 Python
python3+PyQt5使用数据库窗口视图
2018/04/24 Python
pandas 数据索引与选取的实现方法
2019/06/21 Python
python print出共轭复数的方法详解
2019/06/25 Python
python3 实现调用串口功能
2019/12/26 Python
python爬取音频下载的示例代码
2020/10/19 Python
Python实现壁纸下载与轮换
2020/10/19 Python
Skyscanner阿联酋:全球领先的旅游搜索平台
2017/11/25 全球购物
美国围栏公司:Walpole Outdoors
2019/11/19 全球购物
size?荷兰官方网站:英国高级运动鞋精品店
2020/07/24 全球购物
制定岗位职责的原则
2013/11/08 职场文书
信息技术教学反思
2014/02/12 职场文书
《蚕姑娘》教学反思
2014/04/15 职场文书
财政专业大学生职业生涯规划书
2014/09/17 职场文书
《平行四边形的面积》教学反思
2016/02/16 职场文书
Vue鼠标滚轮滚动切换路由效果的实现方法
2021/08/04 Vue.js