我能够成功编写javax.jws.WebService。但是,我不确切地知道如何在java中编写相同的Web服务客户端

时间:2013-04-25 05:59:47

标签: web-services spring jaxb webservice-client

我已经在java中编写了Webservice,它已成功创建了WSDL。我一直在为java中的webservice编写一个webservice客户端。我想从一些jsp类中使用我的web服务。我该怎么做?

@WebService
public interface AddService {
    double getMultipicationResult(double M1, double M2);
}

 @WebService(endpointInterface = "com.sample.AddService")
    public class AddServiceImpl implements AddService {
        public AddServiceImpl() {
        }
        @Override
        public double getMultipicationResult(double M1, double M2) {
            M1 = M1*M2;
            return M1;
        }
    }

我写的客户端类似于: -

public class AddServiceClient {
    private AddServiceClient() {
    }
    public static void main(String args[]){
    {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"SpringClientWebServices.xml"});
        AddService  client = (AddService)context.getBean("client");
        double response = 0.0;
        response = client.getMultipicationResult(10.0, 20.5);
    }
}

和SpringClientWebServices.xml如下: -

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/jdbc
        http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd      
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">


    <bean id="client" class="com.sample.AddService" 
          factory-bean="clientFactory" factory-method="create"/>

    <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
        <property name="serviceClass" value="com.sample.AddService"/>
        <property name="address" value="http://localhost:8080/sample/services/Addition"/>
    </bean>

</beans>

我得到如下例外: -

Exception in thread "main" org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.apache.cxf.jaxws.JaxWsProxyFactoryBean] for bean with name 'clientFactory' defined in class path resource [SpringClientWebServices.xml]; nested exception is java.lang.ClassNotFoundException: org.apache.cxf.jaxws.JaxWsProxyFactoryBean

2 个答案:

答案 0 :(得分:0)

首先,从ClassNotFoundException可以看出,你错过了CXF jar。请加入cxf罐子。

关于在JSP中使用服务,然后您必须首先通过web.xml初始化Spring容器,而不是通过main方法。使用Spring MVC并实现控制器,该控制器调用webservice并向JSP提供数据。

答案 1 :(得分:0)

如果您想直接从JSP使用服务,请考虑提到的JavaScript客户端:http://cxf.apache.org/docs/javascript-client-samples.html

我更喜欢使用包含服务接口的jar并在单独的jar中使用CXF和Spring创建动态Spring客户端,然后引入这些依赖项。这也记录在CXF网站上。