`

Web Services from a Spring Enabled Web-App

阅读更多
I spent some time recently working on an web-application using spring and webwork. (<tangent>this combination created the core of one of the cleanest architectures I've worked with</tangent>). Someone asked how easy it might be to expose some functionality as a web service, so I thought I'd give it a go.

Grabbing a copy of Apache Axis was my first step. It's documentation guided me most of the way, but the only missing bit was how to get at a spring-created bean from an axis-aware class. Here's what I did.

After putting the required libraries (axis-ant.jar, axis.jar, commons-discovery.jar, commons-loggin.jar, jaxrpc.jar, wsdl4j.jar, saaj.jar) on the classpath, I had to declare all of the appropriate axis stuff in my web.xml file.

<web-app>
    <listener>
        <listener-class>org.apache.axis.transport.http.AxisHTTPSessionListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>axis</servlet-name>
        <servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>axis</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>

    <mime-mapping>
        <extension>wsdl</extension>
        <mime-type>text/xml</mime-type>
    </mime-mapping>
</web-app>

The next step was to create a class which would be exposed as a web service by Axis:

public class TodoItemWebService {
    public String getTodoItemDescription(String title) {
        return "dummy description";
    }
}

To get the service to run, I also had to create a file named server-config.wsdd in the WEB-INF directory of my web application. I figured out what this looked like by going through the Axis tutorial which dynamically registers a service in your app. Statically creating the file works fine though too. Here's what mine looked like:

<?xml version="1.0" encoding="UTF-8"?>
<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
    <handler name="LocalResponder" type="java:org.apache.axis.transport.local.LocalResponder"/>
    <handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper"/>
    <handler name="Authenticate" type="java:org.apache.axis.handlers.SimpleAuthenticationHandler"/>

    <service name="items" provider="java:RPC" style="wrapped" use="literal">
        <operation name="getTodoItemDescription" qname="ns1:getTodoList" returnQName="ns1:getTodoItemDescriptionResult"
            returnType="xsd:string" soapAction="" xmlns:ns1="http://wrytradesman.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
            <parameter qname="ns1:TodoItemTitle" type="xsd:string"/>
        </operation>
        <parameter name="allowedMethods" value="*"/>
        <parameter name="className" value="com.wrytradesman.todolist.web.service.TodoItemWebService"/>
        <parameter name="wsdlTargetNamespace" value="http://wrytradesman.com/"/>
    </service>

    <transport name="http">
        <requestFlow>
            <handler type="URLMapper"/>
            <handler type="java:org.apache.axis.handlers.http.HTTPAuthHandler"/>
        </requestFlow>
        <parameter name="qs:list" value="org.apache.axis.transport.http.QSListHandler"/>
        <parameter name="qs:wsdl" value="org.apache.axis.transport.http.QSWSDLHandler"/>
        <parameter name="qs.list" value="org.apache.axis.transport.http.QSListHandler"/>
        <parameter name="qs.method" value="org.apache.axis.transport.http.QSMethodHandler"/>
        <parameter name="qs:method" value="org.apache.axis.transport.http.QSMethodHandler"/>
        <parameter name="qs.wsdl" value="org.apache.axis.transport.http.QSWSDLHandler"/>
    </transport>

    <transport name="local">
        <responseFlow>
            <handler type="LocalResponder"/>
        </responseFlow>
    </transport>
</deployment>

The service definition in bold is what I had to write to get the service running. All of the surrounding info is setup stuff that is just needed to configure axis. At this stage of the game, I could deploy my web application and hit a url http://localhost//services/items?wsdl to get the WSDL definition for my service. In the space of about 5 minutes, a cow-orker had used that WSDL to create a .NET based client which called the service and got my hard-coded response.

With my web service now up and running, the only remaining problem was to create a "real" implementation that used one of my spring managed objects instead of a hard-coded response. The trick here is to get a reference to the application context first. Everything from that point is easy. The simplest path to that is get a reference to the servlet from the Axis message context, then use the servlet context to get the application context from spring. Then just call your spring bean as per normal. Here's what my final code looks like:

public class TodoItemWebService {
    public String getTodoItemDescription(String title) {
        HttpServlet servlet = (HttpServlet) MessageContext.getCurrentContext().getProperty(
                HTTPConstants.MC_HTTP_SERVLET);
        ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servlet.getServletContext());

        Map beans = context.getBeansOfType(TodoListRepository.class, false, false);
        Iterator iterator = beans.values().iterator();
        TodoListRepository repository = (TodoListRepository) iterator.next();

        TodoItem item = repository.getTodoList().getItem(title);
        return item.getDescription();
    }
}

Thats it. Pretty straight forward really. There's a few corner cases to be covered off, but vertical slice is complete. The whole exercise took less than a days effort, which included learning about how Axis hangs together.

Posted by marty at December 14, 2004 10:42 AM [0 Trackbacks]
Comments

Another option is to extend org.springframework.remoting.jaxrpc.ServletEndpointSupport.

I'm using this to insert a level of indirection, where the Axis-registered service class delegates to a bean it retrieves from the application context.

import org.springframework.context.ApplicationContext;
import org.springframework.remoting.jaxrpc.ServletEndpointSupport;


public class JaxRpcVerzeichnisService extends ServletEndpointSupport
implements RemoteMyService {

private MyService _service;

protected void onInit() {
ApplicationContext ac = getWebApplicationContext();
assert ac != null : "no ApplicationContext available";
_service = (MyService)ac.getBean("myservice");
}

public Object myServiceMethod() {
assert _service != null : "_service not initialized";
return _service.myServiceMethod();
}
}

Posted by: Michael Schuerig at December 14, 2004 08:50 PM

THANK YOU! Man I started looking at this last Friday and was dreading the fight... you've made it easy. Thanks VERY VERY much!

R

Posted by: Robert S. Sfeir at December 15, 2004 01:24 AM

you might also take a look at:

http://opensource.atlassian.com/projects/spring/browse/SPR-371

Posted by: The VoodooChile at December 15, 2004 11:53 PM

Interessting options. It would be really helpful to have an official route documented in the Spring docs but i cant find any Webservice-Section at all.

The proposed url of "The VoodooChile" is quite promising though and i think i will take this route. But great blog on this topic, i appreciated the reading.

Marc

Posted by: Marc Logemann at January 27, 2005 01:09 AM

This code doesn't work- specifically

1. what's TodoItem?
2. what's TodoListRepository?

Appreciate your response.

Thx.
lkj


ublic class TodoItemWebService {
public String getTodoItemDescription(String title) {
HttpServlet servlet = (HttpServlet) MessageContext.getCurrentContext().getProperty(
HTTPConstants.MC_HTTP_SERVLET);
ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servlet.getServletContext());

Map beans = context.getBeansOfType(TodoListRepository.class, false, false);
Iterator iterator = beans.values().iterator();
TodoListRepository repository = (TodoListRepository) iterator.next();

TodoItem item = repository.getTodoList().getItem(title);
return item.getDescription();
}
}

Posted by: lalit at January 27, 2005 09:49 AM
分享到:
评论

相关推荐

    使用Spring Boot和Python Flask实现AI文本分类服务注册与发现

    import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController public class ClientController { @Autowired private Discovery...

    param-store-config-demo:使用Spring Cloud AWS演示与AWS Param Store的集成

    Spring Cloud AWS是一个流行的开源项目,它为Spring Boot应用程序提供了与Amazon Web Services (AWS)的集成。在本示例中,我们将利用Spring Cloud AWS的配置客户端来从Parameter Store检索配置信息,而不是直接在...

    Grails帮助文档

    Grails是一个全栈框架,它整合了Spring、Hibernate、Groovy on Grails(GORM)等成熟的技术,提供了MVC(模型-视图-控制器)架构,使得开发人员能够快速构建动态、数据库驱动的Web应用。 ### 2. Groovy语言 Groovy...

    SSO单点登录概要设计说明书.doc

    SSO单点登录概要设计说明书.doc

    奥塔北煤矿6.0 Mta新井设计说明书.docx

    奥塔北煤矿6.0 Mta新井设计说明书.docx

    基于S7-200 PLC和组态王的喷泉控制系统设计与实现

    内容概要:本文详细介绍了基于S7-200 PLC和组态王软件构建喷泉控制系统的全过程。首先明确了系统的IO分配,包括启动按钮、停止按钮以及喷泉水泵的连接方式。接着展示了梯形图程序的设计,涵盖了基本的启停控制逻辑、定时循环和模式切换机制。随后提供了详细的接线图原理图,解释了输入输出部分的具体接线方法。最后讲述了组态王的画面设计,包括创建工程、定义变量和绘制监控界面等步骤。此外还分享了一些调试过程中遇到的问题及解决方案。 适合人群:对自动化控制感兴趣的初学者和技术人员,尤其是那些希望深入了解PLC编程和人机界面设计的人群。 使用场景及目标:适用于小型喷泉项目的实际控制系统开发,旨在帮助读者掌握PLC编程技巧、熟悉组态软件的应用,并能够独立完成类似的自动化控制系统设计。 其他说明:文中不仅包含了理论知识讲解,还附带了许多实践经验分享,如硬件配置建议、常见错误规避措施等,有助于提高实际操作能力。

    计算机二级PPT精选二十套(标红).jpg

    计算机二级PPT精选二十套(标红)

    保险公司IT变更管理流程设计说明书.doc.doc

    保险公司IT变更管理流程设计说明书.doc.doc

    毕业设计说明书A江坝后式厂房双曲拱坝设计.pdf

    毕业设计说明书A江坝后式厂房双曲拱坝设计.pdf

    《计算机二级MS精选300道选择题.docx》

    内容概要:文档《计算机二级MS精选300道选择题.docx》涵盖了计算机二级考试中Microsoft Office软件(Word、Excel、PowerPoint)及计算机基础知识的选择题。题目涉及软件操作技巧、功能应用、常见问题解决等方面,旨在帮助考生熟悉并掌握相关知识点,提高应试能力。文档内容详尽,涵盖面广,从基础操作到高级应用均有涉及,适合考生全面复习备考。 适用人群:准备参加计算机二级考试的学生及相关从业人员,特别是需要强化Office软件操作技能和计算机基础知识的人员。 使用场景及目标:①考生可以在复习过程中使用这些选择题进行自我检测,巩固所学知识;②教师可以将其作为教学辅助材料,帮助学生更好地理解和掌握课程内容;③培训机构可以用这些题目作为测试题库,评估学员的学习效果。 其他说明:文档不仅提供了大量的选择题,还详细解析了每道题目的答案,有助于考生深入理解知识点。此外,题目内容紧跟最新考试大纲,确保考生能够获得最有效的备考资料。

    基于多目标黏菌优化算法(MOSMA)的SVM参数优化及其在回归预测中的应用

    内容概要:本文介绍了一种创新的方法,利用多目标黏菌优化算法(MOSMA)来优化支持向量机(SVM)的参数C和gamma,从而提高回归预测的效果。首先详细解释了MOSMA的工作原理,包括黏菌权重更新、快速非支配排序以及自适应参数调整等关键技术点。接着展示了具体的Python代码实现,涵盖数据预处理、适应度函数定义、参数更新规则等方面。实验结果显示,在风电功率预测等多个应用场景中,相较于传统的网格搜索方法,MOSMA能够更快更有效地找到最优参数组合,显著提升了预测性能。 适合人群:从事机器学习研究或应用开发的技术人员,尤其是关注SVM参数优化及回归预测领域的从业者。 使用场景及目标:适用于需要进行高效参数寻优的回归预测任务,如风电功率预测、设备负载预测等。主要目标是通过改进SVM参数配置,获得更高的预测精度和更好的泛化能力。 其他说明:文中提供了完整的代码示例和详细的实施步骤指导,帮助读者快速理解和应用这一先进的优化技术。此外,还讨论了一些常见的注意事项和技术细节,如数据标准化、参数范围设定、并行化改造等。

    毕业设计 某油库设计说明书.pdf

    毕业设计 某油库设计说明书.pdf

    Q235钢板焊接工艺设计说明书.docx

    Q235钢板焊接工艺设计说明书.docx

    75t循环流化床锅炉设计说明书.doc

    75t循环流化床锅炉设计说明书.doc

    (最新修订版)直列四缸柴油机配气机构设计毕业论文设计说明书.doc

    (最新修订版)直列四缸柴油机配气机构设计毕业论文设计说明书.doc

    DeepSeek大模型生态报告:中国AI技术突破及其全球影响分析

    内容概要:《deepseek大模型生态报告 2025年2月》详细介绍了DeepSeek大模型的背景、应用现状、技术特点及其产业生态。DeepSeek由杭州深度求索公司创立,通过一系列技术创新,如多层注意力架构(MLA)、FP8混合精度训练框架、DualPipe跨节点通信等,显著降低了训练成本和提高了模型性能。DeepSeek在国内和国际市场迅速崛起,登顶苹果应用商店免费APP下载排行榜,并被多家企业和机构接入,包括华为、三大运营商、微软、英伟达等。其开源策略和低成本优势对全球科技供应链和资本市场产生了深远影响,尤其是在AI领域。 适合人群:对人工智能、大模型技术感兴趣的科技爱好者、研究人员、企业家及政策制定者。 使用场景及目标:①了解DeepSeek大模型的技术创新和应用现状;②探讨DeepSeek对全球AI产业生态的影响;③分析DeepSeek在不同行业(如金融、医疗、教育、制造等)的具体应用案例。 其他说明:报告还涵盖了各国政府及相关机构对DeepSeek的态度和政策回应,以及DeepSeek对未来AI技术发展和国际竞争格局的启示。此外,报告深入剖析了DeepSeek在技术架构、数据策略和工程实践方面的突破,揭示了其成功背后的组织文化和创新机制。

    基于粒子群算法的电动汽车区域综合能源系统三方定价优化研究

    内容概要:本文详细介绍了利用粒子群算法解决电动汽车区域综合能源系统中光伏电站、充电桩运营商和电网公司在定价上的三方博弈问题。通过MATLAB代码实现了粒子群算法的具体应用,包括参数设置、适应度函数设计、粒子更新策略以及结果可视化。文中展示了如何将三方定价变量编码成多维粒子,并通过目标函数计算和约束处理确保粒子在合理的解空间内搜索最优解。最终得到的电价曲线反映了不同时间段内的供需变化,验证了算法的有效性和实用性。 适合人群:从事能源系统优化、智能算法应用的研究人员和技术开发者。 使用场景及目标:适用于需要进行能源系统优化调度的场景,特别是涉及多方利益协调的问题。目标是找到光伏电价、充电桩电价和电网电价的最佳组合,使得三方利益达到最优平衡。 其他说明:建议在调试过程中关注特定时段的电价突变,适当调整参数如社会认知系数和社会学习因子,以获得更好的收敛效果。此外,初始粒子的位置选择对收敛速度有很大影响,推荐采用高斯扰动等方法进行初始化。

    WY02锥齿轮的加工工艺规程及工时定额计算 课程设计说明书.docx

    WY02锥齿轮的加工工艺规程及工时定额计算 课程设计说明书.docx

    项目管理制度范文.docx

    项目管理制度范文.docx

    风电工程技术:DFIG双馈风力发电机仿真模型解析与应用

    内容概要:本文深入探讨了双馈风力发电机(DFIG)的仿真建模及其关键技术模块。首先介绍了最大功率跟踪(MPPT)模块的工作原理,利用爬山算法优化风能利用率。接着详细讲解了转子侧变流器的矢量控制,包括坐标变换、PI调节器参数设定以及抗饱和处理。文中还讨论了网侧变流器的直流电压控制方法,强调了双闭环结构的重要性,并分享了低电压穿越仿真的实践经验。此外,文章详细解释了功率解耦控制和变速恒频实现的技术细节,提供了丰富的故障案例和调试技巧。 适合人群:从事风电工程研究和技术开发的专业人士,尤其是对DFIG仿真建模感兴趣的工程师和研究人员。 使用场景及目标:适用于希望深入了解DFIG内部机制的研究人员,帮助他们掌握从基本原理到复杂控制策略的设计与实现。同时,也为实际工程项目提供宝贵的调试经验和故障排除指南。 其他说明:文章不仅涵盖了理论分析,还包括大量实用的代码片段和具体的参数配置建议,确保读者能够顺利构建并运行仿真模型。

Global site tag (gtag.js) - Google Analytics