2012-10-26 71 views
4

當我使用restful webservice訪問rezgo示例api時我正在創建異常.i想要在示例對象中轉換此響應 package com.mycompany.hr.client;在Restful web服務調用中出現錯誤

import java.util.ArrayList; 
import java.util.Collections; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 

import org.apache.log4j.Logger; 
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.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.ModelAttribute; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RequestParam; 
import org.springframework.web.client.RestOperations; 
import org.springframework.web.client.RestTemplate; 
import org.springframework.ws.client.core.WebServiceTemplate; 
public class Main { 

    public static void main(String[] args) throws Exception { 

     List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>(); 
      acceptableMediaTypes.add(MediaType.APPLICATION_XML); 
      HttpHeaders headers = new HttpHeaders(); 
      headers.setAccept(acceptableMediaTypes); 
      //ResponseEntity<String> result = restTemplate.exchange("http://localhost:8080/spring-rest-provider/krams/person/{id}", HttpMethod.PUT, entity, String.class, id); 
      Map<String,String> urlParam=new HashMap<String,String>(); 
      urlParam.put("key","2D8-F3V0-X7C0-C6I"); 
      urlParam.put("transcode", "p103"); 
      urlParam.put("i", "p103"); 
      urlParam.put("t", "tag"); 
      urlParam.put("q", "nature"); 

      RestTemplate wsTemplate = new RestTemplate(); 
     ResponseEntity<TourList> tl=(wsTemplate).getForEntity("http://xml.rezgo.com/xml?key=2D8-F3V0-X7C0-C6I&transcode=p103&i=search_items&t=tag&q=nature",TourList.class,Collections.singleton(urlParam)); 

    } 

} 

這是我的代碼我geting下面的異常。

Exception in thread "main" java.lang.NoSuchMethodError: org.springframework.util.CollectionUtils.unmodifiableMultiValueMap(Lorg/springframework/util/MultiValueMap;)Lorg/springframework/util/MultiValueMap; 
    at org.springframework.web.util.UriComponents.<init>(UriComponents.java:100) 
    at org.springframework.web.util.UriComponentsBuilder.build(UriComponentsBuilder.java:222) 
    at org.springframework.web.util.UriComponentsBuilder.build(UriComponentsBuilder.java:212) 
    at org.springframework.web.util.UriTemplate.<init>(UriTemplate.java:70) 
    at org.springframework.web.client.RestTemplate$HttpUrlTemplate.<init>(RestTemplate.java:653) 
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:407) 
    at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:229) 
    at com.mycompany.hr.client.Main.main(Main.java:52) 

請幫我解決這個問題。

+2

看起來像使用彈簧罐的版本不匹配。 – Vikdor

回答

4

CollectionUtils.unmodifiableMultiValueMap在Spring 3.1中添加了,但並未出現在早期版本中。聽起來你正在使用spring-core 3.1構建你的應用程序,但是你的運行時環境使用的是早期版本。

也許你最近升級了Spring並且有一個陳舊的lib目錄,其中包含多個版本的spring-core?根據我的經驗,Java似乎總是選錯了(舊的),並且我得到了像這樣的錯誤。確保你的環境中沒有任何地方使用spring-core 3.0或更早的版本。