愉快的一天。
我在使用Postman简单地显示原始JSON格式的字符串时遇到了问题。
这是我的Java代码中的内容:
@RestController
public class HeroController {
@RequestMapping(method = {RequestMethod.POST}, value = "/displayHero")
@ResponseBody
public Map<String, String> displayInfo(String name){
//System.out.println(name);
Map<String, String> imap = new LinkedHashMap<String, String>();
map.put("hero", name);
return imap;
}
}
每次我在Postman中测试它时,我总是得到空的(如果我使用raw格式的话):
{
"hero": null
}
但是使用表单数据,另一方面,只显示我输入的内容。
{
"hero": "wolverine"
}
有什么信息,或者应该在Postman中做什么,以使这种原始格式工作,而不是表单数据?顺便说一下,原始格式值是字符集,在Header选项卡中,Content-Type值是应用程序/json;字符集=UTF-8。
谢谢你,祝你有一个美好的一天。
发布于 2018-05-31 18:13:12
在spring boot中尝试使用以下代码将请求正文作为JSON使用:
@RequestMapping(value = "/displayHero", method = POST, consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
@ResponseBody
public String displayInfo(HttpEntity<String> httpEntity) {
String json = httpEntity.getBody();
// json contains the plain json string
// now you can process the json object information as per your need
// and return output as per requirements.
return json;
}
此代码将接受POST请求的json body,然后将其作为响应返回。
https://stackoverflow.com/questions/50621485
复制相似问题