Java 实体的Spring rest模板获取错误为400
我收到以下异常Java 实体的Spring rest模板获取错误为400,java,spring,spring-rest,Java,Spring,Spring Rest,我收到以下异常org.springframework.web.client.HttpClientErrorException:400错误请求 在这个语句中,final ResponseEntity=restTemplate.getForEntity(“http://localhost:12001/api/profiles/bymsisdn/0747894146“,String.class) 但我正在使用 curl url-X GET“” 附加说明: resttemplate=新建resttemp
org.springframework.web.client.HttpClientErrorException:400错误请求
在这个语句中,final ResponseEntity=restTemplate.getForEntity(“http://localhost:12001/api/profiles/bymsisdn/0747894146“,String.class)代码>
但我正在使用
curl url-X GET“”
附加说明:
resttemplate=新建resttemplate();//没有添加头或额外的转换器,因为我认为这不是必需的,因为使用curl可以很好地测试http客户端的性能
我认为RestTemplate正在放置服务器响应的Accept
头。因此,您可能需要编辑您的请求标题:
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity<String> entity = new HttpEntity<String>(headers);
restTemplate.exchange("http://localhost:12001/api/profiles/bymsisdn/0747894146", HttpMethod.GET, entity, String.class);
RestTemplate RestTemplate=new RestTemplate();
HttpHeaders=新的HttpHeaders();
setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity=新的HttpEntity(标题);
restTemplate.exchange(“http://localhost:12001/api/profiles/bymsisdn/0747894146,HttpMethod.GET,entity,String.class);
请注意,这是一个暂定的解决方案,适合您的服务器。最终字符串uri={data}”
RestTemplate RestTemplate=新RestTemplate()
MyResponse result=restTemplate.getForObject(uri,MyResponse.class,valData);curl在未指定接受头时使用/作为接受头。如果未指定接受头,则使用restTemplate,它会尝试为响应类型(以字符串为例)找到合适的MIME类型。对于字符串类,根据您的配置,可以匹配可能与目标端点生成的内容不匹配的text/*MIME类型
尝试使用RestTemplate的exchange重载方法明确指定需要处理的内容类型此答案可能会有所帮助;我在发布问题之前已经尝试过了。不走运,谢谢