我创建了SOAP Web服务,我真的是SOAP的新手。在创建Web服务时,我将面临下面的问题。
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<SOAP-ENV:Fault><faultcode>SOAP-ENV:Server</faultcode>
<faultstring xml:lang="en">unexpected element (uri:"http://spring.io/guides/gs-producing-web-service", local:"getUserRequest"). Expected elements are <{}getUserRequest>
</faultstring>
</SOAP-ENV:Fault>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
这是我的代码:
@Endpoint
public class UserEndpoint {
private static final String NAMESPACE_URI = "http://spring.io/guides/gs-producing-web-service";
//@SuppressWarnings("unused")
private UserRepo repo;
@Autowired
public UserEndpoint(UserRepo repo) {
this.repo = repo;
}
@PayloadRoot(namespace = NAMESPACE_URI, localPart = "getUserRequest")
@ResponsePayload
public GetUserResponse getUser(@RequestPayload GetUserRequest request) {
GetUserResponse response = new GetUserResponse();
response.getUser().getContact()
System.out.println("done!!");
return response;
}
}
输入XML文件:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:gs="http://spring.io/guides/gs-producing-web-service">
<soapenv:Header/>
<soapenv:Body>
<gs:getUserRequest>
<gs:name>Spain</gs:name>
</gs:getUserRequest>
</soapenv:Body>
</soapenv:Envelope>
我无法理解错误及其原因。
修复以下错误:
<faultstring xml:lang="en">unexpected element (uri:"http://spring.io/guides/gs-producing-web-service", local:"getCountryRequest"). Expected elements are <{}getCountryRequest></faultstring>
使用Sergey Usachev提供的解决方案,在班级getCountryRequest中更改以下内容:
@XmlRootElement(namespace="http://spring.io/guides/gs-producing-web-service", name="getCountryRequest") public class GetCountryRequest {
,但是您将获得第二个错误:
<faultstring xml:lang="en">The country's name must not be null</faultstring>
要在同一类中修改此问题,请修改以下内容:
@XmlElement(namespace="http://spring.io/guides/gs-producing-web-service", required = true)
然后,您准备使用此地址上的WSDL使用SOAP UI测试服务:
http://localhost:8080/ws/countries.wsdl
我认为,也许您没有在GetUserRequest
类的注释@XmlRootElement
中编写XML名称空间。例如:
@XmlRootElement(namespace="http://spring.io/guides/gs-producing-web-service", name="getUserRequest")