Springboot中如何自动转JSON输出

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 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输出_第1张图片

 问题分析及解决办法

public Boolean isD(){ return null; }

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

如下:

Springboot中如何自动转JSON输出_第2张图片

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

你可能感兴趣的:(Springboot中如何自动转JSON输出)