编程语言
首页 > 编程语言> > java-使用Gson for Restlet将Post数据(表示形式)转换为对象

java-使用Gson for Restlet将Post数据(表示形式)转换为对象

作者:互联网

我正在尝试将表单发布到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

解决方法:

实际上,无需显式使用gson转换器,就可以利用Restlet的内置转换器支持.

实际上,将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而不是类Representation,如下所述:

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作为服务器资源中处理方法的参数提供.
>序列化为返回的bean的响应内容.

您在这里无事可做.

对于客户端,您可以利用相同的机制.它基于带注释的接口.对于这一点,你需要创建定义什么可以在资源调用的接口.对于我们之前的示例,将是这样的:

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.

希望对您有帮助,
蒂埃里

标签:restlet,gson,google-app-engine,java
来源: https://codeday.me/bug/20191119/2037334.html