最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot之返回json數(shù)據(jù)的實現(xiàn)方法

 更新時間:2018年12月14日 10:11:00   作者:張昊亮  
這篇文章主要介紹了SpringBoot之返回json數(shù)據(jù)的實現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

一、創(chuàng)建一個springBoot個項目

操作詳情參考:1.SpringBoo之Helloword 快速搭建一個web項目

二、編寫實體類

/**
 * Created by CR7 on 2017-8-18 返回Json數(shù)據(jù)實體類
 */
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;

/**
 * Created by CR7 on 2017-8-18 Json返回數(shù)據(jù)的Controller
 */
@RestController
@RequestMapping("user")  
public class ReturnJsoncontroller {

  @RequestMapping("getUser")
  public User getUser(){
    User user = new User();
    user.setId(1);
    user.setUsername("zhanghaoliang");
    user.setPassword("1231");
    return user;
  }
}

四、測試返回Json數(shù)據(jù)

瀏覽器輸入http://localhost:8080/user/getUser

得出結(jié)果:服務(wù)器是以json數(shù)據(jù)格式返回給瀏覽器

五、返回list到頁面

5.1.返回數(shù)據(jù)的controller

package com.example.demo;

import com.example.bean.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by CR7 on 2017-8-18 Json返回數(shù)據(jù)的Controller
 */
@RestController
@RequestMapping("user")
public class ReturnJsoncontroller {
  
  @RequestMapping("getUserList")
  public List<User> getUserList(){
    User user1 = new User();
    user1.setId(1);
    user1.setUsername("zhanghaoliang");
    user1.setPassword("123");
    User user2 = new User();
    user2.setId(2);
    user2.setUsername("chensi");
    user2.setPassword("456");
    User user3 = new User();
    user3.setId(3);
    user3.setUsername("doudou");
    user3.setPassword("789");
    List<User> list = new ArrayList<>();
    list.add(user1);
    list.add(user2);
    list.add(user3);
    return list;
  }
}

5.2.得出結(jié)果

在瀏覽器訪問 http://localhost:8080/user/getUserList

六、返回map到瀏覽器

既然返回實體,和list的試驗過了,那么再試驗一下返回Map類型的數(shù)據(jù)吧

6.1返回的Controller

package com.example.demo;

import com.example.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;

/**
 * Created by CR7 on 2017-8-18 Json返回數(shù)據(jù)的Controller
 */
@RestController
@RequestMapping("user")
public class ReturnJsoncontroller {

  @RequestMapping("getUserMap")
  public Map<String,User> getUserMap(){
    User user1 = new User();
    user1.setId(1);
    user1.setUsername("zhanghaoliang");
    user1.setPassword("123");
    User user2 = new User();
    user2.setId(2);
    user2.setUsername("chensi");
    user2.setPassword("456");
    User user3 = new User();
    user3.setId(3);
    user3.setUsername("doudou");
    user3.setPassword("789");
    Map<String,User> map = new HashMap<>();
    map.put("user1",user1);
    map.put("user2",user2);
    map.put("user3",user3);
    return map;
  }
}

6.2得出的結(jié)果

在瀏覽器中訪問http://localhost:8080/user/getUserMap

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Mybatis使用注解實現(xiàn)復(fù)雜動態(tài)SQL的方法詳解

    Mybatis使用注解實現(xiàn)復(fù)雜動態(tài)SQL的方法詳解

    當(dāng)使用 MyBatis 注解方式執(zhí)行復(fù)雜 SQL 時,你可以使用 @Select、@Update、@Insert、@Delete 注解直接在接口方法上編寫 SQL,本文給大家介紹了Mybatis如何使用注解實現(xiàn)復(fù)雜動態(tài)SQL,文中有相關(guān)的代碼示例供大家參考,需要的朋友可以參考下
    2023-12-12
  • 解決@FeignClient注入service失敗問題

    解決@FeignClient注入service失敗問題

    這篇文章主要介紹了解決@FeignClient注入service失敗問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • 排序算法圖解之Java希爾排序

    排序算法圖解之Java希爾排序

    希爾排序是希爾(Donald?Shell)于1959年提出的一種排序算法,其也是一種特殊的插入排序,即將簡單的插入排序進(jìn)行改進(jìn)后的一個更加高效的版本,也稱縮小增量排序。本文通過圖片和示例講解了希爾排序的實現(xiàn),需要的可以了解一下
    2022-11-11
  • SpringCache之 @CachePut的使用

    SpringCache之 @CachePut的使用

    這篇文章主要介紹了SpringCache之 @CachePut的使用,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • Java-文件File簡單實用方法(分享)

    Java-文件File簡單實用方法(分享)

    下面小編就為大家?guī)硪黄狫ava-文件File簡單實用方法(分享)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • Deepseek整合SpringAI詳細(xì)流程

    Deepseek整合SpringAI詳細(xì)流程

    本文介紹了如何使用SpringBoot、Deepseek和SpringAI構(gòu)建一個簡單的問答系統(tǒng),并通過Postman調(diào)用API接口實現(xiàn)問答功能,通過本文,你將學(xué)習(xí)如何整合這些技術(shù),快速實現(xiàn)一個高效的問答系統(tǒng),感興趣的朋友一起看看吧
    2025-02-02
  • Spring?Security權(quán)限控制的實現(xiàn)接口

    Spring?Security權(quán)限控制的實現(xiàn)接口

    這篇文章主要介紹了Spring?Security的很多功能,在這些眾多功能中,我們知道其核心功能其實就是認(rèn)證+授權(quán)。Spring教程之Spring?Security的四種權(quán)限控制方式
    2023-03-03
  • SpringBoot項目中使用@Scheduled讀取動態(tài)參數(shù)

    SpringBoot項目中使用@Scheduled讀取動態(tài)參數(shù)

    這篇文章主要介紹了SpringBoot項目中使用@Scheduled讀取動態(tài)參數(shù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • SpringCloud全局過慮器GlobalFilter的用法小結(jié)

    SpringCloud全局過慮器GlobalFilter的用法小結(jié)

    這篇文章主要介紹了SpringCloud全局過慮器GlobalFilter的使用,全局過慮器使用非常廣泛,比如驗證是否登錄,全局性的處理,黑名單或白名單的校驗等,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2023-07-07
  • SpringBoot JVM參數(shù)調(diào)優(yōu)方式

    SpringBoot JVM參數(shù)調(diào)優(yōu)方式

    這篇文章主要介紹了SpringBoot JVM參數(shù)調(diào)優(yōu)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09

最新評論

通城县| 伊川县| 广丰县| 祁阳县| 赤水市| 志丹县| 营口市| 五河县| 宣威市| 平陆县| 宜川县| 温州市| 称多县| 博爱县| 深圳市| 凤山县| 昌邑市| 吉安市| 吉水县| 长岛县| 中江县| 农安县| 涡阳县| 明光市| 阜平县| 镇沅| 石狮市| 阿拉善左旗| 类乌齐县| 商南县| 宝鸡市| 咸宁市| 榆树市| 通渭县| 海安县| 庆安县| 延庆县| 寻甸| 灵丘县| 达拉特旗| 准格尔旗|