투케이2K

110. (spring/스프링) GSON 사용해 Map , Json 데이터 변환 수행 실시 본문

Spring

110. (spring/스프링) GSON 사용해 Map , Json 데이터 변환 수행 실시

투케이2K 2022. 10. 10. 13:36
반응형

[개발 환경 설정]

개발 툴 : inteli j

개발 언어 : spring

 

[소스 코드]

    // TODO [SEARCH FAST] : [테스트 함수 실행]
    @GetMapping("/TEST_MAIN")
    public String TEST_MAIN(@RequestParam Map<String, String> param){ // [쿼리 파람 방식]
        System.out.println("\n");
        System.out.println("================================================");
        System.out.println("[CLASS] : "+String.valueOf(CLASS_NAME));
        System.out.println("[METHOD] : "+String.valueOf("TEST_MAIN"));
        System.out.println("[INPUT] : "+String.valueOf(param.toString()));
        System.out.println("================================================");
        System.out.println("\n");


        /**
         * // -----------------------------------------
         * [호출 방법]
         * // -----------------------------------------
         * 1. 호출 방식 : GET
         * // -----------------------------------------
         * 2. 호출 방법 : http://localhost:7000/TEST_MAIN
         * // -----------------------------------------
         * */


        // [변수 선언 실시 및 데이터 삽입]
        String sampleJson = "{\n" +
                "  \"name\" : \"TWOK\",\n" +
                "  \"age\" : \"29\"\n" +
                "}";

        Map<String, Object> map = null;
        String jsonString = "";


        // [로직 처리 실시]
        try {

            /**
             * // -----------------------------------------
             * [GSON 사용 필요 정의]
             * // -----------------------------------------
             * 1. build.gradle :
             *
             * implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.5'
             * // -----------------------------------------
             * 2. import :
             *
             * import com.google.gson.Gson;
             * // -----------------------------------------
             * */

            // [GSON 객체 선언 실시]
            Gson create_gson  = new Gson(); // [일반 GSON 생성]
            //Gson create_gson  = new GsonBuilder().setPrettyPrinting().create(); // [PrettyPrinting 옵션을 추가 데이터 이쁘게 출력]


            // [JSON 데이터를 MAP 으로 변환 수행 실시]
            map = create_gson .fromJson(sampleJson, Map.class);


            // [GSON 사용해 Map 데이터를 JSON 으로 변환 수행 실시]
            jsonString = create_gson.toJson(map); // [Map 객체 삽입]
        }
        catch (Exception e){
            e.printStackTrace();
        }


        // [로그 결과 출력 실시]
        System.out.println("\n");
        System.out.println("================================================");
        System.out.println("[CLASS] : "+String.valueOf(CLASS_NAME));
        System.out.println("[METHOD] : "+String.valueOf("TEST_MAIN"));
        System.out.println("[map] : "+String.valueOf(map.toString()));
        System.out.println("[jsonString] : "+String.valueOf(jsonString));
        System.out.println("================================================");
        System.out.println("\n");


        // [리턴 데이터 반환]
        return "";
    }
 

[결과 출력]


반응형
Comments