java - 使用 Gson for ReSTLet 将 Post 数据(Representation)转换为 Object

标签 java google-app-engine gson restlet

我正在尝试将表单发布到 ReSTLet ServerResource 并使用 Gson Restlet Extension 将其读入对象中.

no documentation关于如何使用它,StackOverflow 上没有任何内容。

gson reSTLet 扩展的正确使用方法是什么?

以下是我到目前为止尝试过的:

public class CustomerSegment {
    private int visitsMin;
    private int visitsMax;
    // Getters, Setters and constructors
}

public class CampaignsResource extends ServerResource {
    @Post
    public Representation createCampaign(Representation entity) {
        Form form = new Form(entity);
        // Using form is the usual way, which works fine
        // form: [[visitsMin=3], [visitsMax=6]]

        CustomerSegment segment = null;
        // Following hasn't worked 
        GsonConverter converter = new GsonConverter();
        try {
            segment = converter.toObject(entity, CustomerSegment.class, this);
            //segment = null
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        GsonRepresentation<CustomerSegment> gson
        = new GsonRepresentation<CustomerSegment>(entity, CustomerSegment.class);
        try {
            segment = gson.getObject();
            //NullPointerException
        } catch (IOException e) {
            e.printStackTrace();
        }

        return new EmptyRepresentation();
    }
} 

正在发布的表单数据: Form Post Data

最佳答案

事实上,您可以利用 ReSTLet 的内置转换器支持,而无需显式使用 gson 转换器。

事实上,当您将 GSON 扩展放在类路径中时,它包含的转换器会自动在 ReSTLet 引擎本身中注册。要检查您是否可以在启动应用程序时简单地使用这些行:

List<ConverterHelper> converters
       = Engine.getInstance().getRegisteredConverters();
for (ConverterHelper converterHelper : converters) {
    System.out.println("- " + converterHelper);
}

/* This will print this in your case:
   - org.restlet.ext.gson.GsonConverter@2085ce5a
   - org.restlet.engine.converter.DefaultConverter@30ae8764
   - org.restlet.engine.converter.StatusInfoHtmlConverter@123acf34
*/

然后您可以在服务器资源的方法签名中依赖 bean,而不是类表示,如下所述:

public class MyServerResource extends ServerResource {
    @Post
    public SomeOutputBean handleBean(SomeInputBean input) {
        (...)
        SomeOutputBean bean = new SomeOutputBean();
        bean.setId(10);
        bean.setName("some name");
        return bean;
    }
}

这对双方都有效:

  • 将请求内容反序列化为 bean,作为服务器资源中处理方法的参数提供。
  • 序列化为返回bean的响应内容。

您在这里没有任何事可做。

对于客户端,您可以利用相同的机制。它基于带注释的接口(interface)。为此,您需要创建一个接口(interface)来定义可以在资源上调用的内容。对于我们之前的示例,它将是这样的:

public interface MyResource {
    @Post
    SomeOutputBean handleBean(SomeInputBean input);
}

然后您可以将它与客户端资源一起使用,如下所述:

String url = "http://localhost:8182/test";

ClientResource cr = new ClientResource(url);
MyResource resource = cr.wrap(MyResource.class);
SomeInputBean input = new SomeInputBean();
SomeOutputBean output = resource.handleBean(input);

因此,在您的情况下,我将按如下所述重构您的代码:

public class CampaignsResource extends ServerResource {
    private String getUri() {
        Reference resourceRef = getRequest().getResourceRef();
        return resourceRef.toString();
    }

    @Post
    public void createCampaign(CustomerSegment segment) {
        // Handle segment
        (...)

        // You can return something if the client expects
        // to have something returned
        // For creation on POST method, returning a 204 status
        // code with a Location header is enough...
        getResponse().setLocationRef(getUri() + addedSegmentId);
    }
}

例如,您可以利用内容类型 application/json 将数据作为 JSON 发送:

{
  visitsMin: 2,
  visitsMax: 11
}

如果你想使用 Gson,你应该使用这种内容类型而不是 urlencoded 类型,因为该工具针对 JSON 转换:

Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. Gson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of.

希望对你有帮助, 蒂埃里

关于java - 使用 Gson for ReSTLet 将 Post 数据(Representation)转换为 Object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33108582/

相关文章:

java - 图像上的粗体文字

python - 在 Google Cloud SQL (GAE) Python 应用程序中管理数据库连接的好方法是什么?

python - 如何跨内部 App Engine 模块验证请求?

javascript - 是否可以从 *Python* Google App Engine 实例执行服务器端 javascript?

java - jsp中如何显示Json对象

android - java.lang.NumberFormatException : Invalid double: "5b80393f7fca491488980fe1" in android

java - 基础Java,如何声明这些Android函数

java - 将数组作为参数放入 java 字节码中的函数

C# DeflateStream 与 Java DeflaterOutputStream

java - 使用 gson 创建复杂的 json 对象