使用spring for android休息POST对象

时间:2013-03-12 05:53:45

标签: java android spring rest

我正在尝试使用spring for android发布到休息服务(我是新手)

宁静的服务有这种结构

@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>]

我很感激任何想法。

3 个答案:

答案 0 :(得分:4)

似乎你的假设是正确的,MultiValueMap造成了麻烦。

试试这个

而不是使用MultiValueMap在SearchInRO对象中设置lat和lng并添加它。

更改

HttpEntity<?> requestEntity = new HttpEntity<Object>(body, requestHeaders);

SearchInRO searchInRO = new SearchInRO();

设置lat和lng

HttpEntity<?> requestEntity = new HttpEntity<Object>(searchInRO , requestHeaders);

还要添加MappingJacksonHttpMessageConverter

 messageConverters.add(new MappingJacksonHttpMessageConverter());

更新: 我刚刚检查了一个使用REST的旧项目,并注意到我也使用了jersey-json jar(我的maven依赖)。
试试这个添加jersey-json jar到你的类路径并更新web.xml,如下所示

<init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>

答案 1 :(得分:0)

我认为你缺少HttpMessageConverter bean。如果您正在生成和使用JSON数据,那么应该有一些方法可以将Java POJO转换为JSON,反之亦然。 尝试在上下文xml文件中添加以下bean。

<beans:bean id="jacksonMessageChanger"
    class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
    <beans:property name="supportedMediaTypes" value="application/json" />
</beans:bean>

<beans:bean
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <beans:property name="messageConverters">
        <util:list id="beanList">
            <beans:ref bean="jacksonMessageChanger" />
        </util:list>
    </beans:property>
</beans:bean>

此外,您还需要为jackson mappers添加maven依赖项。

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-core-asl</artifactId>
    <version>1.8.5</version>
</dependency>
<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.8.5</version>
</dependency>

答案 2 :(得分:0)

在设置HttpEntity之前,使用带有编写器的jackson的diff方法转到字符串...

ref

ObjectNode rootOb = new ObjectMapper().createObjectNode();
  { create obj nodes , adding each to rootNode }
  {jackson api to go to string }

    StringWriter writer = new StringWriter();
    new ObjectMapper().writeValue(writer, rootOb);
    String poststr=writer.toString();

httpPost.setEntity(new StringEntity(poststr));