Restlet 解析 json 请求

Restlet parsing json request

我正在尝试使用 restlet 为书籍和其他东西制作一个简单的天文台应用程序。 目前,我在这里和那里管理了一些简单的 GET,但我对 POST 有疑问。

准确的说,我无法访问实体的body。这是示例请求,通过 Postman

{
    "isbn": "12345678909876",
    "title": "test",
    "genre": "Encyclopedias",
    "publisher": "Tzoman",
    "release": "2011-01-01",
    "language": "Foreign",
    "pages": 666,
    "cover": "Hard",
    "withdrawn": true,
    "tags": "sapiente"
}

这是代码:

@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();
        }
    }

输出多种多样:使用第一种方法,我设法通过以下输出访问请求:

[[
{
    <Object here>
}=null
]]

第二种方法旨在将请求解析为 JSON Object 但我得到的回复是 [application/json].

我已将 Postman 配置为在 headers 中发送 json,经过大量研究后,我被卡住了。如果有人能为我提供一些提示或研究 material,我将感激不尽!

感谢您的宝贵时间!

EDIT1:这是完整的class,包含导入的包以供参考

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"
                )
        );
    }
}

您可以接受 JsonRepresentation 个实体 (docs)。

您只需致电

final JSONObject yourObject = entity.getJsonObject();

您的端点应该发出它接受 application/json 媒体类型的信号。
您可以通过注释 @Post("json")

指定

您也可以接受 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;
}

然后

@Post
public void accept(final MyObject myObject) { ... }

好的,您还没有安装 Restlet JSON 扩展。

<dependency>
    <groupId>org.restlet.jee</groupId>
    <artifactId>org.restlet.ext.json</artifactId>
    <version>2.3.12</version>
</dependency>