服务器给我一个内容类型为text/json
的响应,我需要将其使用到Java类中。当服务器的响应为内容类型application/json
时,我可以做到这一点。如何使用Spring Boot消耗application/json
内容类型时实现与使用text/json
内容类型时相同的功能?
我尝试创建一个HttpHeaders
对象,然后创建setContentType
方法,但据我所知,MediaType
选项均不适用于text/json
。
Request req = new Request();
String url = "<url>";
HttpHeaders headers = new HttpHeaders();
headers.setContentType( MediaType.TEXT_JSON ); // this isn't valid but is where I have tried setting the content-type to text/json
HttpEntity< Request > entity = new HttpEntity<>( req, headers );
ResponseEntity< Response > resp =
restTemplate.exchange( url, HttpMethod.POST, entity, Response.class );
Request
是确定服务器响应的类,而Response
是返回的json的Java表示。
理想情况下,返回的json将存储到Response
类中,但我收到此错误:InvocationTargetException: Failed to execute CommandLineRunner: Could not extract response: no suitable HttpMessageConverter found for response type [class Response] and content type [text/json]