1

我正在使用 Android Spring Framework 和 Jackson 解析器开发一个 Android 3.1 应用程序。我没有使用 Maven,因为我不知道如何使用它。

我已按以下顺序将以下 jar 添加到我的项目的类路径中:

  • 番石榴-11.0.2.jar
  • 杰克逊注释-2.0.0.jar
  • 杰克逊核心2.0.0.jar
  • jackson-databind-2.0.0.jar
  • spring-android-core-1.0.0.RC1.jar
  • spring-android-rest-template-1.0.0.RC1.jar

发出 REST 请求并进行 JSON 解析的类:

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import com.google.common.collect.Lists;

import es.viacognita.models.Form;

public class FormSpringController
{
    public static List<Form> LoadAll()
    {
        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.setAccept(Collections.singletonList(new MediaType("application","json")));
        HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);

        String url = "http://192.168.1.128/RestServiceImpl.svc/forms/";

        MappingJacksonHttpMessageConverter messageConverter = new MappingJacksonHttpMessageConverter();
        List<HttpMessageConverter<?>> messageConverters = Lists.newArrayList();
        messageConverters.add(messageConverter);

        RestTemplate restTemplate = new RestTemplate();
        restTemplate.setMessageConverters(messageConverters);

        ResponseEntity<Form[]> responseEntity = restTemplate.exchange(url, HttpMethod.GET, requestEntity, Form[].class);
        Form[] result= responseEntity.getBody();

        return Arrays.asList(result);
    }
}

这里:

MappingJacksonHttpMessageConverter messageConverter = new MappingJacksonHttpMessageConverter();

我收到此错误:

java.lang.VerifyError: org.springframework.http.converter.json.MappingJacksonHttpMessageConverter


为什么我会收到此错误?
我该如何解决?

4

1 回答 1

2

问题是 Spring 不适用于 Jackson 2.0。这已通过以下方式解决:

<bean id="jacksonMessageConverter"
          class="own.implementation.of.MappingJacksonHttpMessageConverter"/>

<bean class="org.springframework.web.servlet.mvc
             .annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="jacksonMessageConverter"/>
            </list>
        </property>
        <property name="requireSession" value="false"/>
    </bean>

并且own.implementation.of.MappingJacksonHttpMessageConverter基于此:

http://www.jarvana.com/jarvana/view/org/springframework/spring-web/3.0.0.RELEASE/spring-web-3.0.0.RELEASE-sources.jar!/org/springframework/http/converter /json/MappingJacksonHttpMessageConverter.java?format=ok

但是使用来自 Jackson 2.0 的 ObjectMapper 和其他 Jackson 类而不是 Jackson 1.*

于 2012-04-11T21:19:36.780 回答