jar 안에 웹 컨테이너가 포함되어있다.
웹 컨테이너는 ? 톰캣
war 는 웹 컨테이너가 깔려있는 상태에서 web 아카이브 root폴더에 넣어두면 알아서 압축을 풀어 실행
Talend API ,Chrome Extension
RestAPI -> localshot:8081/apis/demo
return을 Hello를 출력하게 Rest client로 확인
package com.example.demo.controller;
import com.example.demo.dto.UserInfo;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
@RestController //Rest 세션리스, 요청에 대한 응답 후 서버에 클라이언트 정보를 가지지 않음, 리소스만 리턴하는 컨트롤러, 이를 위한 어노테이션
@RequestMapping(value = "/apis")
public class ApiController {
@GetMapping("/hello")
public String hello()
{
return "hello";
}
// apis/path/{kim} "kim" 부터는 path variable
// spring이 PathVariable을 쓰는지 알수있도록 어노테이션 사용
// @GetMapping(path = "/path/{name}")
// public String checkName(@PathVariable String name)
// {
// return name;
// }
@GetMapping(path = "/path/{name}/100/{age}")
public String checkName(
@PathVariable String name,
@PathVariable int age){
return name;
}
//query param
//apis/info?id=1&pw=1234
@GetMapping("/info")//입력 할때 id로 하고 프로그램 내부에서는 idx로 쓰고 싶을 경우
//RequestParam 주소에 데이터를 담아서 보낼 때,
// name은 프로그램 내부에서 선언한 변수와 입력 변수가 다를 때 사용.
// required default는 true
// 이런 정보는 클라이언트로부터 넘어오고 DB에서 뒤져야한다. 그리고 다시 리턴
public String login(@RequestParam(name = "id") String idx, @RequestParam String pw)
{
return "user info" + idx + ":" + pw;
}
//apis/info?id=1&pw=1234&name=kim&nick=777
@GetMapping(path = "/info/all")//입력 할때 id로 하고 프로그램 내부에서는 idx로 쓰고 싶을 경우
public String loginAll(@RequestParam Map<String, String> params)
{
var buffer = new StringBuilder();
params.entrySet().forEach(e->{
System.out.println(e.getKey());
System.out.println(e.getValue());
System.out.println();
buffer.append(e.getKey()+":"+e.getValue()+"\n");
});
return buffer.toString();
}
//입력 할때 id로 하고 프로그램 내부에서는 idx로 쓰고 싶을 경우
@GetMapping(path = "/info/all/obj")
public String loginAll(UserInfo info)
{
return info.toString();
}
}
[Json format]
String: {"name":"kim", "nick":"david"}
int: {"age":19}
bool: {"is_agree":true}
object:
{
"account":{
"email":"david@gmail",
"password":"1234",
}
}
array:
{
"employee": [
{"name":"david","age":19},
{"name":"david","age":19},
{"name":"david","age":19},
]
}
Json의 key값은 소문자 or _ (언더바) 사용
PhoneNumber -> phone_number
아래의 Json data를 DTO class 만들기
{
"name": "david",
"email": "david@gmail.com",
"my_hobby": {
"name": "football",
"use": true,
"terms" : 29
}
}
->Update or Create
{
"name": "kim",
"age": 18,
"car_list": [
{"name": "audi", "car_number": "12-0909"},
{"name": "bmw", "car_number": "13-0909"}
]
}
package com.example.demo.controller;
import com.example.demo.dto.Customer;
import com.example.demo.dto.MyCloud;
import com.example.demo.dto.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
@Slf4j//로그찍는
@RestController
//Rest 세션리스, 요청에 대한 응답 후 서버에 클라이언트 정보를 가지지 않음, 리소스만 리턴하는 컨트롤러, 이를 위한 어노테이션
@RequestMapping(value = "/apis")
public class ApiController {
@GetMapping("/hello")
public String hello()
{
return "hello";
}
// apis/path/{kim} "kim" 부터는 path variable
// spring이 PathVariable을 쓰는지 알수있도록 어노테이션 사용
// @GetMapping(path = "/path/{name}")
// public String checkName(@PathVariable String name)
// {
// return name;
// }
@GetMapping(path = "/path/{name}/100/{age}")
public String checkName(
@PathVariable String name,
@PathVariable int age){
return name;
}
//query param
//apis/info?id=1&pw=1234
@GetMapping("/info")//입력 할때 id로 하고 프로그램 내부에서는 idx로 쓰고 싶을 경우
//RequestParam 주소에 데이터를 담아서 보낼 때,
// name은 프로그램 내부에서 선언한 변수와 입력 변수가 다를 때 사용.
// required default는 true
// 이런 정보는 클라이언트로부터 넘어오고 DB에서 뒤져야한다. 그리고 다시 리턴
public String login(@RequestParam(name = "id") String idx, @RequestParam String pw)
{
return "user info" + idx + ":" + pw;
}
//apis/info?id=1&pw=1234&name=kim&nick=777
//입력 할때 id로 하고 프로그램 내부에서는 idx로 쓰고 싶을 경우
@GetMapping(path = "/info/all")
public String loginAll(@RequestParam Map<String, String> params)
{
var buffer = new StringBuilder();
params.entrySet().forEach(e->{
System.out.println(e.getKey());
System.out.println(e.getValue());
System.out.println();
buffer.append(e.getKey()+":"+e.getValue()+"\n");
});
return buffer.toString();
}
//입력 할때 id로 하고 프로그램 내부에서는 idx로 쓰고 싶을 경우
@GetMapping(path = "/info/all/obj")
public String loginAll(UserInfo info)
{
return info.toString();
}
@PostMapping(path = "/user/{id}/info")
public Customer postTest(@PathVariable(name = "id") String myId,
@RequestParam String nick,
@RequestBody Customer customer){
log.info("myId={}, nick={}", myId, nick );
var name = customer.getMyHobby().getName();
return customer;
}
@PutMapping("/put/{userid}")
public MyCloud putTest(@PathVariable(name = "userid") String userId,
@RequestBody MyCloud myCloud)
{
log.info("userId={}", userId);
return myCloud;
}
@DeleteMapping("/delete/{userid}")
public void delete(@PathVariable String userid)
{
log.info("delete id = {}", userid);
}
}