JDK : 1.8
Tomcat : 3.5.63
Spring : 5.2.3
Spring-webmvc : 4.3.7
jackson-mapper-asl : 1.9.4
jackson-databind : 2.9.3
<!--RestTemplate-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.3</version>
</dependency>
▶ 항상 Dependency 추가 후엔 Project Structure에서 추가된 내용 넣어주기!
package controller;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.HashMap;
import java.util.Map;
@RestController
public class homeController {
@RequestMapping(method = RequestMethod.GET, value = "/getTemp/{str}", produces = MediaType.APPLICATION_JSON_VALUE)
public Map temp(@PathVariable String str) {
HashMap<String, String> json = new HashMap<>();
json.put("id", str);
json.put("pw", "sdasd");
return json;
}
}
▶ @RestController : @Controller에 @ResponseBody가 추가된 형태의 Annotion
--> 기본 @Controller에 @ResponseBody를 사용해도(아래 코드) 같은 형태이다.
@Controller
public class homeController {
@RequestMapping(method = RequestMethod.GET, value = "/getTemp/{str}", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody Map temp(@PathVariable String str) {
▶ produces = MediaType.APPLICATION_JSON_VALUE : 리턴 값 혹은 특정 파라미터를 Json으로 받겠다는 의미
--> HashMap으로 만든 뒤 반환하면 자동으로 json 변형 (jackson-databind lib)