我有一种情况,我使用spring andorid apis来做PUT操作。服务(后端)需要自定义对象的xml表示。而Content-Type为" Raw"。我使用SimpleXmlHttpMessageConverter()将自定义对象序列化为xml,并将内容类型设置为" Raw"。但这是失败的,并抛出异常,
" org.springframework.web.client.RestClientException:无法写入请求:找不到合适的HttpMessageConverter请求类型[com.test.myClass]"。
自定义对象类如下所示,
@Root
public class MyClass
{
@Element
private long id_;
@Element
private String info_;
@Element
private long token_;
public long getId() {
return id_;
}
public String getInfo() {
return info_;
}
public long getToken() {
return token_;
}
public void setId(long id_) {
this.id_ = id_;
}
public void setInfo(String info_) {
this.info_ = info_;
}
public void setToken(long token_) {
this.token_ = token_;
}
}
请求PUT操作的代码如下,
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new SimpleXmlHttpMessageConverter());
HttpHeaders headers = new HttpHeaders();
headers.setContentType(new MediaType("application","Raw"));
HttpEntity<MyClass> requestEntity = new HttpEntity<MyClass>(myClass, headers);
ResponseEntity<String> responseEntity = restTemplate.exchange(url,
HttpMethod.PUT, requestEntity, String.class);
HttpStatus status = responseEntity.getStatusCode();
这会在调用restTemplate交换期间抛出异常。 如何解决这个问题,有什么建议吗? Spring Android是否支持Raw类型?