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

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

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

  1. 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中

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
  4. version="2.4">
  5.  
  6. <display-name>Khozzy custom WebService</display-name>
  7.  
  8. <servlet>
  9. <servlet-name>spring-ws</servlet-name>
  10. <servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
  11.  
  12. <init-param>
  13. <param-name>transformWsdlLocations</param-name>
  14. <param-value>true</param-value>
  15. </init-param>
  16. </servlet>
  17.  
  18. <servlet-mapping>
  19. <servlet-name>spring-ws</servlet-name>
  20. <url-pattern>/*</url-pattern>
  21. </servlet-mapping>
  22.  
  23. </web-app>

弹簧-WS-servlet.xml中

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:sws="http://www.springframework.org/schema/web-services"
  6. xsi:schemaLocation=" http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8. http://www.springframework.org/schema/web-services
  9. http://www.springframework.org/schema/web-services/web-services-2.0.xsd
  10. http://www.springframework.org/schema/context
  11. http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  12.  
  13. <context:component-scan base-package="com.mycompany.hr.ws" />
  14. <context:component-scan base-package="com.mycompany.hr.service" />
  15.  
  16. <sws:annotation-driven />
  17.  
  18. <sws:dynamic-wsdl id="holiday" portTypeName="HumanResource" locationUri="/holidayService/" targetNamespace="http://mycompany.com/hr/definitions">
  19. <sws:xsd location="/WEB-INF/hr.xsd"/>
  20. </sws:dynamic-wsdl>
  21. </beans>

HolidayEndpoint.java

  1. package com.mycompany.hr.ws;
  2.  
  3. import com.mycompany.hr.service.HumanResourceService;
  4. import org.jdom.Document;
  5. import org.jdom.Element;
  6. import org.jdom.JDOMException;
  7. import org.jdom.Namespace;
  8. import org.jdom.xpath.XPath;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.ws.server.endpoint.annotation.Endpoint;
  11. import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
  12. import org.springframework.ws.server.endpoint.annotation.RequestPayload;
  13. import org.springframework.ws.server.endpoint.annotation.ResponsePayload;
  14.  
  15. import java.text.SimpleDateFormat;
  16. import java.util.Date;
  17.  
  18. @Endpoint
  19. public class HolidayEndpoint {
  20. private static final String NAMESPACE_URI = "http://mycompany.com/hr/schemas";
  21. private XPath startDateExpression;
  22. private XPath endDateExpression;
  23. private XPath nameExpression;
  24.  
  25. private HumanResourceService humanResourceService;
  26.  
  27. @Autowired
  28. public HolidayEndpoint(HumanResourceService humanResourceService) throws JDOMException {
  29. this.humanResourceService = humanResourceService;
  30.  
  31. Namespace namespace = Namespace.getNamespace("hr",NAMESPACE_URI);
  32.  
  33. startDateExpression = XPath.newInstance("//hr:StartDate");
  34. startDateExpression.addNamespace(namespace);
  35.  
  36. endDateExpression = XPath.newInstance("//hr:EndDate");
  37. endDateExpression.addNamespace(namespace);
  38.  
  39. nameExpression = XPath.newInstance("concat(//hr:FirstName,' ',//hr:LastName)");
  40. nameExpression.addNamespace(namespace);
  41. }
  42.  
  43. @PayloadRoot(namespace = NAMESPACE_URI,localPart = "HolidayRequest")
  44. @ResponsePayload
  45. public Element handleHolidayRequest(@RequestPayload Element holidayRequest) throws Exception {
  46. Element toReturn = null;
  47.  
  48. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  49. Date startDate = dateFormat.parse(startDateExpression.valueOf(holidayRequest));
  50. Date endDate = dateFormat.parse(endDateExpression.valueOf(holidayRequest));
  51. String name = nameExpression.valueOf(holidayRequest);
  52.  
  53. humanResourceService.bookHoliday(startDate,endDate,name);
  54. return toReturn;
  55. }
  56. }

hr.xsd

  1. <?xml version="1.0"?>
  2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  3. xmlns:hr="http://mycompany.com/hr/schemas"
  4. elementFormDefault="qualified"
  5. targetNamespace="http://mycompany.com/hr/schemas">
  6.  
  7. <xs:element name="HolidayRequest">
  8. <xs:complexType>
  9. <xs:all>
  10. <xs:element name="Holiday" type="hr:HolidayType"/>
  11. <xs:element name="Employee" type="hr:EmployeeType"/>
  12. </xs:all>
  13. </xs:complexType>
  14. </xs:element>
  15.  
  16. <xs:complexType name="HolidayType">
  17. <xs:sequence>
  18. <xs:element name="StartDate" type="xs:date"/>
  19. <xs:element name="EndDate" type="xs:date"/>
  20. </xs:sequence>
  21. </xs:complexType>
  22.  
  23. <xs:complexType name="EmployeeType">
  24. <xs:sequence>
  25. <xs:element name="Number" type="xs:integer"/>
  26. <xs:element name="FirstName" type="xs:string"/>
  27. <xs:element name="LastName" type="xs:string"/>
  28. </xs:sequence>
  29. </xs:complexType>
  30.  
  31. </xs:schema>

解决方法

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

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

将jdom源中的导入更改为jdom2

这是HolidayEndpoint的更新版本:

  1. package com.mycompany.ws_template.endpoint;
  2.  
  3. import com.mycompany.ws_template.service.HumanResource;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Date;
  6.  
  7. import org.jdom2.JDOMException;
  8. import org.jdom2.Namespace;
  9. import org.jdom2.Element;
  10. import org.jdom2.filter.Filters;
  11. import org.jdom2.xpath.XPathExpression;
  12. import org.jdom2.xpath.XPathFactory;
  13.  
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.ws.server.endpoint.annotation.Endpoint;
  16. import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
  17. import org.springframework.ws.server.endpoint.annotation.RequestPayload;
  18.  
  19.  
  20. @Endpoint
  21. public class HolidayEndpoint {
  22.  
  23. private static final String NAMESPACE_URI = "http://www.mycompany.com/holiday-service/schemas/holiday-request";
  24.  
  25. private XPathExpression<Element> startDateExpression;
  26. private XPathExpression<Element> endDateExpression;
  27. private XPathExpression<Element> nameExpression;
  28. private XPathExpression<Element> surnameExpression;
  29.  
  30. @Autowired private HumanResource holidayService;
  31.  
  32. public HolidayEndpoint() throws JDOMException {
  33.  
  34. Namespace namespace = Namespace.getNamespace("hr",NAMESPACE_URI);
  35.  
  36. XPathFactory xpathFactory = XPathFactory.instance();
  37. startDateExpression = xpathFactory.compile("//hr:StartDate",Filters.element(),null,namespace);
  38. endDateExpression = xpathFactory.compile("//hr:EndDate",namespace);
  39. nameExpression = xpathFactory.compile("//hr:EmployeeName",namespace);
  40. surnameExpression = xpathFactory.compile("//hr:EmployeeSurname",namespace);
  41. }
  42.  
  43. @PayloadRoot(namespace = NAMESPACE_URI,localPart = "HolidayRequest")
  44. public void handleHolidayRequest(@RequestPayload Element holidayRequest) throws Exception {
  45.  
  46. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  47. Date startDate = sdf.parse(startDateExpression.evaluate(holidayRequest).get(0).getValue());
  48. Date endDate = sdf.parse(endDateExpression.evaluate(holidayRequest).get(0).getValue());
  49. String name = nameExpression.evaluate(holidayRequest).get(0).getValue() + surnameExpression.evaluate(holidayRequest).get(0).getValue();
  50.  
  51. holidayService.bookHoliday(startDate,name);
  52. }
  53.  
  54. }

猜你在找的Java相关文章