Search code examples
javarestrestlet

Restlet cannot process json


This is my simple restlet code:

public class MailServerResource extends ServerResource {

    @Get("json")
    public String getMail() {
        return this.getRequest().getEntityAsText();
    }

    @Post("json")
    public String sendMail() {
        return this.getRequest().getEntityAsText();
    }
}

Now when I access this resource and pass this string:

{"employees":[
    {"firstName":"John", "lastName":"Doe"},
    {"firstName":"Anna", "lastName":"Smith"},
    {"firstName":"Peter", "lastName":"Jones"}
]}

It throws:

The server is refusing to service the request because the entity of the request is in a format not supported by the requested resource for the requested method

enter image description here


Solution

  • Can you try this syntax?

    @Post("json")
    public String sendMail(String json) {
        return json;
    }
    

    Annotated methods can contain a single argument that represents the request's entity (if any).

    Nb: as a side note, the Get requests does not have a payload. This instruction has no meaning:

    this.getRequest().getEntityAsText()