:::: 개발 ::::/└ JSP & SPRING

스프링 부트 http 컨트롤 spring boot http status / header controll

nayha 2023. 1. 23. 23:37
반응형

ResponseDTO.java

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ResponseDTO <T>{
    private String error;
    private List<T> data;
}

Hello.java

import com.example.demo230123.dto.ResponseDTO;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class Hello {

    @GetMapping("/testResponseBody")
    public ResponseDTO<String> testControllerRequestBody(){
        List<String> list = new ArrayList<String>();
        list.add("hello world I'm ResponseDTO");
        ResponseDTO<String> res = ResponseDTO.<String>builder().data(list).build();
        return res;
    }

    @GetMapping("/testResponseEntity")
    public ResponseEntity<?> testControllerRequestEnitity(){
        List<String> list = new ArrayList<String>();
        list.add("hello world I'm ResponseEntity And you got 400 !");
        ResponseDTO<String> res = ResponseDTO.<String>builder().data(list).build();
        return ResponseEntity.badRequest().body(res);
    }
}

restApi Test

responseBody
ResponseEntity<?>

가끔 http 상태코드( ResponseEntity ) 변경 해야 할때..까먹어서 적어둠 

 

반응형