Spring Boot SOAP端点返回No adapter for endpoint错误排查
Spring SOAP Web Service 端点适配失败、请求解析为空问题
我正在深入学习Spring SOAP Web Service与JAXB类生成相关技术,参照spring.io官方教程搭建了简易Web服务应用,但服务无法按预期响应请求。更新:已参考DimasG的建议修复配置,但问题仍未解决。
Web服务配置类代码
@EnableWs @Configuration public class WebServiceConfiguration extends WsConfigurerAdapter { @Bean public ServletRegistrationBean registrationBean(ApplicationContext context) { MessageDispatcherServlet servlet = new MessageDispatcherServlet(); servlet.setApplicationContext(context); servlet.setTransformWsdlLocations(true); return new ServletRegistrationBean(servlet, "/ws/*"); } @Bean(name = "employees") public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema schema) { DefaultWsdl11Definition definition = new DefaultWsdl11Definition(); definition.setPortTypeName("EmployeesPort"); definition.setLocationUri("/ws"); definition.setTargetNamespace("http://spring/demo/webservice"); definition.setSchema(schema); return definition; } @Bean public XsdSchema employeesSchema() { return new SimpleXsdSchema(new ClassPathResource("xsd/employees.xsd")); } }
XSD架构定义文件
<?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://spring/demo/webservice" targetNamespace="http://spring/demo/webservice" elementFormDefault="qualified"> <xs:element name="getEmployeeRequest"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="getEmployeeResponse"> <xs:complexType> <xs:sequence> <xs:element name="employee" type="tns:employee"/> </xs:sequence> </xs:complexType> </xs:element> <xs:complexType name="employee"> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="age" type="xs:int"/> <xs:element name="position" type="xs:string"/> <xs:element name="gender" type="tns:gender"/> </xs:sequence> </xs:complexType> <xs:simpleType name="gender"> <xs:restriction base="xs:string"> <xs:enumeration value="M"/> <xs:enumeration value="F"/> </xs:restriction> </xs:simpleType> </xs:schema>
服务端点实现代码
@Endpoint public class EmployeeWebService { private static final String NAMESPACE_URI = "http://spring/demo/webservice"; private final List<Employee> employees = new ArrayList<>(); @PostConstruct public void init() { employees.addAll( Arrays.asList( createEmployee("John Doe", 30, Gender.M, "Manager"), createEmployee("Jane Doe", 27, Gender.F, "QA"), createEmployee("Bob Johns", 28, Gender.M, "Programmer"), createEmployee("Tiffany Collins", 24, Gender.F, "CEO"), createEmployee("Alice", 25, Gender.F, "Secretary") ) ); } @PayloadRoot(namespace = NAMESPACE_URI, localPart = "getEmployeeRequest") @ResponsePayload public GetEmployeeResponse get(@RequestPayload GetEmployeeRequest request) { Employee found = employees.stream() .filter(emp -> emp.getName().equals(request.getName())) .findFirst() .orElseThrow(() -> new RuntimeException("Not found")); GetEmployeeResponse response = new GetEmployeeResponse(); response.setEmployee(found); return response; } }
测试用SOAP请求报文
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:gs="http://spring/demo/webservice"> <soapenv:Header/> <soapenv:Body> <gs:getEmployeeRequest> <gs:name>Alice</gs:name> </gs:getEmployeeRequest> </soapenv:Body> </soapenv:Envelope>
初始错误响应
请求发送后收到如下报错:
<faultstring xml:lang="en">No adapter for endpoint [public spring.demo.webservice.GetEmployeeResponse com.abondarenkodev.demo.spring.webservice.controller.EmployeeWebService.get(spring.demo.webservice.GetEmployeeRequest)]: Is your endpoint annotated with @Endpoint, or does it implement a supported interface like MessageHandler or PayloadEndpoint?</faultstring>
已尝试的排查方案
方案1:使用JAXBElement包装请求与响应对象
修改端点方法签名与返回逻辑:
@PayloadRoot(namespace = NAMESPACE_URI, localPart = "getEmployeeRequest") @ResponsePayload public JAXBElement<GetEmployeeResponse> get(@RequestPayload JAXBElement<GetEmployeeRequest> request) { Employee found = employees.stream() .filter(emp -> emp.getName().equals(request.getValue().getName())) .findFirst() .orElseThrow(() -> new RuntimeException("Not found")); GetEmployeeResponse response = new GetEmployeeResponse(); response.setEmployee(found); return new JAXBElement<>( QName.valueOf("GetEmployeeResponse"), GetEmployeeResponse.class, response ); }
测试结果:
- 使用
jakarta.xml.bind.JAXBElement时,仍然抛出相同的“No adapter”错误 - 使用
javax.xml.bind.JAXBElement时,触发自定义的“Not found”异常,排查发现request.getValue()返回值为null

方案2:检查JAXB生成类的注解
确认JAXB自动生成的请求、响应类均已标注@XmlRootElement注解,未发现注解缺失问题。
Maven项目配置文件(pom.xml)
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.0</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.abondarenkodev.demo.spring.webservice</groupId> <artifactId>spring-soap-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>spring-soap-demo</name> <description>Demo project for Spring Boot WebServices</description> <properties> <java.version>11</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web-services</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>wsdl4j</groupId> <artifactId>wsdl4j</artifactId> <version>1.6.3</version> </dependency> <dependency> <groupId>jakarta.xml.bind</groupId> <artifactId>jakarta.xml.bind-api</artifactId> <version>4.0.0</version> </dependency> <dependency> <groupId>org.jdom</groupId> <artifactId>jdom2</artifactId> <version>2.0.6.1</version> </dependency> <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.1</version> </dependency> <dependency> <groupId>org.glassfish.jaxb</groupId> <artifactId>jaxb-runtime</artifactId> <version>2.3.1</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>jaxb2-maven-plugin</artifactId> <version>3.1.0</version> <executions> <execution> <id>xjc</id> <goals> <goal>xjc</goal> </goals> </execution> </executions> <configuration> <sources> <source>${project.basedir}/src/main/resources/xsd</source> </sources> <outputDirectory>${project.build.directory}/generated-sources/jaxb/</outputDirectory> <clearOutputDir>false</clearOutputDir> </configuration> </plugin> </plugins> </build> </project>
恳请各位提供解决思路,非常感谢。
内容的提问来源于stack exchange,提问作者Alexander Bond
相关产品推荐
相关产品推荐

