[SOAP Step8] 테스트 Client 생성 CXF

1. 일반 Java 프로젝트를 하나 만들어서 다음과 같은 테스트 Client 프로그램을 만든다.

- 다음과 같이 Customer라는 객체를 xml 형식으로 만들어서 HTTP Request의 BODY영역에 저장해서 서비스를 호출했다.
- 현재의 예제는 RESTful 방식이 아닌 SOAP 방식의 Webservice이기 때문에 xml을 만들때 <soap:Envelope>아 같은 soap 통신에 필요한 태그들을 만들어 줘야한다. 그렇지 않으면 soap 버전이 아니라고 오류가 난다.


소스는 다음과 같다.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

public class CallCXF {
public static void main(String[] args) throws Exception {
String xml = 
"<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"" + 
"                  soap:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"" + 
       "                           xmlns:xsi=\"http://www.w3.org/1999/XMLSchema-instance\"" +
         "                           xmlns:xsd=\"http://www.w3.org/1999/XMLSchema\">" +
         "<soap:Body>" +
         "    <x:addCustomer xmlns:x=\"http://cxf.gnnet.co.kr/\"> +
          "     <customer>                                                           " +
          " <id>1234</id>                                               " +
          " <name>김학수</name>                                   " +
          " <address>경기도</address>                           " +
          " </customer>                                                        " +
          "    </x:addCustomer>                                                          " +
         "</soap:Body>                                                                       " +
         "</soap:Envelope>                                                                 ";

URL url = new URL("http://localhost:8080/CXF/services/CXFServicePort/addCustomer");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        conn.setDoOutput(true);
conn.setRequestMethod("POST");
// Header 영역에 쓰기
conn.addRequestProperty("Content-Type", "text/xml");
        // BODY 영역에 쓰기
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(xml);
        wr.flush();
   
        // 리턴된 결과 읽기
        String inputLine = null;
                BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                while ((inputLine = in.readLine()) != null) {
         System.out.println(inputLine);
                }
                in.close();
wr.close();
         }
}

========================================================================
Tomcat 로그에 다음과 같이 넘어간 Customer 객체의 정보가 보일 것이다.(비록 한글이 깨졌지만)


그리고 Client 쪽으로 리턴된 값도 다음과 같이 볼 수 있다.



트랙백

이 글과 관련된 글 쓰기 (트랙백 보내기)
TrackbackURL : http://hsouhy.egloos.com/tb/461247 [도움말]

덧글

댓글 입력 영역