0

我已经用成功创建了 WSDL 的 java 编写了 Webservice。我被困在用 java 为我的 web 服务编写 web 服务客户端。我想从一些 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
4

2 回答 2

0

首先,从 ClassNotFoundException 中可以明显看出,您缺少 CXF 罐子。请包括 cxf 罐子。

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

于 2013-04-25T12:51:38.737 回答
0

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

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

于 2013-04-25T14:54:07.413 回答