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"}}

 


posted on 2019-01-08 10:57  LoaderMan  阅读(4507)  评论(0编辑  收藏  举报

导航