[TIL] Spring Basics - Controllers and HTTP Mapping
This is a TIL that summarizes how to use Controller and HTTP method mapping in Spring MVC.
한국어 원문은 여기에서 볼 수 있습니다.
What to do today
What I studied
@Controller
- Enables the class to perform the role of Controller
- Spring MVC creates a Front Controller pattern for efficient API processing.
- There is no need to create a file for each API, and APIs with similar characteristics are managed with one controller.
@GET, @POST, @PUT, @DELETE Annotations mapped to each HTTP Method
1 2 3 4 5 @GetMapping("/api/get") @ResponseBody public String get() { return "GET Method 요청"; }
@RequestMapping Shortens duplicate URLs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 @Controller @RequestMapping("/api") public class HelloController { @GetMapping("/hello") @ResponseBody public String hello() { return "Hello World!"; } @GetMapping("/get") @ResponseBody public String get() { return "GET Method 요청"; } @PostMapping("/post") @ResponseBody public String post() { return "POST Method 요청"; } @PutMapping("/put") @ResponseBody public String put() { return "PUT Method 요청"; } @DeleteMapping("/delete") @ResponseBody public String delete() { return "DELETE Method 요청"; } }
Static and dynamic pages
- static pages
/resources/static/파일이름.html- When an html file is requested directly from the SpringBoot server, the html file is found in the static folder and returned.
- It can also be returned through the Controller, but there is no need to return an already completed static HTML file through the Controller.
java @GetMapping("/static-hello") public String hello() { return "hello.html"; }-Redirect - If you want to process HTML files in the static folder through the Controller while applying the template engine (thymeleaf), you can return the redirect request as a string and the request will be re-executed to return the files in the static folder. ``` java @GetMapping(“/html/redirect”) public String htmlStatic() { return “redirect:/hello.html”; }
```
- Dynamic pages
/resources/templates/파일이름.html- The simplest method is to directly call the html file in the static folder.
- If you want to prevent direct access from an external browser or want to control it through a controller in a specific situation,
This can be processed by adding the corresponding static html file to the templates folder and returning the** “file name” string, which is the name of the html file (
.htmlcan be omitted!**)- Dynamic page processing process
- Process client request from Controller to Model
- Passing View and Model to Template engine (Thymeleaf)
- View: Dynamic HTML file
- Model: Information to apply to View
- Template engine
- Apply Model to View -> Create dynamic web page
- Delivers View (dynamic web page, HTML) to Client (browser)
How to return data to Client
- Front-end and back-end develop separately -> Loosely coupled method adopted
- Rather than having the server directly return a view (html/css), the server prefers to return only specific information that matches the request.
- We plan to proceed by manipulating and reflecting HTML in the browser through an API that requests JSON data.
How to return JSON data
- In SpringBoot with the template engine (Thyemleaf), when a string is returned from the Controller, the
.htmlfile for the string is found and returned in the templates folder. -> So, if you want to return JSON data rather than an HTML file to the browser, you must add the@ResponseBodyannotation to the method- In
@Controller, the return value is interpreted as “HTML file name” by default. If there is a
@ResponseBody, Spring interprets it as not looking for a view, but just sending the data as a response.1 2 3 4 5
@ResponseBody @GetMapping("/json") public Star test() { return new Star("Robbie", 95); }
- In
- When the return value is String Since Java does not support the JSON type, it must be converted to a String type in JSON format before use.
- When the return value is a Java class other than String “Java object -> Convert to JSON” Spring automatically converts Java objects to JSON
@RestController @Controller + @ResponseBody
- Using @RestController, you can add the @ResponseBody annotation to all methods of the class.
What is Jackson?
- Jackson Library
- Library that processes
JSONdata structure
- Object can be converted to String of type
JSON.- JSON type String can be converted to Object
- Because Spring provides an API related to
Jackson, you can automatically processJSONdata without writing source code yourself. - When you need to process
JSONdata directly, you can useObjectMapperfrom the Jackson library -Object to JSON- To convert an Object to a
JSONtype String, the get method of the Object is required. ``` java @Test @DisplayName(“Object To JSON : get Method 필요”) void test1() throws JsonProcessingException { Star star = new Star(“Robbie”, 95);
ObjectMapper objectMapper = new ObjectMapper(); // Jackson 라이브러리의 ObjectMapper String json = objectMapper.writeValueAsString(star); // objectMapper의 writeValueAsString 메서드를 사용하여 변환할 수 있음 // 파라미터에 JSON으로 변환시킬 Object의 객체를 주면 됨
System.out.println(“json = “ + json); } ```
- To convert an Object to a
-JSON to Object - In order to convert a JSON type String to an Object, the Object requires a default constructor and a get or set method.
1
2
3
4
5
6
7
8
9
10
11
12
@Test
@DisplayName("JSON To Object : 기본 생성자 & (get OR set) Method 필요")
void test2() throws JsonProcessingException {
String json = "{\"name\":\"Robbie\",\"age\":95}"; // JSON 타입의 String
ObjectMapper objectMapper = new ObjectMapper(); // Jackson 라이브러리의 ObjectMapper
Star star = objectMapper.readValue(json, Star.class);
// objectMapper의 readValue 메서드를 사용해 변환
// 첫 번째 파라미터는 JSON 타입의 String, 두 번째 파라미터에는 변환할 Object의 class 타입을 줌
System.out.println("star.getName() = " + star.getName());
}