Postman调用SpringBoot SOAP API报Invalid Content-Type错误
SpringBoot SOAP接口调用Content-Type报错修复方案
核心报错原因
- SOAP 1.1协议要求请求头
Content-Type必须为text/xml; charset=utf-8,你当前使用的application/xml不符合协议规范,服务端无法将请求识别为合法SOAP消息。 - 接口端点方法返回值定义为
void,方法执行完成后不会返回合法的SOAP格式响应,客户端解析返回内容时会触发内容类型不匹配错误。 - Postman发送的请求体缺少SOAP协议要求的
<Envelope>、<Body>外层包裹标签,直接传入业务节点,服务端无法解析请求结构。
分步修复操作
1. 修正Postman请求配置
- 请求头设置:新增/修改
Content-Type,值为text/xml; charset=utf-8 - 请求地址:填写为
http://localhost:{你的服务端口}/api/,和你配置中MessageDispatcherServlet的映射路径保持一致 - 请求体:按照SOAP标准格式构造,示例如下:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:stu="student"> <soapenv:Header/> <soapenv:Body> <stu:StudentDetailsRequest> <stu:name>测试学生名</stu:name> </stu:StudentDetailsRequest> </soapenv:Body> </soapenv:Envelope>
2. 修正接口端点代码
将StudentController中端点方法的返回值从void改为StudentDetailsResponse,构造并返回响应对象,修正后代码如下:
@Endpoint public class StudentController { private static final String NAMESPACE_URI = "student"; @PayloadRoot(namespace = NAMESPACE_URI, localPart = "StudentDetailsRequest") @ResponsePayload public StudentDetailsResponse getStudent(@RequestPayload StudentDetailsRequest request) { StudentDetailsResponse response = new StudentDetailsResponse(); response.setName(request.getName()); System.out.println("收到学生查询请求,查询姓名:" + request.getName()); return response; } }
3. 服务可用性校验
启动项目后,先访问http://localhost:{你的服务端口}/api/studentDetailsWsdl.wsdl,如果能正常加载WSDL文件内容,说明服务端基础配置无误,再通过Postman发送构造好的请求即可拿到正常响应。
内容的提问来源于stack exchange,提问作者Aks 1316
相关产品推荐
相关产品推荐

