lakshmi
lakshmi

Reputation: 4969

Couldn't create SOAP message due to exception: XML reader error: com.ctc.wstx.exc.WstxParsingException: Undeclared namespace prefix "ws"

I have created the web service using JAX-WS. When I access the web service am getting following response:

<?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>

My web service is:

@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());
    }

}

Client Program:

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();
        }
    }


}

Please help me to solve the problem.

Upvotes: 3

Views: 37017

Answers (1)

Papasmile
Papasmile

Reputation: 634

It's right there

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

You have ws: namespace but you have not declared that namespace anywhere.

You should have a schema for your web service for example (from springsource)

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

or

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

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

Upvotes: 2

Related Questions