如何用feign-soap库创建CRUD SOAP客户端?代码报错求排查
That error pops up because the SOAP requests your Feign client is sending don’t match what your RPC-style SOAP server expects. Let’s break down the key issues and fixes step by step:
1. Correct the Request Path
RPC-style SOAP services use a single endpoint (your http://localhost:8080/ws/users) to handle all operations—you don’t need to append /get or /insert to the POST path. Update all your @RequestLine annotations to:
@RequestLine("POST /")
2. Fix the SOAPAction Header Value
Your server’s @WebService uses a default target namespace derived from your package name: http://ws.gpcoder.com/ (since your package is com.gpcoder.ws). Each method’s SOAPAction must include this full namespace, not just the method name.
To confirm the exact SOAPAction for each method, visit your server’s WSDL at http://localhost:8080/ws/users?wsdl—look for the soap:operation tag under each operation to find the correct soapAction value.
Example corrected annotation for the insert method:
@RequestLine("POST /") @Headers({ "SOAPAction: http://ws.gpcoder.com/insert" }) Integer createUser(User user); // Use Integer instead of String for the return type
3. Adjust Method Parameters & Return Types
Feign-Soap relies on JAXB to serialize/deserialize XML, so you can’t pass raw primitive types like int id directly. You need to wrap parameters in JAXB-annotated request objects, and use proper Java objects for return values (not String).
Example for the get method:
First, create a request wrapper class:
@XmlRootElement(namespace = "http://ws.gpcoder.com/") public class GetUserRequest { private int id; // Getters and Setters }
Then update the Feign interface method:
@RequestLine("POST /") @Headers({ "SOAPAction: http://ws.gpcoder.com/get" }) User getUser(GetUserRequest request);
4. Fix JAXBContextFactory Configuration
Your current schema location is set to a dummy value—replace it with your server’s namespace and WSDL URL, and make sure to scan your entity package so JAXB can find your classes:
JAXBContextFactory jaxbFactory = new JAXBContextFactory.Builder() .withMarshallerJAXBEncoding("UTF-8") .withMarshallerSchemaLocation("http://ws.gpcoder.com/ http://localhost:8080/ws/users?wsdl") .withPackagesToScan("com.gpcoder.model") // Scan your entity package .build();
5. Ensure Your User Class Has Proper JAXB Annotations
The User class needs a @XmlRootElement annotation with the correct namespace to work with JAXB serialization/deserialization:
@XmlRootElement(namespace = "http://ws.gpcoder.com/") public class User { private int id; private String username; // Getters and Setters }
Final Test Adjustment
Note that you don’t need to set the user ID manually—your server generates a unique ID. Update your createUser method:
private static void createUser() throws IOException { User user1 = new User(); user1.setUsername("gpcoder.com"); Integer userId = userService.createUser(user1); System.out.println("Created user with ID: " + userId); }
内容的提问来源于stack exchange,提问作者Giang Phan

