Search code examples
jsonpostsoapuirestlet

Cannot access Restlet service using POST from SOAP UI


I created a series of REST services in Java using Restlets. The majority of these services use JSON, and I have no problem accessing them using SOAP UI via a GET request. However, when I try to access POST based services using SOAP UI, the Representation entity parameter is always null. I have searched Stack Overflow as well as the web, but could find nothing which I either haven't already done, or which addresses my problem.

Here is the code for a POST resource which always seems to receive a null entity:

public class CreateAccountResource extends ServerResource {
    @Post("json")
    public Representation createAccount(Representation entity) throws IOException {
        String message = null;
        boolean result = true;

        try {
            String post = entity.getText();
            Object obj = new JSONParser().parse(post);
            JSONObject jsonObject = (JSONObject) obj;

            String username = (String) jsonObject.get("username");
            String password = (String) jsonObject.get("password");
            String email = (String) jsonObject.get("email");

            // more code
        }
        catch (Exception e) {
            // handle exception here
        }
    }
}

And here is a screen shot from my SOAP UI showing the configuration I used when sending the request:

enter image description here

In case you are wondering, I am using IntelliJ in debug mode to inspect the value of the entity, and the project uses Maven.


Solution

  • I never use Restlet however I think that since you specify @Post("json") annotation for your createAccount method; the method is waiting for a json in the POST body instead of passing the values as a query parameters.

    So probably you must change your actual POST with the query parameters to a POST call to your URL http://localhost:8080/MyApp/service/createAccount passing the parameters in the body as json:

    {
          "username" : "tim",
          "password" : "password",
          "email" : "tim@me.com"
    }
    

    In SOAPUI could be something like:

    enter image description here

    Hope it helps,