SpringBoot 의 json 데이터 반환 실현 방법

1.springBoot 항목 만 들 기
조작 상세 참조:1.SpringBoo 의 Helloword 빠 른 웹 프로젝트 구축
2.실체 클래스 작성

/**
 * Created by CR7 on 2017-8-18   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;
  }
}
3.제어 층 컨트롤 러 클래스 작성

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     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;
  }
}
4.테스트 는 JSon 데 이 터 를 되 돌려 줍 니 다.
브 라 우 저 입력http://localhost:8080/user/getUser
결과:서버 는 json 데이터 형식 으로 브 라 우 저 에 되 돌려 줍 니 다.

5.list 를 페이지 로 되 돌려 줍 니 다.
5.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.List;

/**
 * Created by CR7 on 2017-8-18 Json     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 결과
브 라 우 저 에서 접근http://localhost:8080/user/getUserList

6.맵 을 브 라 우 저 로 되 돌려 줍 니 다
실체 로 돌아 가 list 와 의 시험 이 끝 났 으 니 맵 형식의 데 이 터 를 되 돌려 보 세 요.
6.1 되 돌아 오 는 컨트롤 러

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     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 결과
브 라 우 저 에 접근http://localhost:8080/user/getUserMap

이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기