【问题标题】:Consume Html response using restTemplate使用 restTemplate 使用 Html 响应
【发布时间】:2019-08-23 14:22:28
【问题描述】:

我想使用 restTemplate 来使用 html 响应并将该响应重新运行到我的 html 中,这样我就可以将这些内容添加到我的 html 页面,但是遇到错误尝试了很多替代方法,但没有运气

我想使用每种类型的响应并通过 ajax 将其原样返回到我的 html/jsp 并在 div 中呈现该内容。

HTML (ajax call) --- Spring MVC (rest call to 3rd party) --- 应用程序返回 html

代码

@RequestMapping(value = "/xyz-service/**", method = {RequestMethod.GET, RequestMethod.PUT},produces="application/json;charset=UTF-8")
    public Object mirrorRest(HttpServletRequest request) {
        String url = request.getRequestURI();

        return restTemplate.getForObject("http://xyz-service:8080"+url , String.class);
    }

我能够调用我的服务方法,将 html 重新调整为响应但得到错误

"Could not extract response: no suitable HttpMessageConverter found for response type [class java.lang.String] and content type [text/html;charset=UTF-8]"
]

【问题讨论】:

标签: spring-mvc resttemplate


【解决方案1】:

不,你不能。 HTML 页面不是 json 对象:REST 模板旨在使用 RestServices。 您应该使用 jdk 中的 URLConnection

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 2015-10-07
    • 2019-06-09
    • 2021-09-18
    相关资源
    最近更新 更多