Calling Mendix webservices with Java successfully

3
Has anyone written Java code to call a webservice created by the modeler? If so, may I please have an example? I used the example from O'Reilly, however, I am encountering the following error: Exception in thread "main" AxisFault faultCode: {http://schemas.xmlsoap.org/soap/envelope/}Server.userException faultSubcode: faultString: org.xml.sax.SAXParseException: White spaces are required between publicId and systemId.
asked
1 answers
3

There are sample messages that you can copy/paste from your app's webservice documentation page:

http://localhost:8080/ws-doc/

Some sample java code:

/*
 * Code requires the following libs:
 * commons-codec-1.3.jar
 * commons-httpclient-3.1.jar (or 3.0)
 * commons-logging-1.1.jar
 * These can all be found in mendix\runtime\lib\
 */
public static void main(String[] args) throws HttpException, IOException {
    String location = "http://localhost:8080/ws/Sleepy/";

    // you have to configure username and the password in mendix. 
    // When logged in as MxAdmin, there's a user screen. click on "new web service user"
    String username = "ws_user"; 
    String password = "1";
    String soapAction = "SleepNow"; // this should correspond to whatever operation you want to call

    // The following was copy/pasted from http://localhost:8080/ws-doc/Sleepy/SleepNow
    // Sleepy is my Published Webservice, SleepNow is the soap operation I want to call
    RequestEntity soapRequest = new StringRequestEntity("<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:tns=\"http://localhost:8081/ws/\"> <soap:Header> <tns:authentication> <username>"
            + username
            + "</username><password>"
            + password 
            + "</password></tns:authentication></soap:Header><soap:Body><tns:SleepNow/></soap:Body></soap:Envelope>");

    PostMethod post = new PostMethod(location);
    post.setRequestHeader("Host", new URL(location).getHost());
    post.setRequestHeader("SOAPAction", soapAction);

    post.setRequestEntity(soapRequest);

    HttpClient client = new HttpClient();
    int status = client.executeMethod(post);

    System.out.println(post.getResponseBodyAsString());
    // or, alternatively: post.getResponseBodyAsStream(); if you want an inputstream instead of a string
}
answered