<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

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

          共 3260字,需瀏覽 7分鐘

           ·

          2020-07-31 21:14

          出處:jb51.net/article/152751.htm



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


          操作詳情參考:1.SpringBoo之Helloword 快速搭建一個(gè)web項(xiàng)目


          二、編寫實(shí)體類


          /**
          ?* Created by CR7 on 2017-8-18 返回Json數(shù)據(jù)實(shí)體類
          ?*/

          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類


          /**
          ?* Created by CR7 on 2017-8-18 返回Json數(shù)據(jù)實(shí)體類
          ?*/

          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;
          ??}
          }


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


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


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



          五、返回list到頁(yè)面


          5.1.返回?cái)?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返回?cái)?shù)據(jù)的Controller
          ?*/

          @RestController
          @RequestMapping("user")
          public?class?ReturnJsoncontroller?{
          ??
          ??@RequestMapping("getUserList")
          ??public?List 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 list?= new?ArrayList<>();
          ????list.add(user1);
          ????list.add(user2);
          ????list.add(user3);
          ????return?list;
          ??}
          }


          5.2.得出結(jié)果


          在瀏覽器訪問(wèn) http://localhost:8080/user/getUserList



          六、返回map到瀏覽器


          既然返回實(shí)體,和list的試驗(yàn)過(guò)了,那么再試驗(yàn)一下返回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返回?cái)?shù)據(jù)的Controller
          ?*/

          @RestController
          @RequestMapping("user")
          public?class?ReturnJsoncontroller?{

          ??@RequestMapping("getUserMap")
          ??public?Map 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 map?= new?HashMap<>();
          ????map.put("user1",user1);
          ????map.put("user2",user2);
          ????map.put("user3",user3);
          ????return?map;
          ??}
          }


          6.2得出的結(jié)果


          在瀏覽器中訪問(wèn)http://localhost:8080/user/getUserMap




          瀏覽 26
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产无套内精一级毛片三 | 国产字幕在线观看 | 国产区视频播放 | 区一区二亚洲无码四虎网站 | 在线看片肏 |