问题描述
我以此为参考在 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:
它会为我创建一个包含传入的新正文和标题的收据.
and it creates me a receipt with the new body and title passed in.
这不起作用:
这里有一些代码:
struts.xml
:
ReceiptController.java
:
它应该像我想要的那样工作,还是插件就是这样工作的?
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.
中对 "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 插件 - 使用正文而不是查询字符串作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!