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

Spring RestTemplate设置Accept为application/json仍返回text/html报错如何解决

异常原因

"Could not extract response: no suitable HttpMessageConverter found for response type [interface java.util.Map] and content type [text/html;charset=iso-8859-1]"
该异常本质是接口实际返回的内容格式为HTML,和你期望接收的JSON格式不匹配,RestTemplate找不到对应转换器解析内容。具体触发原因如下:

  • 目标接口对请求User-Agent头做了校验:Postman发起请求时会自动携带标准的PostmanRuntime/xxx格式User-Agent,而RestTemplate默认发送的User-Agent为Java/版本号格式,被接口识别为异常请求,直接返回了HTML错误页,忽略了你设置的Accept头。
  • GET请求设置Content-Type: application/json无实际作用,该头用于描述请求体格式,GET请求无请求体,该配置不会影响接口返回结果。
解决方案

方案1:添加模拟的User-Agent头(核心修复)

在请求头中添加和Postman一致的User-Agent,绕过接口的UA校验,修改后的请求头配置如下:

HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
// 新增模拟Postman的User-Agent头
headers.add("User-Agent", "PostmanRuntime/7.32.3");
HttpEntity entity = new HttpEntity(headers);

方案2:URL编码请求参数

你拼接的URL中cut参数可能包含空格、特殊符号,需要先做URL编码避免请求参数解析错误,示例如下:

// 先对文本内容做UTF-8编码
String encodedText = URLEncoder.encode(cut, StandardCharsets.UTF_8.name());
String url = "http://api.dbpedia-spotlight.org/en/annotate?text=" + encodedText + "&confidence=0.5&support=0&spotter=Default&disambiguator=Default&policy=whitelist&types=&sparql=";

方案3:兼容接口返回HTML的场景(可选)

如果需要兼容接口返回HTML的场景,避免直接抛出转换异常,可以给RestTemplate的Jackson消息转换器添加支持text/html类型:

RestTemplate restTemplate = new RestTemplate();
// 扩展Jackson转换器支持的媒体类型
for (HttpMessageConverter<?> converter : restTemplate.getMessageConverters()) {
    if (converter instanceof MappingJackson2HttpMessageConverter) {
        List<MediaType> mediaTypes = new ArrayList<>(converter.getSupportedMediaTypes());
        mediaTypes.add(MediaType.TEXT_HTML);
        ((MappingJackson2HttpMessageConverter) converter).setSupportedMediaTypes(mediaTypes);
        break;
    }
}
完整可运行代码示例
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Map;

public class DbpediaRequest {
    public static void main(String[] args) throws Exception {
        HttpHeaders headers = new HttpHeaders();
        headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
        headers.add("User-Agent", "PostmanRuntime/7.32.3");
        HttpEntity<Void> entity = new HttpEntity<>(headers);

        String cut = "你的文本内容";
        String encodedCut = URLEncoder.encode(cut, StandardCharsets.UTF_8.name());
        String url = "http://api.dbpedia-spotlight.org/en/annotate?text=" + encodedCut + "&confidence=0.5&support=0&spotter=Default&disambiguator=Default&policy=whitelist&types=&sparql=";

        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<Map> responseEntity = restTemplate.exchange(url, HttpMethod.GET, entity, Map.class);
        System.out.println(responseEntity.getBody());
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 07:24:03