没有Spring WS

时间:2016-07-22 17:26:04

标签: java spring web-services maven

我刚开始使用Spring WS Cookbook练习Spring WS,但我仍然坚持使用我的第一个例子:

这是我的端点类:

/*
* This software is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND.
*/
package com.packtpub.liverestaurant.service.endpoint;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.Namespace;
import org.jdom2.xpath.XPath;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;

import com.packtpub.liverestaurant.service.OrderService;


@Endpoint
public class OrderEndPoint {
    private static final Log logger = LogFactory.getLog(OrderEndPoint.class);
    private static final String NAMESPACE_URI = "http://www.packtpub.com/liverestaurant/OrderService/schema";
    private XPath refNumberExpression;
    private XPath typeExpression;
    private XPath nameExpression;
    private XPath quantityExpression;
    private OrderService orderService;

    @Autowired
    public OrderEndPoint(OrderService orderService) throws JDOMException {
        this.orderService = orderService;
       Namespace namespace = Namespace.getNamespace("QOrder", NAMESPACE_URI);

       refNumberExpression = XPath.newInstance("//QOrder:refNumber");
       refNumberExpression.addNamespace(namespace);

       nameExpression = XPath.newInstance("concat(//QOrder:fName,' ',//QOrder:lName)");
       nameExpression.addNamespace(namespace);

       typeExpression = XPath.newInstance("//QOrder:type");
       typeExpression.addNamespace(namespace);

       quantityExpression = XPath.newInstance("//QOrder:quantity");
       quantityExpression.addNamespace(namespace);
    }

    @PayloadRoot(namespace = NAMESPACE_URI, localPart = "placeOrderRequest")
    public Element handleOrderRequest(@RequestPayload Element placeOrderRequest) throws Exception {
       String name=nameExpression.valueOf(placeOrderRequest);
       String refNumber = refNumberExpression.valueOf(placeOrderRequest);


        String repText= orderService.placeOrder(name, refNumber);

        Namespace resNamespace = Namespace.getNamespace("", NAMESPACE_URI);
        Element root = new Element("placeOrderResponse", resNamespace);
        Element echoResponse = new Element("refNumber", resNamespace);
        echoResponse.setText(repText);
        root.addContent(echoResponse);
        Document doc = new Document(root);


        return doc.getRootElement();
    }
}

这是我的弹簧配置文件:

<context:component-scan base-package="com.packtpub.liverestaurant.*" />

    <sws:annotation-driven />

    <sws:dynamic-wsdl id="OrderService" portTypeName="OrderService"
        locationUri="http://localhost:8080/LiveRestaurant_R-1.1/spring-ws/OrderService.wsdl"
        targetNamespace="http://www.packtpub.com/liverestaurant/OrderService/schema">
        <sws:xsd location="/WEB-INF/orderService.xsd" />
    </sws:dynamic-wsdl>

    <bean
        class="org.springframework.ws.soap.server.endpoint.interceptor.PayloadValidatingInterceptor">
        <property name="schema" value="/WEB-INF/orderService.xsd" />
        <property name="validateRequest" value="true" />
        <property name="validateResponse" value="true" />
    </bean>

我在web.xml中有以下配置:

<servlet>
        <servlet-name>spring-ws</servlet-name>
        <servlet-class>
            org.springframework.ws.transport.http.MessageDispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring-ws</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

我有OrderService及其实现类。

最后这是我的XSD文件:

<?xml version="1.0" encoding="UTF-8"?>
<!--
* This software is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND.
-->
<schema xmlns="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.packtpub.com/liverestaurant/OrderService/schema"
    xmlns:tns="http://www.packtpub.com/liverestaurant/OrderService/schema"
    elementFormDefault="qualified"
    xmlns:QOrder="http://www.packtpub.com/liverestaurant/OrderService/schema">

    <element name="placeOrderRequest">
        <complexType>
            <sequence>
                <element name="order" type="QOrder:Order"></element>
            </sequence>
        </complexType>
    </element>

    <element name="placeOrderResponse">
        <complexType>
            <sequence>
                <element name="refNumber" type="string"></element>
            </sequence>
        </complexType>
    </element>

    <element name="cancelOrderRequest">
        <complexType>
            <sequence>
                <element name="refNumber" type="string"></element>
            </sequence>
        </complexType>
    </element>

    <element name="cancelOrderResponse">
        <complexType>
            <sequence>
                <element name="cancelled" type="boolean"></element>
            </sequence>
        </complexType>
    </element>

  <complexType name="Order">
      <sequence>
          <element name="refNumber" type="string"></element>
          <element name="customer" type="QOrder:Customer"></element>
          <element name="dateSubmitted" type="dateTime"></element>
          <element name="orderDate" type="dateTime"></element>
          <element name="items" type="QOrder:FoodItem"
              maxOccurs="unbounded" minOccurs="1">
          </element>
      </sequence>
  </complexType>

  <complexType name="Customer">
      <sequence>
          <element name="addressPrimary" type="QOrder:Address"></element>
          <element name="addressSecondary" type="QOrder:Address"></element>
          <element name="name" type="QOrder:Name"></element>
      </sequence>
  </complexType>

  <complexType name="Name">
      <sequence>
          <element name="fName" type="string"></element>
          <element name="mName" type="string"></element>
          <element name="lName" type="string"></element>
      </sequence>
  </complexType>

  <complexType name="Address">
      <sequence>
          <element name="doorNo" type="string"></element>
          <element name="building" type="string"></element>
          <element name="street" type="string"></element>
          <element name="city" type="string"></element>
          <element name="country" type="string"></element>
          <element name="phoneMobile" type="string"></element>
          <element name="phoneLandLine" type="string"></element>
          <element name="email" type="string"></element>
      </sequence>
  </complexType>

    <simpleType name="FoodItemType">
        <restriction base="string">
            <enumeration value="Snacks"></enumeration>
            <enumeration value="Beverages"></enumeration>
            <enumeration value="Starters"></enumeration>
            <enumeration value="Meals"></enumeration>
            <enumeration value="Coffee"></enumeration>
            <enumeration value="Juices"></enumeration>
            <enumeration value="Desserts"></enumeration>
        </restriction>
    </simpleType>

    <complexType name="FoodItem">
        <sequence>
            <element name="type" type="QOrder:FoodItemType"></element>
            <element name="name" type="string"></element>
            <element name="quantity" type="double"></element>
        </sequence>
    </complexType>
</schema>

我使用“mvn clean package tomcat:run”

运行程序

现在使用SOAP UI我正在尝试发出请求,我收到的错误是:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Header/>
   <SOAP-ENV:Body>
      <SOAP-ENV:Fault>
         <faultcode>SOAP-ENV:Server</faultcode>
         <faultstring xml:lang="en">No adapter for endpoint [public org.jdom2.Element com.packtpub.liverestaurant.service.endpoint.OrderEndPoint.handleOrderRequest(org.jdom2.Element) throws java.lang.Exception]: Is your endpoint annotated with @Endpoint, or does it implement a supported interface like MessageHandler or PayloadEndpoint?</faultstring>
      </SOAP-ENV:Fault>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

有人可以帮我解决一下这个基本程序的问题吗?

我已经提到过这篇文章Spring ws: No adapter for endpoint并开始使用jdom2但没有运气。

0 个答案:

没有答案