REST API插件 - 使用body而不是查询字符串作为参数 [英] REST API plugin - use body instead of query string for parameters

查看:130
本文介绍了REST API插件 - 使用body而不是查询字符串作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将此作为参考在Struts2上创建仅REST配置:

I'm using this as a reference to create a REST only configuration on Struts2:

https://cwiki.apache.org/confluence/display/WW/REST+Plugin

我有一个型号,带有几个测试字段的收据:标题,正文。

I have one model, Receipt with a few test fields: title, body.

目前要创建收据,我以这种方式发送请求:

Currently to create a receipt, I send a request in this way:

POST /receipt/?body=new_body&title=new_title

它会为我创建一个收据,其中传递了新的正文和标题。

and it creates me a receipt with the new body and title passed in.

这不起作用:

POST /receipt/
{
  "body": "new_body",
  "title": "new title"
}

这里有一些代码:

struts.xml

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

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

它应该按照我想要的方式工作,或者它只是插件的工作方式?

Is it supposed to work as I want it to, or is it just how the plugin works?

推荐答案

我猜邮递员正在请求正文中发送JSON并设置内容类型 application / json 。如果你将 json 拦截器添加到堆栈,Struts可以解析请求。

I guess that postman is sending JSON in the body of the request and sets the content type application/json. Struts can parse the request if you add json interceptor to the stack.

<interceptor-stack name="myStack">
    <interceptor-ref name="json"/>
    <interceptor-ref name="myInterceptor"/>
    <interceptor-ref name="defaultStack"/>
</interceptor-stack>

json拦截器的描述 JSON插件

如果使用拦截器,将从请求中的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:


  • content-type必须是application / json

  • JSON内容必须格式正确,请参阅 json.org 用于语法。

  • 对于必须填充的字段,操作必须具有公共setter方法。

  • 支持的填充类型有:Primitives(int,long ... String),Date,List,Map,Primitive Arrays,Other class(稍后会详细介绍)和其他数组class。

  • JSON中要填充在列表或地图中的任何对象都是Map类型(从属性映射到值),任何整数都是l类型为Long,任何十进制数字的类型为Double,任何类型为List的数组。

  • The "content-type" must be "application/json"
  • The JSON content must be well formed, see json.org 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

这篇关于REST API插件 - 使用body而不是查询字符串作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆