JSON response 보내기

u·2022년 5월 22일
0

SpringBoot

목록 보기
18/20

List 형식으로 보내기

@Controller
@RequestMapping("/root1") // 상위 url 설정
public class MyController(){
	@ResponseBody
    @GetMapping(value  = "/get")
    public List<Team> getTeams(){
    
        // 유저가 소속돼 있는 팀 목록 호출
        List<Team> teamList = new ArrayList<>();
        teamList.add(new Team(1, "team1"));
        teamList.add(new Team(2, "team2"));
        teamList.add(new Team(3, "team3"));
        
        return teamList;
    }
}

// VO - getter가 포함돼 있어야 spring에서 자동으로 json 형식으로 전환해준다.
public class Team {
    int teamId;
    String teamName;

    public Team(int teamId, String teamName){
        this.teamId = teamId;
        this.teamName = teamName;
    }

    public int getTeamId() {
        return teamId;
    }

    public String getTeamName() {
        return teamName;
    }
}

객체 하나 반환

VO 객체 하나 사용하기

Json 반환

Hashmap 사용하기

0개의 댓글