RestController
2024. 1. 28. 21:17ㆍSpring Boot
Spring Boot에서 Restful API를 만들어 json 데이터를 보낼 때 주로 사용되는 controller이다
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@RestController
@RequestMapping("/json")
public class otherServer {
@GetMapping("/data")
public MyData getData() {
MyData data = new MyData();
data.setFrom("java");
data.setData("i am spring boot");
return data;
}
}
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class MyData {
private String from;
private String data;
}
해당하는 URL에 접속하면 class를 json으로 바꿔서 보내준다

'Spring Boot' 카테고리의 다른 글
| Mulit Database by MySQL (0) | 2024.02.03 |
|---|---|
| Redis (0) | 2024.01.28 |
| Oauth2 by Google (0) | 2024.01.22 |
| Docker Container에 JAR 이미지 돌리기 (0) | 2024.01.20 |
| MongoDB Reference (1) | 2024.01.19 |