3 minute read

๐Ÿ“˜ ์‚ฌ์ „์ค€๋น„

postMan์ด๋ผ๋Š” API๋ฅผ ๋””์ž์ธํ•˜๊ณ  ๋นŒ๋“œํ•˜๊ณ  ํ…Œ์ŠคํŠธํ•˜๊ธฐ ์œ„ํ•œ API ํ”Œ๋žซํผ์„ ๊น”์•„์ค€๋‹ค.

๋‹ค์Œ์œผ๋กœ IntelliJ ๋ฅผ ์„ค์น˜ํ•ด์ค€๋‹ค.
์ดํ›„ Spring initializr๋ฅผ ์ด์šฉํ•˜์—ฌ ์ƒˆ ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•ด์ค€๋‹ค.

image

์ด๋•Œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด Spring Boot DevTools, Lombok๊ณผ Spring Web์„ ์ฒดํฌํ•ด์ค€๋‹ค.

image

์ดํ›„, ์œ ๋‹ˆํ‹ฐ ์„ค์น˜ ๋ฐฉ๋ฒ• ์— ์žˆ๋Š” ๋ฐฉ๋ฒ•์„ ๋”ฐ๋ผ์„œ, ์œ ๋‹ˆํ‹ฐ๋ฅผ ์„ค์น˜ํ•œ๋‹ค.


๐Ÿ“˜ PostMapping์„ ์‚ฌ์šฉํ•˜๊ธฐ

๋‹ค์Œ๊ณผ ๊ฐ™์ด main/java/com.example.(ํŒŒ์ผ์ด๋ฆ„) ์•„๋ž˜์— Controller๋ผ๋Š” ํŒจํ‚ค์ง€๋ฅผ ๋งŒ๋“ค๊ณ , ๊ทธ ํด๋” ์•ˆ์— PostController๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ์ž๋ฐ” ์Šคํฌ๋ฆฝํŠธ๋ฅผ ํ•˜๋‚˜ ์ƒ์„ฑํ•ด์ค€๋‹ค.

image

์ดํ›„ ์•ˆ์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์ค€๋‹ค.

package com.example.imsgame.Controller;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class asd {

    @PostMapping("/postMethod")
    public ResponseEntity<String> processData(@RequestBody UnityData unityData, @RequestHeader("Content-Type") String contentType) {
        if ("application/json".equals(contentType)) {
            String result = "์ˆ˜์‹ ๋œ ๋ฐ์ดํ„ฐ: " + unityData.toString();
            return ResponseEntity.ok(result);
        } else {
            return ResponseEntity.badRequest().body("Invalid Content-Type");
        }
    }

    @JsonAutoDetect
    public static class UnityData {
        private String id;
        private String password;

        public UnityData(String id, String password) {
            this.id = id;
            this.password = password;
        }

        public String getId() {
            return id;
        }

        public String getPassword() {
            return password;
        }

        public void setId(String id) {
            this.id = id;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        //toString ์˜ค๋ฒ„๋ผ์ด๋“œ
        @Override
        public String toString() {
            return "์œ ๋‹ˆํ‹ฐ ๋ฐ์ดํ„ฐ{" +
                    "์ด๋ฆ„ ='" + id + "'" +
                    ", password =" + password +
                    '}';
        }
    }
}

์œ„ ์ฝ”๋“œ์—์„œ @ ๋ผ๋Š” ํŠน์ดํ•œ ๊ธฐํ˜ธ๊ฐ€ ๋ณด์ธ๋‹ค.
์ด๋Š” Annotation์ด๊ณ  ์ด๋Š” Annotation ์ด๋ž€ ๋ฌด์—‡์ธ๊ฐ€ ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

์ด๋•Œ ResponseEntity<String> processData() ํ•จ์ˆ˜์—์„œ /postMethod๋ผ๋Š” ์ฃผ์†Œ๋กœ ์˜จ ๋ฐ์ดํ„ฐ์— ๋“ค์–ด์žˆ๋Š” body๋ฅผ ํŒŒ์‹ฑํ•ด์„œ ์ฒ˜๋ฆฌํ•œ๋‹ค.
์ด๋•Œ ์ด ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ UnityData๋ผ๋Š” DTO๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด์ฃผ๊ณ , ์•ˆ์— String ํ˜•์‹์œผ๋กœ id์™€ password๋ฅผ ์ƒ์„ฑํ•ด์ค€๋‹ค.
์—ฌ๊ธฐ์„œ getter, setter๋ฅผ ์„ ์–ธํ•ด ์ค˜์•ผ ํ•˜๋Š”๋ฐ, IntelliJ์—์„œ ์ด๋ฅผ ์ง€์›ํ•˜๋Š” ๊ฐ„๋‹จํ•œ ๋ช…๋ น์–ด๊ฐ€ ์žˆ๋‹ค.
์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ์‹ถ์€ ๊ณณ์— ์ปค์„œ๋ฅผ ๋‘” ๋’ค, alt + insert ํ‚ค๋ฅผ ๋ˆŒ๋Ÿฌ์ค€ ํ›„, getter, setter๋ฅผ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค.

์ด์ œ (ํŒŒ์ผ์ด๋ฆ„)Application์„ ์‹คํ–‰์‹œํ‚ค๋ฉด ๋‹ค์Œ ํ™”๋ฉด๊ณผ ๊ฐ™์ด ์ •์ƒ์ ์œผ๋กœ ์ฝ”๋“œ๊ฐ€ ์ž‘๋™์ด ๋œ๋‹ค.

image

์ดํ›„ postMan์—์„œ ํ˜•์‹์„ post๋กœ ๋ณ€๊ฒฝํ•ด์ค€๋’ค, http://localhost:8080/postMethod๋ฅผ ์ž…๋ ฅํ•ด์ฃผ๊ณ , body ๋ถ€๋ถ„์— raw ๋ฐ์ดํ„ฐ ํ˜•์‹์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž…๋ ฅํ•ด์ค€๋’ค Send๋ฅผ ๋ˆ„๋ฅด๋ฉด ์ด๋ ‡๊ฒŒ ๋‹ต๋ณ€์ด ์˜ค๊ฒŒ ๋œ๋‹ค.

image


๐Ÿ“˜ ๋กœ๊ทธ์ธ ์ฒดํฌํ•˜๊ธฐ

postMapping์œผ๋กœ ๋ฐ›์•„์˜จ ๋ฐ์ดํ„ฐ๊ฐ€ UnityData์™€ ์•„์ด๋””์™€ ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ๊ฐ™์€์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•˜์—ฌ, UnityData class ์•ˆ์— ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.

    public boolean isExist() {
        if (id.equals(id2) && password.equals(password2)) {
            return true;
        } else {
            return false;
        }
    }

์ดํ›„ ์ž„์˜๋กœ ์ •ํ•œ ์•„์ด๋””, ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ๋„ฃ์–ด์ฃผ๊ณ , ๋ฐ›์€๊ฒƒ๊ณผ ์ฒดํฌํ•ด์„œ ๊ฐ™์€์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ๋ณธ๋ฌธ์„ ๋ฐ”๊ฟ”์ค€๋‹ค.

    final static String id2 = "123";
    final static String password2 = "456";

    @PostMapping("/postMethod")
    public ResponseEntity<String> processData(@RequestBody UnityData unityData, @RequestHeader("Content-Type") String contentType) {
        if ("application/json".equals(contentType)) {
            String result = "์ˆ˜์‹ ๋œ ๋ฐ์ดํ„ฐ: " + unityData.toString();
            String result2;
            if (unityData.isExist()) {
                System.out.println("๋งž๋„ค");
                result2 = "ok";
            } else {
                System.out.println("ํ‹€๋ฆฌ๋„ค");
                result2 = "no";
            }

            return ResponseEntity.ok(result);
        } else {
            return ResponseEntity.badRequest().body("Invalid Content-Type");
        }
    }


๐Ÿ“˜ Unity ์—ฐ๋™ํ•˜๊ธฐ

์ดํ›„ ์œ ๋‹ˆํ‹ฐ์— ๋“ค์–ด๊ฐ€์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด UI๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.

image


์ดํ›„ ์•„๋ž˜์™€ ๊ฐ™์€ ScriptํŒŒ์ผ์„ ๋งŒ๋“ค์–ด์ค€๋’ค, ์„ค์ •ํ•ด์ฃผ๋ฉด Unity์ชฝ์€ ์™„์„ฑ๋œ๋‹ค.
์ด ์œ ๋‹ˆํ‹ฐ ์—ฐ๋™ ๋ถ€๋ถ„์—์„œ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๋ถ€๋ถ„์€ ์œ ๋‹ˆํ‹ฐ์—์„œ ๋ณด๋‚ด๋Š” ๋ฐ์ดํ„ฐ์˜ ๋ณ€์ˆ˜๋ช…๊ณผ ์„œ๋ฒ„์ชฝ์—์„œ ๋ฐ›๋Š” ๋ฐ์ดํ„ฐ์˜ ๋ณ€์ˆ˜๋ช…์ด ๋˜‘๊ฐ™์•„์•ผํ•œ๋‹ค.
์ด ์˜ค๋ฅ˜๋ฅผ ์ฐพ์•„๋‚ด๋Š”๋ฐ ๊ฝค๋‚˜ ์‹œ๊ฐ„์ด ๊ฑธ๋ ธ๋‹ค.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
using UnityEngine.Networking;
using System.Text;

public class UI_Manage : MonoBehaviour
{
    public Text id;
    public Text password;
    // Start is called before the first frame update
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
    }

    IEnumerator SendJsonData(){
        MyData data = new MyData();

        data.id = id.text.ToString();
        data.password = password.text.ToString();

        String url = "http://localhost:8080/postMethod"; // ์ƒํ™ฉ์— ๋งž๋Š” api ํ˜ธ์ถœ -> ๋งคํ•‘๋œ url์„ ํ˜ธ์ถœํ•œ๋‹ค.
        String json = JsonUtility.ToJson(data);

        UnityWebRequest request = UnityWebRequest.Post(url, json);

        request.SetRequestHeader("Content-Type", "application/json");

        //==============์ค‘์š”============
        byte[] jsonToSend = new UTF8Encoding().GetBytes(json);
        request.uploadHandler = new UploadHandlerRaw(jsonToSend);

        yield return request.SendWebRequest();

        if(request.isNetworkError || request.isHttpError){
            Debug.LogError(request.error);
        }else{
            Debug.Log(request.downloadHandler.text);
        }
    }

    public void OnClick2(){
        Debug.Log(id.text + " " + password.text);
        StartCoroutine(SendJsonData());
    }
}

[System.Serializable]
public class MyData{
    public string id;
    public string password;
}


์ดํ›„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ‹€๋ ธ์„ ๋–„๋Š” ์„œ๋ฒ„์ชฝ์—์„œ ํ‹€๋ฆฌ๋‹ค๊ณ  ํ•˜๊ณ ,

image

์˜ณ๋ฐ”๋ฅด๊ฒŒ ํ–ˆ์„์‹œ, ๋งž๋‹ค๊ณ  ์š”์ฒญ์ด ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค.

image



๊ฐœ์ธ ๊ณต๋ถ€ ๊ธฐ๋ก์šฉ ๋ธ”๋กœ๊ทธ์ž…๋‹ˆ๋‹ค.
ํ‹€๋ฆฌ๊ฑฐ๋‚˜ ์˜ค๋ฅ˜๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ ์ œ๋ณดํ•ด์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.๐Ÿ˜