WebService CXF 사용 예

6266 단어 webservice
웹 서 비 스 를 배 워 서 참고 할 수 있 도록 작은 예 를 들 었 습 니 다.
 
  1. apache - cxf - 2.2.12. zip 패키지 다운로드, 주소: http://cxf.apache.org/
  2. 자바 프로젝트 를 만 들 고 관련 jar 패 키 지 를 가 져 옵 니 다.
 
  3. 인 터 페 이 스 를 만 듭 니 다 (설명 을 사용 하여 웹 서비스 @ WebService)
   
package test;

import javax.jws.WebService;

@WebService
public interface HelloWorld {

    public String sayHello(String name);

}

 
4. 만 든 인터페이스 구현
 
package test;

import javax.jws.WebService;

@WebService
public class HelloWorldImpl implements HelloWorld {
	public String sayHello(String name) {
		// TODO Auto-generated method stub
		System.out.println("     !");
		return "Hello "+name+" ! ";
	}

}

 
 5. WebService 서버 엔 드 만 들 기
 
  
package test;

import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

public class MainServer {

            /**
               *        
          */
             public static void main(String[] args) {
                 //     WebService      
           JaxWsServerFactoryBean factory =new JaxWsServerFactoryBean();
                 //  webService     ,     
           factory.setServiceClass(HelloWorldImpl.class);  
	 //  webServer     
	factory.setAddress("http://localhost:9001/HelloWorld");
		
	//  Server,   
	Server server= factory.create() ;
	server.start();
            }

}

    서버 엔 드 프로그램 시작, IE 브 라 우 저 중 http://localhost:9001/HelloWorld?WSDL 
     서버 쪽 정상
    
<?xml version="1.0" encoding="UTF-8" ?> 
- <wsdl:definitions name="HelloWorldImplService" targetNamespace="http://test/" xmlns:ns1="http://schemas.xmlsoap.org/wsdl/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://test/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <wsdl:types>
- <xsd:schema attributeFormDefault="unqualified" elementFormDefault="unqualified" targetNamespace="http://test/" xmlns:tns="http://test/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="sayHello" type="tns:sayHello" /> 
- <xsd:complexType name="sayHello">
- <xsd:sequence>
  <xsd:element minOccurs="0" name="arg0" type="xsd:string" /> 
  </xsd:sequence>
  </xsd:complexType>
  <xsd:element name="sayHelloResponse" type="tns:sayHelloResponse" /> 
- <xsd:complexType name="sayHelloResponse">
- <xsd:sequence>
  <xsd:element minOccurs="0" name="return" type="xsd:string" /> 
  </xsd:sequence>
  </xsd:complexType>
  </xsd:schema>
  </wsdl:types>
- <wsdl:message name="sayHello">
  <wsdl:part element="tns:sayHello" name="parameters" /> 
  </wsdl:message>
- <wsdl:message name="sayHelloResponse">
  <wsdl:part element="tns:sayHelloResponse" name="parameters" /> 
  </wsdl:message>
- <wsdl:portType name="HelloWorld">
- <wsdl:operation name="sayHello">
  <wsdl:input message="tns:sayHello" name="sayHello" /> 
  <wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse" /> 
  </wsdl:operation>
  </wsdl:portType>
- <wsdl:binding name="HelloWorldImplServiceSoapBinding" type="tns:HelloWorld">
  <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> 
- <wsdl:operation name="sayHello">
  <soap:operation soapAction="" style="document" /> 
- <wsdl:input name="sayHello">
  <soap:body use="literal" /> 
  </wsdl:input>
- <wsdl:output name="sayHelloResponse">
  <soap:body use="literal" /> 
  </wsdl:output>
  </wsdl:operation>
  </wsdl:binding>
- <wsdl:service name="HelloWorldImplService">
- <wsdl:port binding="tns:HelloWorldImplServiceSoapBinding" name="HelloWorldImplPort">
  <soap:address location="http://localhost:9001/HelloWorld" /> 
  </wsdl:port>
  </wsdl:service>
  </wsdl:definitions>

 
 
  6. webService 클 라 이언 트 만 들 기
 
  
package test;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

public class HelloClient {

	/**
	 *   WebService   
	 */
	public static void main(String[] args) {
		//        (   )
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();	
		factory.setAddress("http://localhost:9001/HelloWorld");
		//         
		factory.setServiceClass(HelloWorld.class);
		
		HelloWorld hello= (HelloWorld)factory.create();
		System.out.println(hello.sayHello("   "));
 
	}

}

 
 
  마지막 으로 서버 엔 드 프로그램 MainServer 를 시작 하면 클 라 이언 트 HelloClient 가 표 시 됩 니 다.
 
2011-7-24 17:42:00 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
  : Creating Service {http://test/}HelloWorldService from class test.HelloWorld
Hello     ! 

   이때 WebService 가 성공 적 으로 발표 되 었 습 니 다!

좋은 웹페이지 즐겨찾기