我正在尝试使用 spring for android 发布到休息服务(我是新手)
restful 服务有这样的结构
@POST
@Path("/ider")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public SearchOutRO hashTrackInJSON(SearchInRO in);
with(缩短的目标代码):
public class SearchInRO implements Serializable {
private Double latitud;
private Double longitud;
}
public class SearchOutRO implements Serializable {
private Integer searchId;
}
所以我正在尝试这个(来自android)
String url = BASE_URL + "ider";
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setContentType(MediaType.APPLICATION_JSON);
MultiValueMap<String, String> body = new LinkedMultiValueMap<String, String>();
body.add("searchInRO[latitud]", String.valueOf(user.getLatitud()));
body.add("searchInRO[longitud]", String.valueOf(user.getLongitud()));
HttpEntity<?> requestEntity = new HttpEntity<Object>(body, requestHeaders);
RestTemplate restTemplate = new RestTemplate();
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
messageConverters.add(new FormHttpMessageConverter());
messageConverters.add(new StringHttpMessageConverter());
restTemplate.setMessageConverters(messageConverters);
try {
ResponseEntity<SearchOutRO> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, SearchOutRO.class);
HttpStatus status = response.getStatusCode();
if (status == HttpStatus.CREATED) {
return true;
} else {
return false;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
并得到这个例外:
org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type [org.springframework.util.LinkedMultiValueMap] and content type [application/json]
你能帮我指出我做错了什么吗?(以及如何使它正确)。
我认为它可能是“body”MultiValueMap。
提前致谢
编辑: 我尝试了@Krisl 的建议。我成功连接到服务器端,但似乎对象未正确编组。
WARNING: javax.xml.bind.UnmarshalException
- with linked exception:
[javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"latitud"). Expected elements are <{}searchInRO>]
我会很感激任何想法。