티스토리 뷰

카테고리 없음

Rest API-GET, POST, PUT, DELETE

James Wetzel 2024. 10. 7. 18:33
728x90
반응형

GET

package com.example.demo.controller;

import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/get")
public class GetController {

    @GetMapping("/pathVariable/{name}")
    public String pathVariable(@PathVariable String name) {
        return "pathVariable " + name;
    }

    @GetMapping("pathVariable2/{name}/{email}")
    public String pathVariable2(
            @PathVariable(value="name") String name,
            @PathVariable(value="email") String email
    ) {
        return String.format("name=%s, email=%s", name, email);
    }

    @GetMapping("/requestParam")
    public String requestParam(
            @RequestParam String name,
            @RequestParam String email
    ) {
        return String.format("name=%s, email=%s", name, email);
    }

    @GetMapping("requestParam2")
    public String requestParam2(@RequestParam Map<String, String> maps) {
        StringBuilder sb = new StringBuilder();

        maps.forEach((key, value) -> {
            sb.append(String.format("%s : %s", key, value));
        });

        return sb.toString();
    }
}


POST

package com.example.demo.controller;

import com.example.demo.dto.MemberDto;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/post")
public class PostController {

    @PostMapping("requestBody")
    public String requestBody(@RequestBody MemberDto memberDto) {
        return memberDto.toString();
    }

    @PostMapping("/requestBody2")
    public String requestBody2(@RequestBody Map<String, String> maps) {
        StringBuilder sb = new StringBuilder();

        maps.forEach((key, value) -> {
            sb.append(String.format("%s : %s, ", key, value) );
        });

        return sb.toString();
    }
}


PUT

package com.example.demo.controller;

import com.example.demo.dto.MemberDto;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/put")
public class PutController {

    @PutMapping("/requestBody")
    public String requestBody(@RequestBody MemberDto memberDto) {
        return memberDto.toString();
    }

    @PutMapping("/responseEntity")
    public ResponseEntity<MemberDto> responseEntity(@RequestBody MemberDto memberDto) {
        return ResponseEntity.status(HttpStatus.BAD_REQUEST)
                .body(memberDto);
    }
}


DELETE

package com.example.demo.controller;

import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/delete")
public class DeleteController {

    @DeleteMapping("/pathVariable/{name}")
    public String pathVariable(@PathVariable String name) {
        return "delete pathVariable " + name;
    }

    @DeleteMapping("/pathVariable2/{pathVariable2}/{pathVariable1}")
    public String pathVariable2(
            @PathVariable(value="pathVariable1") String pathVariable1,
            @PathVariable(value="pathVariable2") String pathVariable2
    ) {
        return "hi2 " + pathVariable1 + pathVariable2;
    }

    @DeleteMapping("/requestParam")
    public String requestParam(
            @RequestParam String name,
            @RequestParam String email
    ) {
        return String.format("%s, %s", name, email);
    }

}

728x90
반응형