java将POJO映射到应用程序/xwwForMurlen,并使用自定义表单键进行编码
我正在使用RoboSpice和Spring Android来执行REST api调用。 发送数据时,需要将内容类型设置为application/x-www-form-urlencoded
比如说我有一个像这样的Pojo课程:
public class Pojo {
private String pojoAttribute;
// getter + setter
}
以下是我的申请课程:
public class PojoRequest extends AbstractApiRequest<PojoResult> {
private Pojo pojo;
public PojoRequest(Pojo pojo) {
super(PojoResult.class);
this.pojo = pojo;
}
@Override
public PojoResult loadDataFromNetwork() throws Exception {
HttpEntity<Pojo> requestEntity = new HttpEntity<Pojo>(pojo, getDefaultHeaders());
String url = buildUrlForPath("mypath");
RestTemplate restTemplate = getRestTemplate();
restTemplate.getMessageConverters().add(new FormHttpMessageConverter());
return restTemplate.postForObject(url, requestEntity, PojoResult.class);
}
}
那么,假设我需要发送这个身体:
pojo_attribute=foo
现在我的代码不起作用了,因为FormHttpMessageConverter不处理POJO
我希望能够做到的是:
public class Pojo {
@FormProperty("pojo_attribute")
private String pojoAttribute;
// getter + setter
}
使用神奇的HttpMessageConverter将我的POJO转换为key=value格式,通过检查@FormProperty(不存在)注释,将pojoAttribute转换为POJO_属性,就像Jackson对@JsonProperty所做的那样
我的问题是:
-是否可以对现有的类/注释执行此操作
-还有其他类似的方法吗
-如果我创建自己的HttpMessageConverter和一组注释来实现这一点,会不会有些过头了
共 (0) 个答案