I am trying to make a simple observatory app for books and stuff using restlet. Currently, I've managed some simple GETs here and there but i have a problem with POST.
To be precise, I can't access the entity's body. Here is the sample request, through Postman
{
"isbn": "12345678909876",
"title": "test",
"genre": "Encyclopedias",
"publisher": "Tzoman",
"release": "2011-01-01",
"language": "Foreign",
"pages": 666,
"cover": "Hard",
"withdrawn": true,
"tags": "sapiente"
}
And here is the code:
@Override
protected Representation post(Representation entity) throws ResourceException {
if (entity.getMediaType().isCompatible(MediaType.APPLICATION_JSON)) {
//Create a new restlet form
Form form = new Form(entity);
System.out.println("Form start");
System.out.println(form);
System.out.println(form.getClass().getSimpleName());
System.out.println("Form end");
//Read the parameters
String isbn = form.getFirstValue("isbn");
System.out.println(isbn);
try {
System.out.println("The entity:" + entity);
JSONObject json = new JSONObject(entity);
String isbn2 = json.getString("isbn");
System.out.println(isbn2);
} catch (JSONException e){
e.printStackTrace();
}
}
The output is varied: Using the first approach, I've managed to somewhat access the request with the following output:
[[
{
<Object here>
}=null
]]
The second approach aims to parse the request as a JSON Object but the reply i get is [application/json].
I have configured Postman to send json in headers and after an exhausting amount of research, I'm stuck. If anyone can provide me with some hints or research material, I would be indebted!
Thanks for your time!
EDIT1: This is the entire class with the imported packages for reference
package org.mypackage;
import org.mypackage.config.Configuration;
import org.mypackage.data.DataAccess;
import org.mypackage.data.Limits;
import org.mypackage.data.model.Book;
import org.json.JSONException;
import org.json.JSONObject;
import org.restlet.data.Form;
import org.restlet.data.MediaType;
import org.restlet.representation.Representation;
import org.restlet.resource.ResourceException;
import org.restlet.resource.ServerResource;
public class BooksResource extends ServerResource {
private final DataAccess dataAccess = Configuration.getInstance().getDataAccess();
String start;
String count;
String sort;
String status;
@Override
protected Representation get() throws ResourceException {
start = getQueryValue("start") != null ? getQueryValue("start") : "0";
count = getQueryValue("count") != null ? getQueryValue("count") : String.valueOf(Limits.DEFAULT_COUNT);
if(getQueryValue("sort") != null && Limits.SORTING_OPTIONS_LIST.contains(getQueryValue("sort"))) {
sort = getQueryValue("sort");
System.out.println(sort);
} else {
sort = String.valueOf(Limits.SORTING_OPTIONS_LIST.get(0));
System.out.println(sort);
}
if(getQueryValue("status") != null && Limits.BOOK_STATUS_LIST.contains(getQueryValue("status"))) {
status = getQueryValue("status");
System.out.println(status);
} else {
status = String.valueOf(Limits.BOOK_STATUS_LIST.get(0));
System.out.println(status);
}
return new JsonMapRepresentation(dataAccess.getProducts(
new Limits(
Integer.parseInt(start),
Integer.parseInt(count)
),
sort,
status
));
}
@Override
protected Representation post(Representation entity) throws ResourceException {
if (entity.getMediaType().isCompatible(MediaType.APPLICATION_JSON)) {
//Create a new restlet form
Form form = new Form(entity);
System.out.println("Form start");
System.out.println(form);
System.out.println(form.getClass().getSimpleName());
System.out.println("Form end");
//Read the parameters
String isbn = form.getFirstValue("isbn");
System.out.println(isbn);
try {
System.out.println("The entity:" + entity);
JSONObject json = new JSONObject(entity);
String isbn2 = json.getString("isbn");
System.out.println(isbn2);
} catch (JSONException e){
e.printStackTrace();
}
}
/* String title = form.getFirstValue("title");
String genre = form.getFirstValue("genre");
String publisher = form.getFirstValue("publisher");
String release = form.getFirstValue("release");
String language = form.getFirstValue("language");
Integer pages = Integer.parseInt(form.getFirstValue("pages"));
String cover = form.getFirstValue("cover");
boolean withdrawn = Boolean.valueOf(form.getFirstValue("withdrawn"));
String tags = form.getFirstValue("tags");*/
//validate the values (in the general case)
//...
/*Book book = dataAccess.addProduct(isbn, title, genre, publisher, release, language, pages, cover, withdrawn, tags);*/
/*return new JsonBookRepresentation(book);*/
return new JsonBookRepresentation(
new Book(
"isbn": "12345678909876",
"title": "test",
"genre": "Encyclopedias",
"publisher": "Tzoman",
"release": "2011-01-01",
"language": "Foreign",
"pages": 666,
"cover": "Hard",
"withdrawn": true,
"tags": "sapiente"
)
);
}
}
You can accept a JsonRepresentation
entity ( docs ).
Than you'll simply have to call
final JSONObject yourObject = entity.getJsonObject();
Your endpoint should signal it accepts an application/json
media type.
You can specify that via the annotation @Post("json")
You can also accept a Java class.
class MyObject {
public String isbn;
public String title;
public String genre;
public String publisher;
public String release;
public String language;
public int pages;
public String cover;
public boolean withdrawn;
public String tags;
}
And then
@Post
public void accept(final MyObject myObject) { ... }
Ok, you haven't got the Restlet JSON extension.
<dependency>
<groupId>org.restlet.jee</groupId>
<artifactId>org.restlet.ext.json</artifactId>
<version>2.3.12</version>
</dependency>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.