当前位置: 首页 > 知识库问答 >
问题:

使用映射为使用RestTemplate的rest调用设置参数

姬翰林
2023-03-14
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);

如果我用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

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());),我的意思是如果我使用多值地图,我就没有异常,所以HashMap就是问题所在!

共有1个答案

谷梁英资
2023-03-14

与其他map实现相比,使用LinkedMultiValueMap的唯一真正优势是它可以存储多个值。

如果需要为同一个键传递多个值(例如,如果需要传递一组表单复选框值),这将非常有用。

请参见这里的JavaDoc。

 类似资料:
  • 如果需要从应用程序调用远程REST服务,可以使用Spring Framework的RestTemplate类。 由于RestTemplate实例在使用之前通常需要进行自定义,因此Spring Boot不提供任何单个自动配置的RestTemplate bean。 但是,它会自动配置RestTemplateBuilder,可用于在需要时创建RestTemplate实例。 自动配置的RestTempla

  • 我正在尝试使用客户端发送到映射(或JSON对象)中的JSON对象。我使用的是Jersey2.22.1,默认情况下它使用的是Moxy。尝试了如下所示的HashMap,但没有成功。它给出415错误-“不支持的媒体类型” 尝试使用自定义类以及包装一个映射。同样的错误。有谁能帮我,让我知道如何处理地图。 作为临时解决方案,我使用下面代码。但想知道如何正确地使用Map完成此操作

  • 我正在尝试使用这个api,这是链接 我已经在我的控制器中尝试过了 在我的日程安排程序中 而他们都没能拿到汇率。 为什么我不能使用rest模板获得汇率。

  • 我从地图生成一个复选框列表。现在如何设置键的值(false/true),现在我可以在UserConfig中下载它,以便在项目的其余部分使用该值。 我的看法: 我的类UserConfig: 控制器 我不使用数据库。它只需要保存用于生成报告的值

  • 在进行web客户端调用并使用前一个Mono的响应后,我无法在映射中获得填充值。以下是我所尝试的代码。parameters.size()的值为零。无法获得该值未被文件化的原因。我基本上希望从该方法返回年龄(而不是单对象)。使用block给出一个错误block()/blockfirst()/blocklast()是blocking,这在thread Reactor-http-nio-3中是不支持的。

  • 问题内容: 我正在尝试使用RestTemplate和Jackson json转换器调用Restful JSON服务。现在,为了调用该服务,我需要传递一个安全性cookie。我可以通过使用URLConnection来实现(请参见下面的代码) RestTemplate中与此并行的是什么?这是我一直在使用RestTemplate调用Restful Service的代码片段: 我无法弄清楚在使用RestT