PDA

View Full Version : Exception in Spring Android with Spring Rest Template



arun.rocky4
Sep 16th, 2011, 03:51 AM
I am trying to access a rest webservice with Json,

below is the code in Activity class which i am using to retireve the values

List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
messageConverters.add(new FormHttpMessageConverter());
messageConverters.add(new StringHttpMessageConverter());
messageConverters.add(new MappingJacksonHttpMessageConverter());
restTemplate.setMessageConverters(messageConverter s);
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
UserCredentials result1 = restTemplate.getForObject(url,UserCredentials.clas s,userName, password);

I am getting the following error :

java.lang.NoSuchMethodError: org.codehaus.jackson.map.ObjectMapper.getTypeFacto ry

at org.springframework.http.converter.json.MappingJac ksonHttpMessageConverter.getJavaType(MappingJackso nHttpMessageConverter.java:116)
at org.springframework.http.converter.json.MappingJac ksonHttpMessageConverter.canRead(MappingJacksonHtt pMessageConverter.java:93)
at org.springframework.web.client.RestTemplate$Accept HeaderRequestCallback.doWithRequest(RestTemplate.j ava:529)
at org.springframework.web.client.RestTemplate.doExec ute(RestTemplate.java:460)
at org.springframework.web.client.RestTemplate.execut e(RestTemplate.java:425)
at org.springframework.web.client.RestTemplate.getFor Object(RestTemplate.java:223)


But when i hit the URL in browser, i am getting the response as {"userName":"admin","userRole":"admin","valid":"false"}

can u anyone help me on this :(

dutchman_mn
Sep 16th, 2011, 08:49 AM
Mmmmm.... Everything looks good.

What does your dependency entry look like in your pom.xml?

arun.rocky4
Sep 16th, 2011, 09:07 AM
I didnt used maven, but i got all these jars in my build path
commons-httpclient-3.1
jackson-core-asl-1.6.1
jackson-mapper-asl-1.6.1
jersey-client-1.9-ea04
spring-android-rest-template-1.0.0.M4.jar
spring-beans-3.0.5.RELEASE.jar
spring-core-3.0.5.RELEASE.jar

thanks

arun.rocky4
Sep 17th, 2011, 10:23 AM
dutchman , i have tried with maven and i got the same error, here i am posting my pom.xml


<dependencies>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<version>
2.3.3
</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.android</groupId>
<artifactId>spring-android-rest-template</artifactId>
<version>1.0.0.M4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.6.1</version>
</dependency>
</dependencies>


thanks

dutchman_mn
Sep 20th, 2011, 06:46 AM
My assumption in that you have some overlap. The following is a pom.xml I have been using successfully on a project.

<dependencies>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<version>2.1.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.android</groupId>
<artifactId>spring-android-rest-template</artifactId>
<version>1.0.0.M3</version>
</dependency>
<dependency>
<!-- Using Jackson for JSON marshaling -->
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.7.2</version>
</dependency>
</dependencies>

You do not need the spring-core/spring-beans, just the rest-template. I would upgrade to Jackson 1.7.2.

arun.rocky4
Sep 22nd, 2011, 04:23 AM
thanks for ur reply. Now my application is working fine. But i am facing problems gen\R.java in maven. Its getting generated in target\generated-sources\ folder. any idea ?

Roy Clarkson
Sep 23rd, 2011, 04:45 PM
Maven and the plugins use alternate directories from the standard Android SDK. If you are having issues with R.java refreshing, I will sometimes run "mvn clean" from the command line, and delete the gen folder. It seems that sometimes it gets a little stuck. The gen folder will be automatically recreated.

omercz
Oct 7th, 2011, 03:51 PM
Spring version m4 needs Jackson version > 1.8

I would recommend using Jackson 1.8.5
And spring m4 rest template and core