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 14 Java/Android
springboot集成flyway自动创表的详细配置
Jun 26 Java/Android
Springboot配置suffix指定mvc视图的后缀方法
Jul 03 Java/Android
java泛型通配符详解
Jul 25 Java/Android
浅谈Java父子类加载顺序
Aug 04 Java/Android
MyBatis自定义SQL拦截器示例详解
Oct 24 Java/Android
解析mybatis-plus中的resultMap简单使用
Nov 23 Java/Android
Java实现给Word文件添加文字水印
Feb 15 Java/Android
Java实现HTML转为Word的示例代码
Jun 28 Java/Android
SpringBoot项目多数据源及mybatis 驼峰失效的问题解决方法
Jul 07 Java/Android
springboot+rabbitmq实现智能家居实例详解
Jul 23 Java/Android
IDEA中sout快捷键无效问题的解决方法
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 增加了对 .ZIP 文件的读取功能
2006/10/09 PHP
php 定界符格式引起的错误
2011/05/24 PHP
php转换颜色为其反色的方法
2015/04/27 PHP
php实现点击可刷新验证码
2015/11/07 PHP
游览器中javascript的执行过程(图文)
2012/05/20 Javascript
AngularJS  $modal弹出框实例代码
2016/08/24 Javascript
Bootstrap 模态框实例插件案例分析
2016/12/28 Javascript
vue组件中的数据传递方法
2018/05/14 Javascript
微信小程序搭建自己的Https服务器
2019/05/02 Javascript
微信小程序实现商城倒计时
2020/11/01 Javascript
搭建一个nodejs脚手架的方法步骤
2019/06/28 NodeJs
vue el-table实现行内编辑功能
2019/12/11 Javascript
如何使用JavaScript实现无缝滚动自动播放轮播图效果
2020/08/20 Javascript
vue select 获取value和lable操作
2020/08/28 Javascript
Vue循环中多个input绑定指定v-model实例
2020/08/31 Javascript
讲解python参数和作用域的使用
2013/11/01 Python
Python使用xlrd读取Excel格式文件的方法
2015/03/10 Python
Python字符串详细介绍
2015/05/09 Python
Python基于Matplotlib库简单绘制折线图的方法示例
2017/08/14 Python
Python实现基于二叉树存储结构的堆排序算法示例
2017/12/08 Python
Python3中exp()函数用法分析
2019/02/19 Python
python在回调函数中获取返回值的方法
2019/02/22 Python
Python多继承以及MRO顺序的使用
2019/11/11 Python
使用python实现对元素的长截图功能
2019/11/14 Python
基于python判断目录或者文件代码实例
2019/11/29 Python
win10安装tesserocr配置 Python使用tesserocr识别字母数字验证码
2020/01/16 Python
Python实现病毒仿真器的方法示例(附demo)
2020/02/19 Python
Python解析m3u8拼接下载mp4视频文件的示例代码
2021/03/03 Python
爱他美官方海外旗舰店:Aptamil奶粉
2017/12/22 全球购物
HomeAway的巴西品牌:Alugue Temporada
2018/04/10 全球购物
如何写一个自定义标签
2012/12/28 面试题
公司拓展活动方案
2014/02/13 职场文书
坚定理想信念心得体会
2014/03/11 职场文书
2015年社区平安建设工作总结
2015/05/13 职场文书
小学英语教学反思范文
2016/02/15 职场文书
Python实现灰色关联分析与结果可视化的详细代码
2022/03/25 Python