반응형

728x90
반응형

1) rest api가 무엇인가?

자원을 이름(자원의 표현)으로 구분하여 해당 자원의 상태(정보)를 주고 받는 모든 것을 의미한다. 즉, 자원(resource)의 표현(representation) 에 의한 상태 전달

- 암호화 처리 개념

대칭키를 활용하면 통신 구간 암호화를 적용합니다.(AES256 등)

{"userid":"kimjiwon"} -> 클라이언트 암호화 ->ㅕ984ㅕ98ㅛ4ㅂ98고9ㅗ륰ㅍ42797294(전송구간) -> 서버 복호화 -> {"userid":"kimjiwon", "RESULT":"OK"} -> 결과값 암호화 -> ㅕ984ㅕ98ㅛ4ㅂ98고9ㅗ륰ㅍ42797294(전송구간) ->

클라이언트 복호화 -> {"userid":"kimjiwon", "RESULT":"OK"}

2) spring 기반 간단한 예제를 통한 Rest api 이해

package com.ji.restapitest;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

@Controller
public class RestApiController {

@ResponseBody
@RequestMapping(value = "/getUserId", method = RequestMethod.GET)
public Object getUserId(HttpServletRequest req) {

String result = "";

String param = req.getParameter("param");

if (param.equals("1")) {
result = "jiwon";
} else {
result = "seonhak";
}

return result;

}

@ResponseBody
@RequestMapping(value = "/getUserId2", method = RequestMethod.GET)
public Object getUserIdOther(@RequestParam("param") String param) {

String result = "";

if (param.equals("1")) {
result = "jiwon";
} else {
result = "seonhak";
}

Map<String, Object> resultMap = new HashMap<String, Object>();
resultMap.put("STATUS", "OK");
resultMap.put("RESULT", result);

Gson gSon = new Gson();
String jsonStr = gSon.toJson(resultMap);

return jsonStr;

}

@ResponseBody
@RequestMapping(value = "/getUserIdPost", method = RequestMethod.POST)
public Object getUserIdPost(@RequestBody String jStr) {

Gson gSon = new Gson();

Type type = new TypeToken<Map<String, String>>() {
}.getType();
Map<String, String> myMap = gSon.fromJson(jStr, type);

String result = "";

if (myMap.get("param").equals("1")) {
result = "jiwon";
} else {
result = "seonhak";
}

Map<String, Object> resultMap = new HashMap<String, Object>();
resultMap.put("STATUS", "OK");
resultMap.put("RESULT", result);

String jsonStr = gSon.toJson(resultMap);

return jsonStr;

}

}

* 참고사이트

https://gmlwjd9405.github.io/2018/09/21/rest-and-restful.html

728x90

+ Recent posts