首页 > 其他分享 >第十一章 处理JSON

第十一章 处理JSON

时间:2022-11-05 17:58:45浏览次数:75  
标签:Monster 处理 第十一章 lzw JSON springboot import monster

演示返回 JSON 格式数据

应用实例

SpringBoot 支持返回 JSON 格式数据,在启用 WEB 开发场景时,已经自动引入了相关依赖。


创建 src/main/java/com/lzw/springboot/controller/ResponseController.java

package com.lzw.springboot.controller;

import com.lzw.springboot.bean.Car;
import com.lzw.springboot.bean.Monster;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * @author LiAng
 */
@Controller
public class ResponseController {

    //返回Monster数据-要求以json格式返回
    @GetMapping("/get/monster")
    @ResponseBody
    public Monster getMonster() {
        //开发中, monster对象是从DB获取
        Monster monster = new Monster();
        monster.setId(100);
        monster.setName("奔波霸");
        monster.setAge(200);
        monster.setIsMarried(false);
        monster.setBirth(new Date());
        Car car = new Car();
        car.setName("奔驰");
        car.setPrice(222.2);
        monster.setCar(car);
        return monster;
    }

}

image-20220804101419746
使用到了转换器


标签:Monster,处理,第十一章,lzw,JSON,springboot,import,monster
From: https://www.cnblogs.com/liangnice/p/16860698.html

相关文章