通过以下代码,我设法读取以纯文本/文本形式作为字符串出现的 http-post 信息,并使用字符串对其进行响应。
我的问题是:我假设 http-body 将包含以下形式的信息:
Name="myName"&place="here"&age="40"
我收到了整个字符串,但在寻找将这些对放入变量或对象中的方法时迷失了。并将我的回应放入这个模式中 你能帮忙吗?
@RequestMapping( value="/info",
method = RequestMethod.POST, consumes = {"text/plain"} )
public ResponseEntity<String> receiveBody(@RequestBody String vtext )
{
....
....
return new ResponseEntity<String> (vtext, headers, HttpStatus.OK);
}
}
最佳答案
我认为处理此类问题的更好解决方案是 @RequestParam
注解。假设您有一个如下所示的表单:
<input type="text" name="name" id="name"/>
<input type="text" name="age" id="age"/>
<input type="text" name="place" id="place"/>
现在您所要做的就是在方法的参数列表中添加适当的注释。示例:
@RequestMapping( value="/info", method = RequestMethod.POST)
public ResponseEntity<String> receiveBody(@RequestParam("name") String name, @RequestParam("place") String place, @RequestParam("age") int age)
{
//you can do something with variables age, name and place here
....
return new ResponseEntity<String> ("someResponse", headers, HttpStatus.OK);
}
关于java - 休息/ Spring : get http-body info (plain/text) into variables/objects?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28308521/