@RestController > 스프링 부트

본문 바로가기

[개발] @RestController

필기자
2022-09-07 12:08 6,494 0

본문

프로젝트 구조

 

20220907120813_ddfa29dc4cb6a70c618839d7aa582e62_bw24.png

 



@RestController
@RequestMapping("/rest")
public class DefaultRestController {

    //http://localhost:8080/rest/json
    @GetMapping("/json")
    public ResponseEntity<Object> html(Model m) {
        m.addAttribute("model","모델값 json");
        //return new ResponseEntity<>(m, HttpStatus.OK);
        return ResponseEntity.ok(m);
    }

    //http://localhost:8080/rest/dto?name=홍길동&phone=01011111111
    @GetMapping("/dto")
    public UserDto html(UserDto userDto) {
        LOGGER.info("dto > json");
        return userDto;
    }

    //http://localhost:8080/rest/map
    @GetMapping("/map")
    public HashMap<String,String> html() {
        HashMap<String,String> map = new HashMap<>(){{
            put("이름","홍길동");
            put("나이","30");
            put("국적","서울");
        }};
        return map;
    }

    //http://localhost:8080/rest/parm?name=홍길동&phone=01011111111
    @GetMapping(value = "/parm")
    public String getRequestParam2(@RequestParam Map<String, String> param) {
        StringBuilder sb = new StringBuilder();

        param.entrySet().forEach(map -> {
            sb.append(map.getKey() + " : " + map.getValue() + "\n");
        });
        
        /*
        for (String key : param.keySet()) {
            String value = param.get(key);
            sb.append(key + " : " + value + "\n");
            System.out.println("[key]:" + key + ", [value]:" + value);
        }
        */

        return sb.toString();
    }
}

댓글목록0

등록된 댓글이 없습니다.
게시판 전체검색