zoukankan      html  css  js  c++  java
  • SpringBoot之返回json数据

    一、创建一个springBoot个项目

    二、编写实体类

    /**
     * 返回Json数据实体类
     */
    public class User {
        private int id;
        private String username;
        private String password;
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    }

    三、编写控制层Controller类

    import com.example.bean.User;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     *Json返回数据的Controller
     */
    @RestController
    @RequestMapping("user")   
    public class ReturnJsoncontroller {
    
        @RequestMapping("getUser")
        public User getUser(){
            User user = new User();
            user.setId(1);
            user.setUsername("loaderman");
            user.setPassword("123123");
            return user;
        }
    }

    四、测试返回Json数据

    浏览器输入http://localhost:8080/user/getUser

    输出:

    {"id":1,"username":"loaderman","password":"123123"}

    返回listjson数据:

    控制类:

    package com.generalichina.cimc.controller;
    
    import com.generalichina.cimc.bean.User;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     *  Json返回数据的Controller
     */
    @RestController
    @RequestMapping("user")
    public class UserController {
    
      
        @RequestMapping("getUserList")
        public List<User> getUserList(){
            User user1  = new User();
            user1.setId(1);
            user1.setUsername("loaderman1");
            user1.setPassword("123");
            User user2 = new User();
            user2.setId(2);
            user2.setUsername("loaderman2");
            user2.setPassword("456");
            User user3 = new User();
            user3.setId(3);
            user3.setUsername("loaderman3");
            user3.setPassword("789");
            List<User> list = new ArrayList<>();
            list.add(user1);
            list.add(user2);
            list.add(user3);
            return list;
        }
    }

    测试

    在浏览器访问 http://localhost:8080/user/getUserList

    输出:

    [{"id":1,"username":"loaderman1","password":"123"},{"id":2,"username":"loaderman2","password":"456"},{"id":3,"username":"loaderman3","password":"789"}]

    返回map测试

    package com.generalichina.cimc.controller;
    
    import com.generalichina.cimc.bean.User;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    /**
     *  Json返回数据的Controller
     */
    @RestController
    @RequestMapping("user")
    public class UserController {
    
    
        @RequestMapping("getUserMap")
        public Map<String,User> getUserMap(){
            User user1  = new User();
            user1.setId(1);
            user1.setUsername("loaderman1");
            user1.setPassword("123");
            User user2 = new User();
            user2.setId(2);
            user2.setUsername("loaderman2");
            user2.setPassword("456");
            User user3 = new User();
            user3.setId(3);
            user3.setUsername("loaderman3");
            user3.setPassword("789");
            Map<String,User> map = new HashMap<>();
            map.put("user1",user1);
            map.put("user2",user2);
            map.put("user3",user3);
            return map;
        }
    }
    
    
     {"user1":{"id":1,"username":"loaderman1","password":"123"},"user2":{"id":2,"username":"loaderman2","password":"456"},"user3":{"id":3,"username":"loaderman3","password":"789"}}
    
    
    
    
    
  • 相关阅读:
    关于产品的一些想法
    js中this的问题
    C# webkit 内核浏览器 访问https 网站 提示 Problem with the SSL CA cert (path? access rights?)
    SSL握手中win xp和SNI的那点事
    VMware安装GHOST版XP不成功的解决
    Inno Setup入门(十二)——Pascal脚本(1)
    VS打包软件部署------ClickOnce应用安装 (各版本.net引导文件安装,再发布文档离线安装下载地址)
    利用VS自带的命令行工具查看和生产PublicKeyToken
    获取批处理文件所在路径
    Nginx Windows详细安装部署教程
  • 原文地址:https://www.cnblogs.com/loaderman/p/10237729.html
Copyright © 2011-2022 走看看