Web #5. Java에서 Json 타입으로 RestAPI 활용
목차

개요
Java프로젝트에서 RestAPI 활용하여 데이터를 Json 형식으로 POST
Java 프로젝트에서의 RESTapi
이전 포스팅에서는 RestAPI 방식을 활용해 Vue 컴포넌트에서 자바스크립트로 코드를 작성하고 서버까지 프론트단에서 받은 데이터가 정상으로 전달 되는지 확인했었다.
Web 3. Rest API에 대한 이해
개요 RestAPI 가 무엇이고 어떻게 사용해야하는가? 목차 [RestAPI란 무엇인가] [Restful API?] RestAPI란 무엇인가? RestAPI는 Representational State Transfer의 약자이며 클라이언트(웹브라우저, 모바일)가 필요한
zunoxi.github.io
다음으로 확인해 볼 부분은 내가 설계한 백엔드단 로직에서 RestAPI 방식으로 다른 서버에 POST 전송을 해보는 부분이다.
이는 html같은 프론트단이 아닌 java 프로그래밍 계층에서 RestAPI를 사용해보는 방법이다.
post 관련 소스가 글을 정리하는데 큰 도움이 되었다.
OKKY | post로 rest쏘기! (http body에 data담아서..)
2일걸려서 샘플같지도 않은 샘플만들었네요 ㅠㅠ 혹시 있을지 모르는 저와 같은 쌩초보분들을 위해 자료 정리겸 공유하려고 올립니다... rest방식의 post send 샘플예제입니다.. client side - json형태
okky.kr
구조
java object => json 변환 => http post 전송 => server 데이터 전송 확인
Sample 예제
(1) java 자료형 선언
JSONObject json = new JSONObject(); // JSON 변환용 오브젝트 선언
String custMsgSn = "test"; // 서버에 전송할 데이터 선언
String senderKeyy = "test";
String phoneNum = "test";
String templateCode = "test";
String message = "test";
String smsSndNum = "test";
String smsKind = "test";
String smsMessage = "test";
String lmsMessage = "test";
String subject = "test";
String button = "test"; }
(2) JSON 변환
JSON형식으로 변환하기 위해서는 json-simple-1.1.1.jar 라는 라이브러리 파일이 필요하다.
다음 링크 를 참고하자.
Central Repository: com/googlecode/json-simple/json-simple/1.1.1
repo1.maven.org
json.put("id", custMsgSn);
json.put("keyy", senderKeyy);
json.put("phone", phoneNum);
json.put("code", templateCode);
json.put("mess", message);
json.put("snd", smsSndNum);
json.put("kind", smsKind);
json.put("smes", smsMessage);
json.put("lmes", lmsMessage);
json.put("sub", subject);
json.put("but", button);
(3) http post 전송
URL postUrl = new URL("http://localhost:9091/api/addalarm"); // RestAPI 서버 및 요청 양식 준수
HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
connection.setDoOutput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("POST"); // post 방식으로 전송
connection.setRequestProperty("Content-Type", "application/json");
OutputStream os= connection.getOutputStream();
os.write(body.getBytes());
os.flush();
System.out.println("Location: " + connection.getHeaderField("Location"));
BufferedReader br = new BufferedReader(new InputStreamReader(
(connection.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
connection.disconnect();
(4) server 구조
Restapi 서버 구조는 링크를 참고하면 될것같다.
Web 3. Rest API에 대한 이해
개요 RestAPI 가 무엇이고 어떻게 사용해야하는가? 목차 [RestAPI란 무엇인가] [Restful API?] RestAPI란 무엇인가? RestAPI는 Representational State Transfer의 약자이며 클라이언트(웹브라우저, 모바일)가 필요한
zunoxi.github.io
그리고 해당 java 파일을 실행시키고 RestAPI 서버를 확인하면 데이터가 들어오는것을 확인할 수 있다.
해당 포스팅은 다음 링크의 글로 부터 이전되었습니다.
https://zunoxi.github.io/programming/2020/06/23/dev-web-restjava/
Web 4. Java에서 Json 타입으로 RestAPI 활용
개요 Java프로젝트에서 RestAPI 활용하여 데이터를 Json 형식으로 POST 목차 Java 프로젝트에서의 RESTapi 구조 Sample 예제 Java 프로젝트에서의 RESTapi 이전 포스팅에서는 RestAPI 방식을 활용해 Vue 컴포넌트
zunoxi.github.io
'Develop > Web' 카테고리의 다른 글
Web #7. 스프링부트(Spring-boot) jar 파일 만들기 (0) | 2020.06.24 |
---|---|
Web #6. html, css, javascript의 관계와 정의 (0) | 2020.06.22 |
Web #4. Rest API에 대한 이해 (0) | 2020.05.29 |
Web #3. Web 프로그래밍? CS 프로그래밍? (2) | 2020.04.06 |
Web #2. Jar와 War (0) | 2020.04.03 |
댓글