SpringBoot 의 json 데이터 반환 실현 방법
4832 단어 SpringBoot되돌아오다json 데이터
조작 상세 참조: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
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
【Java・SpringBoot・Thymeleaf】 에러 메세지를 구현(SpringBoot 어플리케이션 실천편 3)로그인하여 사용자 목록을 표시하는 응용 프로그램을 만들고, Spring에서의 개발에 대해 공부하겠습니다 🌟 마지막 데이터 바인딩에 계속 바인딩 실패 시 오류 메시지를 구현합니다. 마지막 기사🌟 src/main/res...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.