restcontroller

최고고·2022년 11월 30일
0
  • 순수 데이터 전송
  • 기존 컨트롤러와 다르게 동작
    jsp로 보내는게 아님
  • 단순 문자열 : value, produces 속성 이용 <- MIME타입 지정
    response headers 통해 확인가능
  • 객체 반환 : 사용자 지정 객체 반환가능
    produce 속성 생략가능함
  • 컬렉션객체 반환 :
    IntStream.range(1, 10).mapToObj(i -> new SampleVO(i, i+ "First", i+"Last")).collect(Collectors.toList());
    Map<String, String> map = new HashMap<>();
    map.put("first", new Sample(111,"그루트","주니어");
  • ResponsEntity : 데이터와 http 헤더의 상태 메시지 등을 같이 전달하는 용도로 사용함. 상태코드와 에러메시지등을 데이터와 함께 전달할수 있음. ResponseEntity.status(HttpStatus.OK).body(vo); 이렇게
@org.springframework.web.bind.annotation.RestController
public class RestController {

	
	@GetMapping("/getSample")
	public SampleVO getSample() {
		
		return new SampleVO(112,"스타","로드");
		
	}
	@GetMapping(value = "/getText", produces = "text/plain; charset=UTF-8")
	public String getText() {
		
		return "안녕하세요";
		
	}
	
	
	@GetMapping("/getList")
	public List<SampleVO> getList(){
		return IntStream.range(1, 10).mapToObj(i -> new SampleVO(i, i+ "First", i+"Last"))
				.collect(Collectors.toList());
	}
	
	@GetMapping(value="/check", params= {"h", "w"})
	public ResponseEntity<SampleVO> check(Double h, Double w){
		SampleVO vo = new SampleVO(0, ""+ h, ""+w);
		ResponseEntity<SampleVO> result = null;
		if(h<150) {
			result = ResponseEntity.status(HttpStatus.BAD_GATEWAY).body(vo);
		} else {
			result = ResponseEntity.status(HttpStatus.OK).body(vo);
		}
		return result;
		
	}
	
	@GetMapping("pro/{cat}/{pid}")
	public String[] getPath(@PathVariable("cat") String cat,
			@PathVariable("pid") int pid) {
		return new String[] {"category: " + cat, "pid : " + pid}; 
	}
	
	
	// post - 요청한 내용(body)을 처리하기땜에 일반적 파라미터 전달방식 사용x
	@PostMapping("/ticket")
	public Ticket convert(@RequestBody Ticket ticket) {
		return ticket;
	}
}

0개의 댓글