2015-07-10 49 views
3
我目前使用的一段代碼來設置參數

REST調用的參數,我做使用restTemplate一個REST調用一個URL,它工作正常:使用地圖設置爲使用RestTemplate

MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>(); 
map.add("grant_type", grantType); 
map.add("client_id", clientId); 
map.add("client_secret", clientSecret); 
HttpEntity<?> entity = new HttpEntity<Object>(map); 
restTemplate.exchange("myurl", HttpMethod.POST, entity, Void.class); 

但如果我使用的是LinkedMultiValueMap,這是因爲我在網上查看;)

如果我用HashMap取代它,它也可以,所以任何人都可以告訴我使用LinkedMultiValueMap的好處嗎?

代碼HashMap

Map<String, String> map = new HashMap<String, String>(); 
map.put("grant_type", grantType); 
map.put("client_id", clientId); 
map.put("client_secret", clientSecret); 
HttpEntity<?> entity = new HttpEntity<Object>(map); 
restTemplate.exchange("myurl", HttpMethod.POST, entity, Void.class); 

我可以看到我們能夠保持在LinkedMultiValueMap參數的順序,但它並不在我的項目關係......所以,如果訂單的問題我仍然可以使用一個LinkedHashMap

編輯:

它看起來像我需要MultiValueMap如果我使用APPLICATION_FORM_URLENCODED和HashMap,該代碼拋出一個異常:

Map<String, String> map = new HashMap<String, String>(); 
map.add("grant_type", grantType); 
map.add("client_id", clientId); 
map.add("client_secret", clientSecret); 
HttpHeaders headers = new HttpHeaders(); 
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); 
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); 
HttpEntity<?> entity = new HttpEntity<Object>(map, headers); 
restTemplate.exchange("myurl", HttpMethod.POST, entity, Void.class); 

例外:

org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type [java.util.HashMap] and content type [application/x-www-form-urlencoded] 
     at org.springframework.web.client.RestTemplate$HttpEntityRequestCallback.doWithRequest(RestTemplate.java:784) 
     at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:567) 
     at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:530) 
     at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:448) 
     at uk.co.wowcher.marketplace.commons.rest.RestTemplateUtils.getForEntity(RestTemplateUtils.java:54) 
     at uk.co.wowcher.marketplace.submission.service.ParameterService.getProductParameterVOs(ParameterService.java:38) 
     at uk.co.wowcher.marketplace.submission.service.ParameterService$$FastClassBySpringCGLIB$$3f87231d.invoke(<generated>) 
     at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) 
     at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717) 
     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) 
     at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:85) 
     at uk.co.wowcher.marketplace.submission.logging.MarketplaceLogger.logMethodCalls(MarketplaceLogger.java:27) 
     at sun.reflect.GeneratedMethodAccessor72.invoke(Unknown Source) 
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
     at java.lang.reflect.Method.invoke(Method.java:483) 
     at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:621) 
     at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:610) 
     at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:68) 
     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
     at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) 
     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
     at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653) 
     at uk.co.xxx.marketplace.submission.service.ParameterService$$EnhancerBySpringCGLIB$$a52b80b.getProductParameterVOs(<generated>) 
     at uk.co.xxx.marketplace.submission.service.SubmissionService.getAgreementData(SubmissionService.java:449) 
     at uk.co.xxx.marketplace.submission.service.SubmissionService$$FastClassBySpringCGLIB$$92bbe798.invoke(<generated>) 
     at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) 
     at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717) 

因此,它看起來像有一個HashMap設置一些參數是不是一個好主意,在所有... 所以我刪除了,現在公認的答案,等待更多的解釋在這一點上... 而我有合適的轉換器(converters.add(new FormHttpMessageConverter());),我的意思是如果我使用MultiValueMap我沒有例外,所以HashMap是問題!

回答

0

MultiValueMap用於存儲單個KEY的多個值(Map(key:ArrayOfValues[])),而LinkedMultiValueMap執行相同的操作,但保留了插入順序。在你的情況下,HashMap就足夠了,因爲LinkedMultiValueMap的性能會比HashMap慢,並且HashMap對於您的方案不是必需的。

3

使用LinkedMultiValueMap而不是其他Map實現的唯一真正優點是它可以存儲多個值。

如果您需要爲同一個鍵傳遞多個值(例如,如果您需要傳遞一組表單複選框值),這非常有用。

請參閱JavaDoc here

就您而言,由於每個鍵只有一個值,因此您應該能夠安全地使用HashMap

-1

這是一個古老的問題,但我今天剛剛陷入這個問題。您可以使用 https://docs.spring.io/spring-social/docs/current/apidocs/org/springframework/social/support/FormMapHttpMessageConverter.html

編輯:

FormMapHttpMessageConverter.java是彈簧社會項目的一部分。

+0

雖然此鏈接可能會回答問題,但最好在此包含答案的基本部分,並提供供參考的鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 - [來自評論](/ review/low-quality-posts/17905113) – stan0

相關問題