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

SpringBoot之返回json数据

时间:2019-01-08 12:19:13      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:bean   new   ade   block   frame   pass   web   list()   control   

一、创建一个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"}}

 

SpringBoot之返回json数据

标签:bean   new   ade   block   frame   pass   web   list()   control   

原文地址:https://www.cnblogs.com/loaderman/p/10237729.html

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