首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用邮递器值发送原始JSON为空

使用邮递器值发送原始JSON为空
EN

Stack Overflow用户
提问于 2018-05-31 17:54:05
回答 1查看 14.4K关注 0票数 -1

愉快的一天。

我在使用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

谢谢你,祝你有一个美好的一天。

EN

回答 1

Stack Overflow用户

发布于 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,然后将其作为响应返回。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50621485

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档