Exporting beans as web services using XFire

Once XFire is in your build’s classpath, creating a web service is as simple as following these three steps:
1. Configure an XFireExporter bean to export a Spring bean as a web service.
2. Configure a Spring DispatcherServlet to handle incoming HTTP requests.
3. Configure a handler mapping so as to map DispatcherServlet-handled requests to XFireExporter-exported services.

[b](1)、定义Interface和Implement[/b]

public interface GreetService {

String sayHello(String name);

}



import org.springframework.stereotype.Repository;

@Repository("greetService")
public class GreetServiceImpl implements GreetService{

public String sayHello(String name) {
return "welcome to you, "+name;
}

}



import javax.jws.WebMethod;
import javax.jws.WebResult;
import javax.jws.WebService;

@WebService
public interface HelloService {

@WebMethod(operationName="hello")
@WebResult(name="helloWords")
String sayHello(String name);

}


import javax.jws.WebService;

import org.springframework.stereotype.Repository;

@WebService(serviceName="helloService",
endpointInterface="com.logcd.jws.HelloService")
@Repository("helloService")
public class HelloServiceImpl implements HelloService{

public String sayHello(String name) {
return "hello, " + name;
}

}


[b](2)、web.xml[/b]

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:jwsContext.xml,
classpath:xfire-servlet.xml
</param-value>
</context-param>

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

<servlet>
<servlet-name>xfire</servlet-name>
<servlet-class>
<!-- org.springframework.web.servlet.DispatcherServlet -->
org.codehaus.xfire.spring.XFireSpringServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>xfire</servlet-name>
<url-pattern>/jws/*</url-pattern>
</servlet-mapping>


[b](3)、jwsContext.xml[/b]

<context:component-scan base-package="com.logcd.jws"/>

<!-- import the XFire-defined Spring context
where xfire.serviceFactory and xfire are already declared in -->
<import resource="classpath:org/codehaus/xfire/spring/xfire.xml"/>

<!-- Configuring XFireExporter -->
<bean id="greetService.xfire"
class="org.codehaus.xfire.spring.remoting.XFireExporter">
<property name="serviceFactory"
ref="xfire.serviceFactory" />
<property name="xfire" ref="xfire" />
<property name="serviceBean"
ref="greetService" />
<property name="serviceClass"
value="com.logcd.jws.GreetService" />
</bean>

<!-- declaring web services with JSR-181 annotations -->
<bean id="webAnnotations"
class="org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations"/>

<bean id="annotationHandlerMapping"
class="org.codehaus.xfire.spring.remoting.Jsr181HandlerMapping">
<property name="typeMappingRegistry">
<ref bean="xfire.typeMappingRegistry"/>
</property>
<property name="xfire" ref="xfire" />
<property name="webAnnotations">
<ref bean="webAnnotations"/>
</property>
</bean>


[b](4)、xfire-servlet.xml(注意命名与web.xml中对应)[/b]

<!-- Mapping requests to XFireExporter -->
<bean id="handlerMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

<property name="mappings">
<props>
<prop key="greetService">greetService.xfire</prop>
<prop key="helloService">annotationHandlerMapping</prop>
</props>
</property>

<!--
<property name="urlMap">
<map>
<entry key="/">
<ref bean="annotationHandlerMapping"/>
</entry>
</map>
</property>
-->
</bean>


[b](5)、Consuming web services[/b]

import java.net.MalformedURLException;

import org.codehaus.xfire.annotations.AnnotationServiceFactory;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;

public class ConsumeService {

public static void accessJwsUseXFireAPI() throws MalformedURLException{
Service serviceModel = new ObjectServiceFactory()
.create(GreetService.class);

GreetService greetService =
(GreetService) new XFireProxyFactory().create(
serviceModel,
"http://127.0.0.1/spring_ws/jws/GreetService");

System.out.println(greetService.sayHello("bela"));
}

public static void accessJwsUseXFireAPI2() throws MalformedURLException{
Service serviceModel = new AnnotationServiceFactory()
.create(HelloService.class);

HelloService helloService =
(HelloService) new XFireProxyFactory().create(
serviceModel,
"http://127.0.0.1/spring_ws/jws/helloService");

System.out.println(helloService.sayHello("bela"));
}

public static void main(String[] args) throws Exception {
accessJwsUseXFireAPI();
accessJwsUseXFireAPI2();
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值