码迷,mamicode.com
首页 > 编程语言 > 详细

java spring boot返回json的写法

时间:2020-04-21 14:51:26      阅读:61      评论:0      收藏:0      [点我收藏+]

标签:control   res   string   ajax   bind   ati   ash   demo   arraylist   

java spring boot返回json的写法 (用于写接口)

加了

@RestController 控制器的返回值就会返回json了

package com.example.demo2122;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
@RestController
public class HelloControl {
    @GetMapping("/hello")
    public Map<Object, Object> hello(@RequestParam(value = "name", defaultValue = "World") String name) {
        List<Map<Object, Object>> list = new ArrayList<Map<Object, Object>>();
        Map<Object, Object> map1 = new HashMap<Object, Object>();
        map1.put("姓名1", "张三");
        map1.put("姓名2", "李四");
        map1.put("姓名3", "王五");
        list.add(map1);

        Map<Object, Object> map=this.commonajax(100,"查询成功",list);


        return map;
    }

    private Map<Object, Object> commonajax(Integer code,String msg,List<Map<Object, Object>> data){
        Map<Object, Object> map = new HashMap<Object, Object>();
        map.put("code", code);
        map.put("msg", "查询成功");
        map.put("data", data);
        return map;
    }
}

 

java spring boot返回json的写法

标签:control   res   string   ajax   bind   ati   ash   demo   arraylist   

原文地址:https://www.cnblogs.com/newmiracle/p/12744486.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!