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 가 성공 적 으로 발표 되 었 습 니 다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
java가 클라이언트를 통해 서버 웹 서비스에 접근하는 방법본고는 자바가 클라이언트를 통해 서버 웹 서비스에 접근하는 방법을 실례로 설명한다.다음과 같이 여러분에게 참고할 수 있도록 공유합니다. 자바 관련 내용에 관심이 있는 더 많은 독자들은 본 사이트의 주제를 볼 수 있습...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.