java - 休息/ Spring : get http-body info (plain/text) into variables/objects?

标签 java spring rest annotations

通过以下代码,我设法读取以纯文本/文本形式作为字符串出现的 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/

相关文章:

java - 整数文字

spring - 如何在 IntelliJ 中给定数据库连接生成 Spring REST 服务?

Spring Cloud 配置客户端无法解析值“${driverClassName}”中的占位符 'driverClassName'

c# - 大小超过64KB的请求无法完成(Asp.Net Core Web API)

php - 使用 Omnipay 将 PayPal REST API 集成到 Laravel - 需要卡参数

java - 使用连接池时出错

java - JPanel 位于另一个 JPanel 内 : Swing

java - 通过反射修改不可变的scala类字段

java - 设计带有2个外键的spring data jpa注释类

java - 使用 spring boot 进行 rest webservices 异常处理