我正在进行REST API的测试自动化,而且我是Spring的新手。我有一个非常简单的代码,适用于JSON。
Map<String, String> vars = new HashMap<String, String>();
vars.put("format", "json");
vars.put("address", "1600 Amphitheatre Parkway, Mountain View, CA");
vars.put("sensor", "false");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON) ;
headers.setAccept(Arrays.asList(MediaType.APPLICAT ION_JSON));
HttpEntity<String> entity = new HttpEntity<String>(headers);
GeocodeResponsePage response = restTemplate.postForObject(url, entity, GeocodeResponsePage.class, vars);
System.out.println(response.getStatus());
但是只要我将JSON更改为XML
Map<String, String> vars = new HashMap<String, String>();
vars.put("format", "xml");
vars.put("address", "1600 Amphitheatre Parkway, Mountain View, CA");
vars.put("sensor", "false");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_XML);
headers.setAccept(Arrays.asList(MediaType.APPLICATION_XML));
HttpEntity<String> entity = new HttpEntity<String>(headers);
GeocodeResponsePage response = restTemplate.postForObject(url, entity,GeocodeResponsePage.class, vars);
System.out.println(response.getStatus());
我收到以下错误
org.springframework.web.client.RestClientException :
Could not extract response: no suitable HttpMessageConverter found for response
type [class com.safenet.GeocodeResponsePage] and content type
[application/xml;charset=UTF-8]...
GeocodeResponsePage也很简单。
@JsonIgnoreProperties(ignoreUnknown = true)
public class GeocodeResponsePage
{
private String status;
public String getStatus()
{
return status;
}
public void setStatus(String status)
{
this.status = status;
}
}
如果我使用String作为响应类,它就可以工作。
这是http://maps.googleapis.com/maps/api/geocode/{format}?address={address}&sensor={sensor}
的Google网络服务
请帮忙。我真的很感激任何帮助。已经花了一整天而且无法使它工作。
如果可能,请将代码放入问题解决方案中。
提前致谢