WebService 단순 응용

서버 쪽 작성
1. my eclipes 에 웹 서 비 스 를 새로 만 듭 니 다. my webservice
2. 웹. xml 설정
   
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <servlet>
    <servlet-name>XFireServlet</servlet-name>
    <servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>
    <load-on-startup>0</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>XFireServlet</servlet-name>
    <url-pattern>/services/*</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

3. 새 인터페이스 클래스 Helloworldservice

	package com.web.service;

	public interface Helloworldservice {
		public String sayHellow(String str);
	}

4. 새로운 구현 클래스 Helloworldservice

	package com.web.impl;

	import com.web.service.Helloworldservice;

	public class Helloworldimpl implements Helloworldservice {

		public String sayHellow(String str) {
			return "  ,"+str+"!";
		}

	}

5. services. xml 설정

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
<service>
<!--    -->
<name>hellow</name>
<namespace>gjy</namespace>
<!--    -->
<serviceClass>com.gjy.inter.Helloworldservice</serviceClass>
<!--     -->
<implementationClass>com.gjy.impl.Helloworldimpl</implementationClass>
</service>
</beans>

이 서버 의 작성 이 완료 되 었 습 니 다.
tomcat 에 my webservice 를 배치 하고 tomcat 를 시작 하여 브 라 우 저 에 입력 합 니 다.http://localhost:8088/mywebservice/services/hellow?wsdl, 리 턴, 다음 과 같은 내용 이 나타 나 서버 측 이 성공 적 으로 작 성 했 음 을 증명 합 니 다.

  <?xml version="1.0" encoding="UTF-8" ?> 
- <wsdl:definitions targetNamespace="gjy" xmlns:soapenc12="http://www.w3.org/2003/05/soap-encoding" xmlns:tns="gjy" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
- <wsdl:types>
- <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="gjy">
- <xsd:element name="sayHellow">
- <xsd:complexType>
- <xsd:sequence>
  <xsd:element maxOccurs="1" minOccurs="1" name="in0" nillable="true" type="xsd:string" /> 
  </xsd:sequence>
  </xsd:complexType>
  </xsd:element>
- <xsd:element name="sayHellowResponse">
- <xsd:complexType>
- <xsd:sequence>
  <xsd:element maxOccurs="1" minOccurs="1" name="out" nillable="true" type="xsd:string" /> 
  </xsd:sequence>
  </xsd:complexType>
  </xsd:element>
  </xsd:schema>
  </wsdl:types>
- <wsdl:message name="sayHellowRequest">
  <wsdl:part name="parameters" element="tns:sayHellow" /> 
  </wsdl:message>
- <wsdl:message name="sayHellowResponse">
  <wsdl:part name="parameters" element="tns:sayHellowResponse" /> 
  </wsdl:message>
- <wsdl:portType name="hellowPortType">
- <wsdl:operation name="sayHellow">
  <wsdl:input name="sayHellowRequest" message="tns:sayHellowRequest" /> 
  <wsdl:output name="sayHellowResponse" message="tns:sayHellowResponse" /> 
  </wsdl:operation>
  </wsdl:portType>
- <wsdl:binding name="hellowHttpBinding" type="tns:hellowPortType">
  <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> 
- <wsdl:operation name="sayHellow">
  <wsdlsoap:operation soapAction="" /> 
- <wsdl:input name="sayHellowRequest">
  <wsdlsoap:body use="literal" /> 
  </wsdl:input>
- <wsdl:output name="sayHellowResponse">
  <wsdlsoap:body use="literal" /> 
  </wsdl:output>
  </wsdl:operation>
  </wsdl:binding>
- <wsdl:service name="hellow">
- <wsdl:port name="hellowHttpPort" binding="tns:hellowHttpBinding">
  <wsdlsoap:address location="http://localhost:8088/mywebservice/services/hellow" /> 
  </wsdl:port>
  </wsdl:service>
  </wsdl:definitions>

클 라 이언 트 작성
1. 일반적인 자바 프로젝트 Webwervice Client 를 새로 만 들 고 XFire 의 jar 패 키 지 를 도입 합 니 다.
2. 클 라 이언 트 코드 작성


package com.web.client;

import java.net.MalformedURLException;
import java.net.URL;

import org.codehaus.xfire.client.Client;

public class HelloworldClient {
	public static void main(String args[]){
		 try {
			   Client client=new Client(new URL("http://localhost:8088/mywebservice/services/hellow?wsdl"));
			   Object[] result=client.invoke("sayHellow", new Object[]{"   "});
			   System.out.println(result[0]);
			  } catch (MalformedURLException e) {
			   e.printStackTrace();
			  } catch (Exception e) {
			   e.printStackTrace();
			}

	}
}

서버 시작, 클 라 이언 트 실행, 결과: 안녕하세요, 공 선생님!
이로써 간단 한 웹 서비스 응용 개발 이 완료 되 었 습 니 다.

좋은 웹페이지 즐겨찾기