Cannot access Restlet service using POST from SOAP UI

时间:2016-08-31 16:58:46

标签: json post soapui restlet

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.

1 个答案:

答案 0 :(得分:1)

我从不使用 Restlet 但我认为,因为您为@Post("json")方法指定了createAccount注释;该方法正在等待json正文中的POST,而不是将值作为查询参数传递。

因此,您可能必须使用查询参数将实际POST更改为POST调用您的网址http://localhost:8080/MyApp/service/createAccount,将正文中的参数作为json传递:

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

SOAPUI 中可能是这样的:

enter image description here

希望它有所帮助,