弹簧 – 没有适用于端点SWS的适配器

前端之家收集整理的这篇文章主要介绍了弹簧 – 没有适用于端点SWS的适配器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用 this教程创建一个简单的Hello World WebService.

我正在运行Java 1.7.0_04,Spring 2.1,所有内容都是使用Maven构建的,并使用Tomcat6进行部署.但是,当尝试发送SOAP请求(soapUI)时,服务器返回我

No adapter for endpoint [public org.jdom.Element com.mycompany.hr.ws.HolidayEndpoint.handleHolidayRequest(org.jdom.Element) throws java.lang.Exception]: Is your endpoint annotated with @Endpoint,or does it implement a supported interface like MessageHandler or PayloadEndpoint?

我认为注释有问题,但这是我的文件的样子:

web.xml中

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
         version="2.4">

    <display-name>Khozzy custom WebService</display-name>

    <servlet>
        <servlet-name>spring-ws</servlet-name>
        <servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>

        <init-param>
            <param-name>transformWsdlLocations</param-name>
            <param-value>true</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring-ws</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

</web-app>

弹簧-WS-servlet.xml中

<?xml version="1.0" encoding="UTF-8"?>
<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:sws="http://www.springframework.org/schema/web-services"
       xsi:schemaLocation=" http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                            http://www.springframework.org/schema/web-services
                            http://www.springframework.org/schema/web-services/web-services-2.0.xsd
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.mycompany.hr.ws" />
    <context:component-scan base-package="com.mycompany.hr.service" />

    <sws:annotation-driven />

    <sws:dynamic-wsdl id="holiday" portTypeName="HumanResource" locationUri="/holidayService/" targetNamespace="http://mycompany.com/hr/definitions">
        <sws:xsd location="/WEB-INF/hr.xsd"/>
    </sws:dynamic-wsdl>
</beans>

HolidayEndpoint.java

package com.mycompany.hr.ws;

import com.mycompany.hr.service.HumanResourceService;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.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 org.springframework.ws.server.endpoint.annotation.ResponsePayload;

import java.text.SimpleDateFormat;
import java.util.Date;

@Endpoint
public class HolidayEndpoint {
    private static final String NAMESPACE_URI = "http://mycompany.com/hr/schemas";
    private XPath startDateExpression;
    private XPath endDateExpression;
    private XPath nameExpression;

    private HumanResourceService humanResourceService;

    @Autowired
    public HolidayEndpoint(HumanResourceService humanResourceService) throws JDOMException {
        this.humanResourceService = humanResourceService;

        Namespace namespace = Namespace.getNamespace("hr",NAMESPACE_URI);

        startDateExpression = XPath.newInstance("//hr:StartDate");
        startDateExpression.addNamespace(namespace);

        endDateExpression = XPath.newInstance("//hr:EndDate");
        endDateExpression.addNamespace(namespace);

        nameExpression = XPath.newInstance("concat(//hr:FirstName,' ',//hr:LastName)");
        nameExpression.addNamespace(namespace);
    }

    @PayloadRoot(namespace = NAMESPACE_URI,localPart = "HolidayRequest")
    @ResponsePayload
    public Element handleHolidayRequest(@RequestPayload Element holidayRequest) throws Exception {
        Element toReturn = null;

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = dateFormat.parse(startDateExpression.valueOf(holidayRequest));
        Date endDate = dateFormat.parse(endDateExpression.valueOf(holidayRequest));
        String name = nameExpression.valueOf(holidayRequest);

        humanResourceService.bookHoliday(startDate,endDate,name);
        return toReturn;
    }
}

hr.xsd

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:hr="http://mycompany.com/hr/schemas"
           elementFormDefault="qualified"
           targetNamespace="http://mycompany.com/hr/schemas">

    <xs:element name="HolidayRequest">
        <xs:complexType>
            <xs:all>
                <xs:element name="Holiday" type="hr:HolidayType"/>
                <xs:element name="Employee" type="hr:EmployeeType"/>
            </xs:all>
        </xs:complexType>
    </xs:element>

    <xs:complexType name="HolidayType">
        <xs:sequence>
            <xs:element name="StartDate" type="xs:date"/>
            <xs:element name="EndDate" type="xs:date"/>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="EmployeeType">
        <xs:sequence>
            <xs:element name="Number" type="xs:integer"/>
            <xs:element name="FirstName" type="xs:string"/>
            <xs:element name="LastName" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>

</xs:schema>

解决方法

这是解决您问题的有效方法

将此依赖项添加到您的pom.xml

将jdom源中的导入更改为jdom2

这是HolidayEndpoint的更新版本:

package com.mycompany.ws_template.endpoint;

import com.mycompany.ws_template.service.HumanResource;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.jdom2.JDOMException;
import org.jdom2.Namespace;
import org.jdom2.Element;
import org.jdom2.filter.Filters;
import org.jdom2.xpath.XPathExpression;
import org.jdom2.xpath.XPathFactory;

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;


@Endpoint
public class HolidayEndpoint {

private static final String NAMESPACE_URI = "http://www.mycompany.com/holiday-service/schemas/holiday-request";

private XPathExpression<Element> startDateExpression;
private XPathExpression<Element> endDateExpression;
private XPathExpression<Element> nameExpression;
private XPathExpression<Element> surnameExpression;

@Autowired private HumanResource holidayService;

public HolidayEndpoint() throws JDOMException {

    Namespace namespace = Namespace.getNamespace("hr",NAMESPACE_URI);

    XPathFactory xpathFactory = XPathFactory.instance();
    startDateExpression = xpathFactory.compile("//hr:StartDate",Filters.element(),null,namespace);
    endDateExpression = xpathFactory.compile("//hr:EndDate",namespace);
    nameExpression = xpathFactory.compile("//hr:EmployeeName",namespace);
    surnameExpression = xpathFactory.compile("//hr:EmployeeSurname",namespace);
}

@PayloadRoot(namespace = NAMESPACE_URI,localPart = "HolidayRequest")
public void handleHolidayRequest(@RequestPayload Element holidayRequest) throws Exception {

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date startDate = sdf.parse(startDateExpression.evaluate(holidayRequest).get(0).getValue());
    Date endDate = sdf.parse(endDateExpression.evaluate(holidayRequest).get(0).getValue());
    String name = nameExpression.evaluate(holidayRequest).get(0).getValue() + surnameExpression.evaluate(holidayRequest).get(0).getValue();

    holidayService.bookHoliday(startDate,name);
}

}
原文链接:https://www.f2er.com/java/128817.html

猜你在找的Java相关文章