java-REST API插件-使用主体而不是查询字符串作为参数
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-REST API插件-使用主体而不是查询字符串作为参数,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3711字,纯文字阅读大概需要6分钟。
内容图文
我以此为参考在Struts2上创建仅REST配置:
https://cwiki.apache.org/confluence/display/WW/REST+Plugin
我有一个模型,Receipt有几个测试字段:标题,正文.
当前要创建收据,我以这种方式发送请求:
POST /receipt/?body=new_body&title=new_title
它会为我创建一个收据,其中包含新的正文和标题.
这不起作用:
POST /receipt/
{
"body": "new_body",
"title": "new title"
}
这是一些代码:
struts.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<bean type="org.apache.struts2.rest.handler.ContentTypeHandler" name="jackson" class="org.apache.struts2.rest.handler.JacksonLibHandler"/>
<constant name="struts.rest.handlerOverride.json" value="jackson"/>
<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
<constant name="struts.devMode" value="true"/>
<constant name="struts.rest.content.restrictToGET" value="false"/>
<constant name="struts.rest.defaultExtension" value="json"/>
<constant name="struts.rest.handlerOverride.EXTENSION" value="json"/>
<constant name="struts.i18n.encoding" value="UTF-8"/>
<constant name="struts.action.extension" value="xhtml,,xml,json,action"/>
<constant name="struts.mapper.class" value="org.apache.struts2.dispatcher.mapper.PrefixBasedActionMapper" />
<constant name="struts.mapper.prefixMapping" value="/receipt:rest,:struts"/>
<constant name="struts.convention.action.suffix" value="Controller"/>
<constant name="struts.convention.action.mapAllMatches" value="true"/>
<constant name="struts.convention.default.parent.package" value="receipto"/>
<constant name="struts.convention.package.locators" value="controllers,actions"/>
</struts>
ReceiptController.java:
public class ReceiptController implements ModelDriven<Object> {
private ReceiptManager receiptManager = new ReceiptManager();
private String id;
private Receipt model = new Receipt();
private Collection list;
public Object getModel()
{
return (list==null ? model : list);
}
public HttpHeaders create()
{
receiptManager.save(model);
return new DefaultHttpHeaders("create");
}
public HttpHeaders show()
{
model = receiptManager.find(id);
return new DefaultHttpHeaders("show");
}
public HttpHeaders update()
{
receiptManager.save(model);
return new DefaultHttpHeaders("update");
}
public HttpHeaders destroy()
{
model = receiptManager.destroy(id);
return new DefaultHttpHeaders("destroy");
}
public HttpHeaders index()
{
list = receiptManager.list();
return new DefaultHttpHeaders("index").disableCaching();
}
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
}
它是否应该按我想要的方式工作,或者仅仅是插件的工作方式?
解决方法:
我猜想邮递员正在请求的正文中发送JSON,并设置了内容类型application / json.如果将json拦截器添加到堆栈,Struts可以解析请求.
<interceptor-stack name="myStack">
<interceptor-ref name="json"/>
<interceptor-ref name="myInterceptor"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
JSON Plugin中对“ json”拦截器的描述:
If the interceptor is used, the action will be populated from the JSON content in the request, these are the rules of the interceptor:
- The “content-type” must be “application/json”
- The JSON content must be well formed, see 07001 for grammar.
- Action must have a public “setter” method for fields that must be populated.
- Supported types for population are: Primitives (int,long…String), Date, List, Map, Primitive Arrays, Other class (more on this later), and Array of Other class.
- Any object in JSON, that is to be populated inside a list, or a map, will be of type Map (mapping from properties to values), any whole number will be of type Long, any decimal number will be of type Double, and any array of type List.
资源:
> FAQ
> Getting started
> The resources page
内容总结
以上是互联网集市为您收集整理的java-REST API插件-使用主体而不是查询字符串作为参数全部内容,希望文章能够帮你解决java-REST API插件-使用主体而不是查询字符串作为参数所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。