java-使用Gson for Restlet将Post数据(表示形式)转换为对象
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-使用Gson for Restlet将Post数据(表示形式)转换为对象,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3591字,纯文字阅读大概需要6分钟。
内容图文
![java-使用Gson for Restlet将Post数据(表示形式)转换为对象](/upload/InfoBanner/zyjiaocheng/663/778df2e5c6d8472996d93430f99ff907.jpg)
我正在尝试将表单发布到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();
}
}
正在发布的表单数据:
解决方法:
实际上,无需显式使用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.
希望对您有帮助,
蒂埃里
内容总结
以上是互联网集市为您收集整理的java-使用Gson for Restlet将Post数据(表示形式)转换为对象全部内容,希望文章能够帮你解决java-使用Gson for Restlet将Post数据(表示形式)转换为对象所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。