我使用Android Spring Framework和Jackson解析器开发Android 3.1应用程序。我没有使用Maven,因为我不知道如何使用它。
我已按以下顺序将以下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
为什么我会收到此错误?
我怎么解决呢?
答案 0 :(得分: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
基于此:
但是使用ObjectMapper和Jackson 2.0中的其他Jackson类而不是Jackson 1。*