JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java Application Framework » Spring  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 Spring Remote Services part2 Web services using SOAP
uxa





發文: 489
積分: 6
於 2007-12-14 14:13 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
小弟又來分享囉...這次是Spring Remote Service Web services using SOAP and JAX-RPC
和部署RMI一樣,先建立出interface
1
2
3
4
5
6
7
8
9
10
package com.springinaction.chapter08;
 
import javax.jws.WebMethod;
import javax.jws.WebService;
 
public interface CitationService {
 
  String getCitation();
}
 


實作部份,簡單地回傳固定字串
1
2
3
4
5
6
7
8
9
10
11
12
13
package com.springinaction.chapter08;
 
import javax.jws.WebService;
 
public class CitationServiceImpl implements CitationService {
 
  public String getCitation() {
    // TODO Auto-generated method stub
    return "Citation";
  }
 
}
 


接下來看到Spring的設定
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  <!-- declare WebService with XFire   -->
  <import resource="classpath:org/codehaus/xfire/spring/xfire.xml" />
 
  <bean id="citationService.xfire"
    class="org.codehaus.xfire.spring.remoting.XFireExporter">
    <property name="serviceFactory" ref="xfire.serviceFactory"></property>
    <property name="xfire" ref="xfire"></property>
    <property name="serviceBean" ref="citationService"></property>
    <property name="serviceClass"
      value="com.springinaction.chapter08.CitationService">
    </property>
    <property name="namespace"
      value="http://localhost:8080/SpringInAction/ws/citation">
    </property>
  </bean>


xfire.serviceFactory及xfire這兩個被reference的bean是由XFire所提供,import時會載入
接下來要設定Spring MVC DispatcherServlet的部份,請看以下web.xml的部份
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  <servlet>
    <servlet-name>citation</servlet-name>
    <servlet-class>
      org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>
        /WEB-INF/classes/com/springinaction/chapter08/ApplicationContext.xml
      </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>citation</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>


由於利用DispatcherServlet在Spring設定中要有相對應的url mapping,設定如下
1
2
3
4
5
6
7
8
9
  <!-- DispatchServlet mapping   -->
  <bean id="urlMapping"
    class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
      <props>
        <prop key="/ws/citation">citationService.xfire</prop>
      </props>
    </property>
  </bean>


完成後可以來測試一下這個Web Service是否成功部署了,開啟IE直接打入http://localhost:8080/SpringInAction/ws/citation?wsdl


reply to postreply to post
☆只要努力就有希望☆
作者 Re:Spring Remote Services part2 Web services using SOAP [Re:uxa]
uxa





發文: 489
積分: 6
於 2007-12-17 15:24 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
介紹過Spring + XFire來部署Web Service,接下來就是Client端的應用囉

其實非常的簡單,首先看到Spring的設定
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
 
  <bean id="citationService"
    class="org.codehaus.xfire.spring.remoting.XFireClientFactoryBean">
    <property name="wsdlDocumentUrl"
      value="http://localhost:8080/SpringInAction/ws/citation?wsdl">
    </property>
    <property name="serviceInterface"
      value="com.springinaction.chapter08.CitationService">
    </property>
  </bean>
 
</beans>


給定WSDL的位置以及interface就能夠存取到這個Web Service了,非常的簡單
再來測試一下存取這個Web Service吧
1
2
3
    ApplicationContext ctx = new ClassPathXmlApplicationContext("com/springinaction/chapter08/springWebServiceClientXFire.xml");
    CitationService service = (CitationService)ctx.getBean("citationService");
    System.out.println(service.getCitation());


由於部署的Web Service只是簡單的回傳字串,所以這裡小弟只是將其打印出來。
以上就是Spring + XFire的應用啦,希望對各位會有些幫助^^


reply to postreply to post
☆只要努力就有希望☆
作者 Re:Spring Remote Services part2 Web services using SOAP [Re:uxa]
chih0812





發文: 629
積分: 0
於 2009-06-16 15:38 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
uxa wrote:
介紹過Spring + XFire來部署Web Service,接下來就是Client端的應用囉

給定WSDL的位置以及interface就能夠存取到這個Web Service了,非常的簡單





請問這裡的 wsdl 是本機的 ip,假如我是要呼叫遠端的 web service 的話,也是這樣用嗎?

另外,interface 是上面就有寫過,如果我要呼叫的是遠端,事先並沒有 serviceInterface,
這是要看 wsdl 裡的東西,自己寫嗎?


reply to postreply to post
SCJP, SCWCD, SCBCD, NCLP, OCUP
作者 Re:Spring Remote Services part2 Web services using SOAP [Re:chih0812]
chih0812





發文: 629
積分: 0
於 2009-06-16 17:59 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我寫了一個簡單的 HelloWS (web service) 在遠端

然後在本機設定
1
2
3
4
5
6
7
8
9
10
11
  <bean id="citationService"
    class="org.codehaus.xfire.spring.remoting.XFireClientFactoryBean">
    
    <property name="wsdlDocumentUrl"
      value="http://172.16.38.191:7001/NotificationWS/HelloWS?WSDL">
    </property>
    
    <property name="serviceInterface"
      value="com.springinaction.chapter08.CitationService">
    </property>
  </bean>


然後有在 com.springinaction.chapter08 的 package 寫上這個 web service 的 method , code 如下:
1
2
3
4
5
6
package com.springinaction.chapter08;
 
public interface CitationService {
 
  String sayHello();
}


但是在啟動 weblogic 或 publish 時,會有下面 exception...
1
2
3
4
5
6
7
org.codehaus.xfire.XFireRuntimeException: Could not find port type {http://temp}HelloWSServicePortType
  at org.codehaus.xfire.wsdl11.parser.WSDLServiceConfigurator.setWrapped(WSDLServiceConfigurator.java:138)
  at org.codehaus.xfire.wsdl11.parser.WSDLServiceConfigurator.<init>(WSDLServiceConfigurator.java:83)
  at org.codehaus.xfire.service.binding.ObjectServiceFactory.create(ObjectServiceFactory.java:243)
  at org.codehaus.xfire.service.binding.ObjectServiceFactory.create(ObjectServiceFactory.java:193)
  at org.codehaus.xfire.spring.remoting.XFireClientFactoryBean.makeClient(XFireClientFactoryBean.java:504)
  Truncated. see log file for complete stacktrace


(temp 是我在遠端的 web service 的 package name)
請問是少了什麼設定嗎?


reply to postreply to post
SCJP, SCWCD, SCBCD, NCLP, OCUP
» JWorld@TW »  Java Application Framework » Spring

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8