@GetMapping, @PostMapping > 스프링 부트

본문 바로가기

[개발] @GetMapping, @PostMapping

필기자
2022-09-07 11:32 5,869 0

본문

프로젝트 구조

 

20220907115034_ddfa29dc4cb6a70c618839d7aa582e62_utmr.png

 

controller/DefaultGetController.java



@Controller
@RequestMapping("/get")
public class DefaultGetController {
    //http://localhost:8080/get/html
    @GetMapping("/html")
    public String html() {
        return "get/index";
    }

    //http://localhost:8080/get/html/안녕하세요
    @GetMapping("/html/{msg}")
    public String html(@PathVariable String msg, Model m) {
        m.addAttribute("msg", msg);
        return "get/index";
    }

    //http://localhost:8080/get/txt?msg=안녕하세요
    @GetMapping("/txt")
    @ResponseBody
    public String html(@RequestParam(value="msg", required=false) String msg) {
        return msg;
    }

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

    //http://localhost:8080/get/dto?name=홍길동&phone=01011111111
    @GetMapping("/dto")
    @ResponseBody
    public UserDto html(UserDto userDto) {
        return userDto;
    }

    //http://localhost:8080/get/dto-model?name=홍길동&phone=01011111111
    @GetMapping("/dto-model")
    public String html(UserDto userDto, Model m) {
        m.addAttribute("dto", userDto);
        return "get/index";
    }
}

 

controller/DefaultPostController.java



@Controller
@RequestMapping("/post")
public class DefaultPostController {

    @PostMapping("/html")
    public String html() {
        return "post/index";
    }

    @PostMapping("/html/{msg}")
    public String html(@PathVariable String msg, Model m) {
        m.addAttribute("msg", msg);
        return "post/index";
    }

    @PostMapping("/txt")
    @ResponseBody
    public String html(@RequestParam(value="msg", required=false) String msg) {
        return msg;
    }

    @PostMapping("/json")
    @ResponseBody
    public ResponseEntity<Object> html(Model m) {
        m.addAttribute("model","모델값 json");
        return new ResponseEntity<>(m, HttpStatus.OK);
    }

    @PostMapping("/dto")
    @ResponseBody
    public UserDto html(UserDto userDto) {
        return userDto;
    }

    @PostMapping("/dto-model")
    public String html(UserDto userDto, Model m) {
        m.addAttribute("dto", userDto);
        return "post/index";
    }
}

 

댓글목록0

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