web-services - 由于异常 : XML reader error: com. ctc.wstx.exc.WstxParsingException:未声明的命名空间前缀 "ws",无法创建 SOAP 消息

标签 web-services jax-ws

我已经使用 JAX-WS 创建了 Web 服务。当我访问网络服务时得到以下响应:

<?xml version='1.0' encoding='UTF-8'?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><S:Fault xmlns:ns4="http://www.w3.org/2003/05/soap-envelope"><faultcode>S:Client</faultcode><faultstring>Couldn't create SOAP message due to exception: XML reader error: com.ctc.wstx.exc.WstxParsingException: Undeclared namespace prefix "ws"&#xD;
 at [row,col {unknown-source}]: [1,169]</faultstring></S:Fault></S:Body></S:Envelope>

我的网络服务是:

@WebService
public interface HelloService {
    @WebMethod
    String Hello(@WebParam(name = "name")String msg);   
} 

public class HelloServiceIml implements HelloService {
@Override
String Hello(String msg){
return "Hello" + msg;
}
}

public class Mainws {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Endpoint.publish("http://localhost:8085/hello", new HelloServiceIml());
    }

}

客户端程序:

public class PostSoap {
    public static void main(String[] args) throws Exception {
        // Get target URL
        String strURL = "http://localhost:8085/HelloService";
        // Get SOAP action
        String strSoapAction = "SOAPAction";
        // Get file to be posted
        String strXML = "<?xml version=\"1.0\" encoding=\"windows-1251\"?> " +
                "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"> " +
                "<soapenv:Header/> " +              
                "<soapenv:Body> <ws:Hello> " +
                " <name>John</name> " +     
                " </ws:Hello> </soapenv:Body> </soapenv:Envelope> ";

        // Prepare HTTP post
        PostMethod post = new PostMethod(strURL);
        post.setRequestBody(strXML);
        post.setRequestHeader("Content-Type", "text/xml");

        // consult documentation for your web service
        post.setRequestHeader("SOAPAction", strSoapAction);
        // Get HTTP client
        HttpClient httpclient = new HttpClient();
        httpclient.getParams().setAuthenticationPreemptive(true);
        httpclient.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("user", "pwd"));

        // Execute request
        try {
            int result = httpclient.executeMethod(post);
            // Display status code
            System.out.println("Response status code: " + result);
            // Display response
            System.out.println("Response body: ");
            System.out.println(post.getResponseBodyAsString());
        } finally {
            // Release current connection to the connection pool once you are done
            post.releaseConnection();
        }
    }


}

请帮我解决这个问题。

最佳答案

就在那里

<ws:Hello> " +
" <name>John</name> " +     
" </ws:Hello>

您有 ws: 命名空间,但您尚未在任何地方声明该命名空间。

例如,您应该有一个 Web 服务架构(来自 springsource)

<Holiday xmlns="http://mycompany.com/hr/schemas">
    <StartDate>2006-07-03</StartDate>
    <EndDate>2006-07-07</EndDate>
</Holiday>

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"  xmlns:ws="http://mycompany.com/hr/schemas">

参见:http://static.springsource.org/spring-ws/site/reference/html/tutorial.html

关于web-services - 由于异常 : XML reader error: com. ctc.wstx.exc.WstxParsingException:未声明的命名空间前缀 "ws",无法创建 SOAP 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17585702/

相关文章:

c# - 如何使用 C# 将 JSon 数组发送到远程 asmx web 服务?

php - Web 服务不适用于 mysql

web-services - SharePoint 上的多页列表

java - 使用 Java Web 服务进行远程连接

javascript - 需要从 api 调用中获取响应 json

web-services - 带空格的 Systemd 服务名称

java - 从 Java 使用 SharePoint Web 服务时 NTLM 失败?

java - 解码时出现 JAXB 意外元素错误

java - WsImport ant 任务和 JDK 6

xpath - 如何自定义在导入的 WSDL 中内联的模式