• <bdo id='CgLtc'></bdo><ul id='CgLtc'></ul>

      1. <legend id='CgLtc'><style id='CgLtc'><dir id='CgLtc'><q id='CgLtc'></q></dir></style></legend>
      2. <tfoot id='CgLtc'></tfoot>

      3. <i id='CgLtc'><tr id='CgLtc'><dt id='CgLtc'><q id='CgLtc'><span id='CgLtc'><b id='CgLtc'><form id='CgLtc'><ins id='CgLtc'></ins><ul id='CgLtc'></ul><sub id='CgLtc'></sub></form><legend id='CgLtc'></legend><bdo id='CgLtc'><pre id='CgLtc'><center id='CgLtc'></center></pre></bdo></b><th id='CgLtc'></th></span></q></dt></tr></i><div id='CgLtc'><tfoot id='CgLtc'></tfoot><dl id='CgLtc'><fieldset id='CgLtc'></fieldset></dl></div>

        <small id='CgLtc'></small><noframes id='CgLtc'>

      4. REST API 插件 - 使用正文而不是查询字符串作为参数

        时间:2023-09-25

        <small id='8GeMv'></small><noframes id='8GeMv'>

      5. <i id='8GeMv'><tr id='8GeMv'><dt id='8GeMv'><q id='8GeMv'><span id='8GeMv'><b id='8GeMv'><form id='8GeMv'><ins id='8GeMv'></ins><ul id='8GeMv'></ul><sub id='8GeMv'></sub></form><legend id='8GeMv'></legend><bdo id='8GeMv'><pre id='8GeMv'><center id='8GeMv'></center></pre></bdo></b><th id='8GeMv'></th></span></q></dt></tr></i><div id='8GeMv'><tfoot id='8GeMv'></tfoot><dl id='8GeMv'><fieldset id='8GeMv'></fieldset></dl></div>
          <tbody id='8GeMv'></tbody>
      6. <tfoot id='8GeMv'></tfoot>
                <bdo id='8GeMv'></bdo><ul id='8GeMv'></ul>
                <legend id='8GeMv'><style id='8GeMv'><dir id='8GeMv'><q id='8GeMv'></q></dir></style></legend>

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

                  问题描述

                  我以此为参考在 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+插件

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

                  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:

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

                  它应该像我想要的那样工作,还是插件就是这样工作的?

                  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插件:

                  The description for "json" interceptor in the JSON Plugin:

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

                  • 内容类型"必须是application/json"
                  • JSON 内容必须格式正确,语法见 json.org.
                  • 动作必须有一个公开的setter";必须填充的字段的方法.
                  • 支持的填充类型有:Primitives (int,long...String)、Date、List、Map、Primitive Arrays、Other class(稍后会详细介绍)和 Array of Other 类.
                  • JSON 中要填充到列表或映射中的任何对象都将是 Map 类型(从属性到值的映射),任何整数都将是 Long 类型,任何十进制数都将是类型Double 和任何 List 类型的数组.

                  资源:

                  • Kickstart 常见问题解答
                  • 入门
                  • 常见问题解答李>
                  • 其他资源

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

                  上一篇:除了一个 servlet 之外,过滤器映射到 Struts2 吗? 下一篇:从jsp到action类获取List对象值

                  相关文章

                  1. <i id='kshxx'><tr id='kshxx'><dt id='kshxx'><q id='kshxx'><span id='kshxx'><b id='kshxx'><form id='kshxx'><ins id='kshxx'></ins><ul id='kshxx'></ul><sub id='kshxx'></sub></form><legend id='kshxx'></legend><bdo id='kshxx'><pre id='kshxx'><center id='kshxx'></center></pre></bdo></b><th id='kshxx'></th></span></q></dt></tr></i><div id='kshxx'><tfoot id='kshxx'></tfoot><dl id='kshxx'><fieldset id='kshxx'></fieldset></dl></div>
                  2. <legend id='kshxx'><style id='kshxx'><dir id='kshxx'><q id='kshxx'></q></dir></style></legend>
                      <bdo id='kshxx'></bdo><ul id='kshxx'></ul>
                  3. <small id='kshxx'></small><noframes id='kshxx'>

                      <tfoot id='kshxx'></tfoot>