返回顶部
首页 > 资讯 > 后端开发 > Python >Springboot中如何自动转JSON输出
  • 545
分享到

Springboot中如何自动转JSON输出

2024-04-02 19:04:59 545人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

目录SpringBoot是如此的暴力原因springboot自动转JSON的出现问题问题复现 问题分析及解决办法Springboot是如此的暴力 在Controller中注

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;
 

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;
 

@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;
 

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);
    }
}

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

 问题分析及解决办法

public Boolean isD(){ return null; }

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

如下:

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: Springboot中如何自动转JSON输出

本文链接: https://lsjlt.com/news/151684.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
  • Springboot中如何自动转JSON输出
    目录Springboot是如此的暴力原因Springboot自动转json的出现问题问题复现 问题分析及解决办法Springboot是如此的暴力 在Controller中注...
    99+
    2024-04-02
  • php如何输出json对象
    在PHP中,可以使用`json_encode()`函数将PHP对象转换为JSON字符串,然后使用`echo`语句输出。下面是一个示例...
    99+
    2023-08-24
    php json
  • 如何在Linux中格式化输出 JSON
    这篇文章将为大家详细讲解有关如何在Linux中格式化输出 JSON,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。 JSON 是一种轻量级且与语言无关的数据存储格式,易于与大多数编程...
    99+
    2023-06-09
  • python输出结果如何自动换行
    在Python中,可以使用换行符"\n"来实现输出结果的自动换行。可以通过在字符串中插入"\n"或者使用print函数来实现。例子1:在字符串中插入换行符```pythonprint("Hello\nWorld")```输出结果:```...
    99+
    2023-08-11
    python
  • SpringMVC @RequestBody自动转json Http415错误如何解决
    本篇内容介绍了“SpringMVC @RequestBody自动转json Http415错误如何解决”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望...
    99+
    2023-07-06
  • Docker中如何转化docker inspect输出
    小编给大家分享一下Docker中如何转化docker inspect输出,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!转化docker inspect输出&nbs...
    99+
    2023-06-27
  • rust format!如何转义{},输出{}?
    在Rust中,如果你想要在字符串中包含花括号 {} ,你需要使用双花括号 {{}} 来进行转义。这是因为单个花括号 {} 在字符串中表示占位符,用于格式化字符串。 以下是一个示例: fn main()...
    99+
    2023-09-08
    rust
  • springBoot启动输出三行日志控制台自动停止操作
    springBoot启动输出三行日志控制台自动停止 在https://start.spring.io/(官网)快速创建的springBoot工程,导入到myeclipse中后,启动后...
    99+
    2024-04-02
  • XML、XSL如何转换输出HTML
    这篇文章主要为大家展示了“XML、XSL如何转换输出HTML”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“XML、XSL如何转换输出HTML”这篇文章吧。Jav...
    99+
    2024-04-02
  • javascript中xml如何转json
    这篇文章主要介绍javascript中xml如何转json,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!JavaScript可以做什么1.可以使网页具有交互性,例如响应用户点击,给用户提供更好的体验。2.可以处理表单...
    99+
    2023-06-14
  • springboot项目中不输出nohup.out日志如何解决
    今天就跟大家聊聊有关springboot项目中不输出nohup.out日志如何解决,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。springboot不输出nohup.out日志noh...
    99+
    2023-06-20
  • PyCharm如何设置Console控制台输出自动换行
    目录pycharm设置Console控制台输出自动换行解决方法pycharm使用技巧之换行问题第一种方法第二种方法总结pycharm设置Console控制台输出自动换行 解决方法 F...
    99+
    2023-05-19
    PyCharm Console Console控制台输出 Console控制台自动换行
  • python如何将整数反转输出
    这篇文章主要介绍了python如何将整数反转输出,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。Python的优点有哪些1、简单易用,与C/C++、Java、C# 等传统语言相...
    99+
    2023-06-14
  • php如何输出跳转后的url
    本文小编为大家详细介绍“php如何输出跳转后的url”,内容详细,步骤清晰,细节处理妥当,希望这篇“php如何输出跳转后的url”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。php输出跳转后的url的方法:1、通...
    99+
    2023-07-05
  • 如何解决springboot整合cxf-jaxrs中json转换的问题
    本篇内容主要讲解“如何解决springboot整合cxf-jaxrs中json转换的问题”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何解决springboot整合cxf-jaxrs中json...
    99+
    2023-06-20
  • 如何使用自定义Json注解实现输出日志字段脱敏
    这篇文章给大家分享的是有关如何使用自定义Json注解实现输出日志字段脱敏的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。自定义Json注解实现输出日志字段脱敏背景在日志输出的时候,有时会输出一些用户的敏感信息,如手...
    99+
    2023-06-22
  • Golang如何将日志以Json格式输出到Kafka
    目录格式化接口普通文本格式化器Json文本格式化器写日志接口写日志到文件写日志到Kafka接口的组装如何提高日志处理的吞吐量在上一篇文章中我实现了一个支持Debug、Info、Err...
    99+
    2024-04-02
  • php中xml转json格式如何转换
    这篇文章主要介绍php中xml转json格式如何转换,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!php xml转json格式的方法:首先创建一个PHP示例文件;然后通过“function xmlToArray($x...
    99+
    2023-06-15
  • 如何使用shell实现转换输出
    这篇文章主要为大家展示了“如何使用shell实现转换输出”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“如何使用shell实现转换输出”这篇文章吧。一个简单的转换输出的shell脚本,从健盘输入 ...
    99+
    2023-06-09
  • javascript中如何输出Array
    javascript中如何输出Array?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。javascript的Array输出方式有:1、使用“for in”方法循...
    99+
    2023-06-15
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作