Spring - java http get请求,返回字符串多加了一层引号“

    公司其他项目的同事调用我们模块的一个GET接口时,发现返回的字符串多了一层引号,刚看到这个问题,一脸疑惑,String类型的字符串不就是应该是这样的吗?

String result1= HttpUtil.get("http://localhost:8080/demo-service/v1/api/email/content?id=27");
System.out.println(result1);

输出:"https://demofile.aliyun.com/27.html"

    自己写了一个简单的HTTP Get请求一下,果然是这个样子的,但是我是直接从数据库拿到的这个url的啊,怎么跟平常的String不太一样?

    平常普通的字符串是这样的:

String result3 = "https://demofile.aliyun.com/27.html";
System.out.println(result3);

输出:https://demofile.aliyun.com/27.html

    神奇哦~~~

    猛然瞥见自己写的@RequestMapping,顿时觉得找到问题所在了!

    看来是把返回的数据类型配置成了application/json,怪不得加了一层双引号,改成文本类型就好了text/plain

修改前:
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation("获取邮件信息-对外api")
    @RequestMapping(value = "/email/content", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "Authorization", value = "Authorization", required = true, dataType = "string", paramType = "header")})
    public String getEmailInfo(@RequestParam(value = "id") Integer id) {
        return emailService.getEmailUriFormOss(id);
    }

修改后:
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation("获取邮件信息-对外api")
    @RequestMapping(value = "/email/content", method = RequestMethod.GET, produces = MediaType.TEXT_PLAIN)
    @ApiImplicitParams({@ApiImplicitParam(name = "Authorization", value = "Authorization", required = true, dataType = "string", paramType = "header")})
    public String getEmailInfo(@RequestParam(value = "id") Integer id) {
        return emailService.getEmailUriFormOss(id);
    }

posted @   zhangdaopin  阅读(683)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示