第十一章 处理JSON
演示返回 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;
    }
}

使用到了转换器




 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号