package com.example.hello.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
// Controller로서 동작하기 위해서는 어노테이션
@RestController
// 요청에 대해 어떤 Controller, 어떤 메소드가 처리할지를 맵핑하기 위한 어노테이션
@RequestMapping("/api/get")
public class GetApiController {
}
package com.example.hello.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
// Controller로서 동작하기 위해서는 어노테이션
@RestController
// 요청에 대해 어떤 Controller, 어떤 메소드가 처리할지를 맵핑하기 위한 어노테이션
@RequestMapping("/api/get")
public class GetApiController {
// http://localhost:8090/api/get/hello
@GetMapping(path = "/hello")
public String getHello() {
return "get Hello";
}
// get / post / put / delete 모두에 동작
// http://localhost:8090/api/get/hi
@RequestMapping(path = "/hi", method = RequestMethod.GET)
public String hi() {
return "hi";
}
}
package com.example.hello.controller;
import org.springframework.web.bind.annotation.*;
// Controller로서 동작하기 위해서는 어노테이션
@RestController
// 요청에 대해 어떤 Controller, 어떤 메소드가 처리할지를 맵핑하기 위한 어노테이션
@RequestMapping("/api/get")
public class GetApiController {
// http://localhost:8090/api/get/path-variable/{name}
// http://localhost:8090/api/get/path-variable/{spring}
// http://localhost:8090/api/get/path-variable/{java}
// ...
@GetMapping("/path-variable/{name}")
public String pathValiable(@PathVariable String name) {
System.out.println("PathVariable : "+name);
return name;
}
}
package com.example.hello.controller;
import org.springframework.web.bind.annotation.*;
// Controller로서 동작하기 위해서는 어노테이션
@RestController
// 요청에 대해 어떤 Controller, 어떤 메소드가 처리할지를 맵핑하기 위한 어노테이션
@RequestMapping("/api/get")
public class GetApiController {
// http://localhost:8090/api/get/path-variable/{name}
// http://localhost:8090/api/get/path-variable/{spring}
// http://localhost:8090/api/get/path-variable/{java}
// ...
@GetMapping("/path-variable/{name}")
public String pathValiable(@PathVariable(name="name") String pathName) {
System.out.println("PathVariable : "+pathName);
return pathName;
}
}
package com.example.hello.controller;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
// Controller로서 동작하기 위해서는 어노테이션
@RestController
// 요청에 대해 어떤 Controller, 어떤 메소드가 처리할지를 맵핑하기 위한 어노테이션
@RequestMapping("/api/get")
public class GetApiController {
// https://www.google.com/search?q=IntelliJ&oq=in&aqs=chrome.3.69i59l2j69i57j69i59j69i60l2j69i61j69i60.1399j0j7&sourceid=chrome&ie=UTF-8
// ?q = IntelliJ
// &oq = in
// &aqs = chrome.3.69i59l2j69i57j69i59j69i60l2j69i61j69i60.1399j0j7
// &sourceid = chrome
// &ie = UTF-8
// http://localhost:8090/api/get/query-param?user=steve&email=steve@gmail.com&age=30
@GetMapping(path = "/query-param")
public String queryParam(@RequestParam Map<String, String> queryParam) {
StringBuilder sb = new StringBuilder();
queryParam.entrySet().forEach(entry -> {
System.out.println(entry.getKey());
System.out.println(entry.getValue());
System.out.println("\n");
sb.append(entry.getKey()+" = "+entry.getValue()+"\n");
});
return sb.toString();
}
}
package com.example.hello.controller;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
// Controller로서 동작하기 위해서는 어노테이션
@RestController
// 요청에 대해 어떤 Controller, 어떤 메소드가 처리할지를 맵핑하기 위한 어노테이션
@RequestMapping("/api/get")
public class GetApiController {
// https://www.google.com/search?q=IntelliJ&oq=in&aqs=chrome.3.69i59l2j69i57j69i59j69i60l2j69i61j69i60.1399j0j7&sourceid=chrome&ie=UTF-8
// ?q = IntelliJ
// &oq = in
// &aqs = chrome.3.69i59l2j69i57j69i59j69i60l2j69i61j69i60.1399j0j7
// &sourceid = chrome
// &ie = UTF-8
// http://localhost:8090/api/get/query-param?user=steve&email=steve@gmail.com&age=30
@GetMapping(path = "/query-param")
public String queryParam(@RequestParam Map<String, String> queryParam) {
StringBuilder sb = new StringBuilder();
queryParam.entrySet().forEach(entry -> {
System.out.println(entry.getKey());
System.out.println(entry.getValue());
System.out.println("\n");
sb.append(entry.getKey()+" = "+entry.getValue()+"\n");
});
return sb.toString();
}
// query set을 명확하게 지정
@GetMapping("/query-param02")
public String queryParam02(
@RequestParam String name,
@RequestParam String email,
@RequestParam int age
) {
System.out.println(name);
System.out.println(email);
System.out.println(age);
return name + " " + email + " " + age;
}
}
package com.example.hello.dto;
public class UserRequest {
private String name;
private String email;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "UserRequest{" +
"name='" + name + '\'' +
", email='" + email + '\'' +
", age=" + age +
'}';
}
}
package com.example.hello.controller;
import com.example.hello.dto.UserRequest;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
// Controller로서 동작하기 위해서는 어노테이션
@RestController
// 요청에 대해 어떤 Controller, 어떤 메소드가 처리할지를 맵핑하기 위한 어노테이션
@RequestMapping("/api/get")
public class GetApiController {
// https://www.google.com/search?q=IntelliJ&oq=in&aqs=chrome.3.69i59l2j69i57j69i59j69i60l2j69i61j69i60.1399j0j7&sourceid=chrome&ie=UTF-8
// ?q = IntelliJ
// &oq = in
// &aqs = chrome.3.69i59l2j69i57j69i59j69i60l2j69i61j69i60.1399j0j7
// &sourceid = chrome
// &ie = UTF-8
// http://localhost:8090/api/get/query-param?user=steve&email=steve@gmail.com&age=30
@GetMapping(path = "/query-param")
public String queryParam(@RequestParam Map<String, String> queryParam) {
StringBuilder sb = new StringBuilder();
queryParam.entrySet().forEach(entry -> {
System.out.println(entry.getKey());
System.out.println(entry.getValue());
System.out.println("\n");
sb.append(entry.getKey()+" = "+entry.getValue()+"\n");
});
return sb.toString();
}
// query set을 명확하게 지정
@GetMapping("/query-param02")
public String queryParam02(
@RequestParam String name,
@RequestParam String email,
@RequestParam int age
) {
System.out.println(name);
System.out.println(email);
System.out.println(age);
return name + " " + email + " " + age;
}
// dto
@GetMapping("/query-param03")
public String queryParam03(UserRequest userRequest) {
System.out.println(userRequest.getName());
System.out.println(userRequest.getEmail());
System.out.println(userRequest.getAge());
return userRequest.toString();
}
}