SOAP 자바 에서 의 응용 인 스 턴 스

다음은 자바 에서 SOAP 의 관련 유형 과 개념 에 관 한 것 이다.
보기 전에 SOAP 의 기본 개념 을 이해 해 야 한다.
자바 프로그램 에서 SOAP request 를 보 내 는 것 은 어떤 방법 으로 든 최종 적 으로 보 내 는 것 은 표준 SOAP request 라 고 생각 할 수 있 습 니 다.
자바 에는 일부 클래스 가 포함 되 어 있 을 뿐 실제 SOAP request 의 다른 부분 을 대표 합 니 다.그리고 SOAP request 의 내용 을 구성 하 는 데 도움 을 주 는 방법 도 있 습 니 다.
우선 자바 에서 SOAP message 를 어떻게 보 내 는 지 살 펴 보 자.
첫 번 째 유형:SOAPConnectionfinal SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); final SOAPConnection soapConnection = soapConnectionFactory.createConnection();
SOAPMessage reply = soapConnection.call(loginSOAPRequest, new URL("http://172.28.85.6/ASAPService/ASAPService_V1.svc"));
따라서 SOAP request 를 보 내 는 것 은 간단 하 다.SOAP Connection 대상 을 만 들 고 call 방법 을 실행 해 야 한다.
그 중에서 보 낸 loginSOAPRequest 는 이전에 구 성 된 SOAPMessage 대상 이 고 보 낸 목적 URL 은 웹 서비스의 주소 입 니 다.
코드 에서 볼 수 있 듯 이 웹 서비스 가 되 돌아 오 는 response 도 SOAPMessage 대상 이다.그래서 우 리 는 인정 할 수 있다.
자바 에서 보 내 고 받 는 대상 은 모두 SOAPMessage 입 니 다.
우 리 는 SOAPMessage 라 는 대상 에 SOAP 와 http 헤드 의 정보 가 포함 되 어 있 을 것 이 라 고 더욱 인정 할 수 있다.이렇게 보 내 는 것 만 이 완전 하고 효과 적 인 SOAP request 입 니 다.
두 번 째 유형:SOAPMessage우선 하나의 개념 을 명 확 히 한다.SOAPMessage 라 는 대상 은 http 헤드 의 정 보 를 포함 하고 있 지만.그러나 자바 에는 http 헤드 정보 에 대응 하 는 별도의 클래스 가 없습니다.
http header 의 정 보 는 코드 자체 구조 가 필요 없 이 자바 에서 자동 으로 완성 되 기 때문이다.즉,위 에서 본 필수 http 정보 POST,contenttype 등 정 보 는 자동 으로 채 워 집 니 다.
우 리 는 이 정 보 를 수정 할 수 있 지만,나중에 언급 할 것 이지 만,스스로 구성 할 필 요 는 없다.
SOAPMessage 대상 은 SOAPpart 와 Attachment Part 두 부분 을 포함한다.  그 중에서 SOAPPArt 는 필수 적 인 부분 이 고 Attachment Part 는 선택 할 수 있 는 부분 이다.
SOAPpart 는 사실 완전한 SOAP request 입 니 다.http 헤드 와 SOAP message 정 보 를 포함 합 니 다.SOAPPArt 는 XML 구조 여야 합 니 다.
Attachment Part 는 0 개 또는 여러 개 로 SOAP 에서 휴대 할 수 있 는 추가 정보,예 를 들 어 그림,오디 오 등 정보 로 이해 할 수 있다.
SOAPPart  Attachment Part 와 다음 두 부분 으로 구성 되 어 있 습 니 다.
application-specific content and associated MIME headers.
MIME(Multipurpose Internet Mail Extension)의 개념 은 javax.xml.soap 에 대응 하 는 클래스 가 있 습 니 다.이런 종류의 하위 클래스 라면 SOAP 로 전송 할 수 있다.
그 중에서 MIME headers 는 아래 의 http 헤더 정 보 를 대표 합 니 다.
POST /item HTTP/1.1 Host: 189.123.345.239 Content-Type: text/plain Content-Length: 200
SOAPAction: "http://ASAP.services.tfn.**.com/2010-03-01/Login"
그러나 앞에서 말 한 바 와 같이 우 리 는 이러한 string 을 직접 구성 할 필요 가 없다.SOAPMessage 는 실례 화 할 때 기본 MIMEHeader 를 자동 으로 구성 합 니 다.수정 할 수 있 습 니 다.
MimeHeaders hd = loginSOAPRequest.getMimeHeaders(); //loginSOAPRequest 는 이전에 구 조 된 SOAPMessage 입 니 다.hd.setHeader("SOAPAction", "http://ASAP.services.tfn.**.com/2010-03-01/Login";);
//이 문장 은 MIMEHeader 에 포 함 된 SOAPAction 부분 을 기본""로 지정 한 모양 으로 수정 합 니 다.
The one new piece here is that SOAP 1.1 requires the client to set a SOAPAction field in the HTTP header.
Attachment Part 는 XML 구조 가 아 닐 수 있 습 니 다.Attachment Part 에 대하 여 본 고 는 더 이상 말 할 준비 가 되 어 있 지 않다.
SOAPMessage 클래스 에는 포 함 된 대상 을 조작 하 는 데 사용 할 수 있 는 방법 이 많 습 니 다.
Message Factory 를 사용 하여 SOAPMessage 를 만 들 수 있 습 니 다.그리고 이 message 에 필요 한 정 보 를 추가 합 니 다.
SOAPMessage 는 다음 과 같이 사용 할 수 있 습 니 다.
  • create a point-to-point connection to a specified endpoint
  • create a SOAP message
  • create an XML fragment
  • add content to the header of a SOAP message
  • add content to the body of a SOAP message
  • create attachment parts and add content to them
  • access/add/modify parts of a SOAP message
  • create/add/modify SOAP fault information
  • extract content from a SOAP message
  • send a SOAP request-response message
  • SOAPPart

  • SOAPMessage 를 보면 SOAPpart 가 사실 SOAP 메시지 의 주체 라 는 것 을 알 수 있 습 니 다.
    SOAPMessage 는 SOAPpart 대상 을 포함 하고 SOAPpart 는 SOAPEnvelope 대상 을 포함 하 며 SOAPEnvelope 는 SOAPBody 와 SOAPHeader 대상 을 포함한다.다음 과 같다.     SOAPPart sp = message.getSOAPPart();
         SOAPEnvelope se = sp.getEnvelope();
         SOAPBody sb = se.getBody();
         SOAPHeader sh = se.getHeader();
    자바 API 에서 설명 한 바 와 같이 SOAPpart object,which contains information used for message routing and identification,and which can include application-specific content.위의 이러한 계층 관 계 는 SOAP 프로 토 콜 의 문법 구조 와 일치 합 니 다.
    우 리 는 편리 하 게 SOAPMessage 를 만 들 수 있다.이 때 기본 SOAPpart 가 이 SOAPMessage 에 속 합 니 다.
    그 후에 저 희 는 SOAPPArt 의 방법 으로 SOAPPArt 에 정 보 를 편리 하 게 추가 할 수 있 습 니 다.
    setContent(); //Sets the content of the SOAPEnvelope object with the data from the given Source object.
    앞의 각 대상 의 관 계 를 떠 올 리 면 알 수 있 듯 이 이 방법 은 실제로 SOAPEnvelop,SOAPBody,SOAPFault,SOAPHeader 를 설정 했다.
  • SOAPElement

  • An object representing an element of a SOAP message that is allowed but not specifically prescribed by a SOAP specification. This interface serves as the base interface for those objects that are specifically prescribed by a SOAP specification.
    즉,element 는 SOAPMessag 의 모든 element 를 대표 할 수 있 습 니 다.이 element 가 SOAPBody 든 SOAPEnvelope 든.
  • SOAPFactory
  • SOAPFactory is a factory for creating various objects that exist in the SOAP XML tree. SOAPFactory can be used to create XML fragments that will eventually end up in the SOAP part.
    따라서 SOAPFactory 는 XML tree 를 읽 고 해당 하 는 SOAPElement 를 만 드 는 데 사용 할 수 있 습 니 다.이후 SOAPElement 를 SOAPMessage 에 편리 하 게 추가 할 수 있다.
  • Transformer

  • 네,그것 도 좋아요.
    javax.xml.soap 의 또 다른 중요 한 클래스 입 니 다.실제 응용 프로그램 에서 우 리 는 이 종 류 를 자주 사용 하여 특정한 XML 대상 을 DOM 대상 으로 변환 할 것 이다.
    SOAPElement parent = SOAPFactory.newInstance().createElement("dummy");
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
    transformer.transform(new DOMSource(doc), new DOMResult(parent));//XML 대상 doc 를 DOM 대상 으로 변환
    return (SOAPElement) parent.getChildElements().next();//모든 childElement 를 가 져 오고 되 돌려 줍 니 다.
    Transformer 대상 과 SOAPElement 대상 을 사용 하면 XML 을 읽 고 SOAPElement 으로 전환 한 후 SOAPMessage 로 구성 하여 최종 적 으로 웹 서비스 에 보 낼 수 있 습 니 다.
    DOM 대상 을 XML 대상 으로 변환 한 후 출력 할 수도 있다.
    다음은 SOAP 를 사용 하 는 아주 간단 한 예제 입 니 다.파일 에서 SOAP message 를 읽 고 웹 서비스 에 보 냅 니 다.
        //Send SOAP request messages and then return response.     public SOAPMessage sendSOAPMessage() throws Exception {          
            //공장 으로 SOAPMessage 만 들 기        MessageFactory mf = MessageFactory.newInstance()  ;         SOAPMessage loginSOAPRequest = mf.createMessage();
            //이 SOAPMessge 에 정 보 를 추가 합 니 다.출처 는 d:\\testsoap.txt 파일 입 니 다.내용 은 표준 SOAP 입 니 다.        SOAPPart sp = loginSOAPRequest.getSOAPPart();         StreamSource prepMsg = new StreamSource(                     new FileInputStream("d:\\testSOAP.txt"));                     sp.setContent(prepMsg);                    final String SOAPACTION = "http://ASAP.services.tfn.thomson.com/2010-03-01/Login";         //SOAPAction 을 MimeHeaders 에 추가 하기        MimeHeaders hd = loginSOAPRequest.getMimeHeaders();         hd.setHeader("SOAPAction", SOAPACTION);
            //위의 변경 사항 저장        loginSOAPRequest.saveChanges();         //SOAPConnection 을 만 듭 니 다.구체 적 인 방법 은 앞에서 SOAPConnection 에서 말씀 드 렸 어 요.                  SOAPConnection con = getSoapConnection();         //SOAP request 보 내기.여기 목표 주 소 는 웹 서비스 나 tornado 일 수 있 습 니 다.        SOAPMessage reply = con.call(loginSOAPRequest, new URL("http://172.28.85.6/ASAPService/ASAPService_V1.svc"));         con.close();         //복귀 응답        return reply;             }
    다음 방법 은 SOAPMessage 를 받 고 그 내용 을 파일 에 인쇄 합 니 다.이에 따라 구 조 된 SOAPMessage 가 자신 이 원 하 는 내용 인지 확인 할 수 있다.
    private void getSoapContent(SOAPMessage loginSOAPRequest) {           String output = "d:\\test1.txt";           try{           FileOutputStream tt = new FileOutputStream(output);           loginSOAPRequest.writeTo(tt);           tt.toString();           }catch(Exception e){               System.out.println();           }                  }
    알 아야 할 명사:
    SAAJ refer to SOAP with Attachments API for JavaTM (SAAJ)
    기타 참고 할 수 있 는 코드 예 는:
    http://technet.rapaport.com/Info/Prices/SampleCode/Java_Webservice_Example.aspx http://java.boot.by/wsd-guide/ch05s04.html

    좋은 웹페이지 즐겨찾기