You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot调用外部Get API:如何将字符串响应转为JSON格式

解决Spring Boot Get API返回原始JSON格式响应的问题

你的代码当前返回字符串格式响应,核心原因是返回类型为String时,Spring Boot会将其识别为纯文本(text/plain)而非JSON。同时方法使用private修饰会导致外部无法访问接口,需要先修正这一点。以下是几种可行的解决方案:

方案1:直接返回Object类型(快速实现)

将返回类型改为Object,让RestTemplate自动解析响应为通用对象,Spring Boot会自动将其序列化为JSON格式返回:

@RestController
public class HelloController {
    @GetMapping("/discover")
    public Object getApi(){
        String url ="https://discover.search.hereapi.com/v1/discover?at=18.49093,73.8332&limit=2&q=restaurant&in=countryCode:IND&apiKey=hhV0nHGeHgrg5LACzw7dDJNe49bYCNvkCWxV94LlHno";
        RestTemplate restTemplate = new RestTemplate();
        return restTemplate.getForObject(url, Object.class);
    }
}

方案2:创建实体类映射响应(类型安全,推荐)

根据Here API的响应结构定义对应的实体类,让RestTemplate直接解析为实体对象,返回时Spring会自动序列化为JSON:

// 示例实体类,根据实际API响应字段补充
public class DiscoverResponse {
    private List<PlaceItem> items;
    // 其他字段、getter/setter方法

    public static class PlaceItem {
        private String title;
        private Address address;
        // 其他字段、getter/setter方法
    }

    public static class Address {
        private String label;
        // 其他字段、getter/setter方法
    }
}

修改Controller代码:

@RestController
public class HelloController {
    @GetMapping("/discover")
    public DiscoverResponse getApi(){
        String url ="https://discover.search.hereapi.com/v1/discover?at=18.49093,73.8332&limit=2&q=restaurant&in=countryCode:IND&apiKey=hhV0nHGeHgrg5LACzw7dDJNe49bYCNvkCWxV94LlHno";
        RestTemplate restTemplate = new RestTemplate();
        return restTemplate.getForObject(url, DiscoverResponse.class);
    }
}

方案3:返回ResponseEntity并指定JSON类型

如果需要保留字符串格式但强制返回JSON类型,可以通过ResponseEntity设置响应头:

import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

@RestController
public class HelloController {
    @GetMapping("/discover")
    public ResponseEntity<String> getApi(){
        String url ="https://discover.search.hereapi.com/v1/discover?at=18.49093,73.8332&limit=2&q=restaurant&in=countryCode:IND&apiKey=hhV0nHGeHgrg5LACzw7dDJNe49bYCNvkCWxV94LlHno";
        RestTemplate restTemplate = new RestTemplate();
        String result = restTemplate.getForObject(url,String.class);
        return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_JSON)
                .body(result);
    }
}

优化建议:复用RestTemplate实例

不要在方法内每次创建RestTemplate,建议通过配置类注入单例实例:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class RestTemplateConfig {
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

然后在Controller中注入使用:

@RestController
public class HelloController {
    private final RestTemplate restTemplate;

    public HelloController(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    @GetMapping("/discover")
    public Object getApi(){
        String url ="https://discover.search.hereapi.com/v1/discover?at=18.49093,73.8332&limit=2&q=restaurant&in=countryCode:IND&apiKey=hhV0nHGeHgrg5LACzw7dDJNe49bYCNvkCWxV94LlHno";
        return restTemplate.getForObject(url, Object.class);
    }
}

内容的提问来源于stack exchange,提问作者Akshay Ghawate

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 04:55:29