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

使用Spring RestTemplate-BadRequest 400发布

佟阳焱
2023-03-14

希望Spring大师能在这方面帮助我,Iam开发了一个多web服务应用程序,所有这些web服务都基于一个名为based-server的Jar,它得到了所有可以在任何需要的地方继承的基类。因此,在这个基础服务器项目中,我有一个BaseClient类,它有一个spring RestTemplate属性。当我试图在实际实现的web服务类中使用这个客户机(原因是继承)时,当我试图执行POST请求时,它会给我http 400错误请求错误。然而,它工作起来不会给GET请求带来任何麻烦。如果有人能指出我哪里弄错了,我就学徒。

基础服务器中的RestTemplate

<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
   <constructor-arg ref="httpClientFactory"/> 

    <property name="messageConverters">
        <list>
            <bean id="jsonViewResolver" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" >
               <property name="objectMapper" ref="JacksonObjectMapper" />               
               <property name="supportedMediaTypes">
                          <list>
                            <bean class="org.springframework.http.MediaType">
                               <constructor-arg value="application" />
                               <constructor-arg value="json" />
                               <constructor-arg value="#{T(java.nio.charset.Charset).forName('UTF-8')}"/>
                             </bean>
                          </list>
                </property>
            </bean> 
            <bean class="org.springframework.http.converter.FormHttpMessageConverter" />
            <bean class="org.springframework.http.converter.StringHttpMessageConverter" />                              
        </list>
    </property>
</bean>
<bean id="JacksonObjectMapper" class="org.codehaus.jackson.map.ObjectMapper" />

 <bean id="httpClient" class="org.apache.commons.httpclient.HttpClient">
    <constructor-arg ref="httpClientParams"/>
</bean>

 <bean id="httpClientParams" class="org.apache.commons.httpclient.params.HttpClientParams">

    <property name="connectionManagerClass"
              value="org.apache.commons.httpclient.MultiThreadedHttpConnectionManager"/>
</bean>

<bean id="httpClientFactory" class="org.springframework.http.client.CommonsClientHttpRequestFactory">
    <constructor-arg ref="httpClient"/>
</bean>

 <bean id="baseClient" class="com.tapgift.base.client.BaseClientImpl" >
    <property name="restTemplate" ref="restTemplate" />
 </bean>
@Override
public BaseResponse updateItemAvailableQuantity(final String token,final Integer qty, final Integer itemId) {
    LOG.info("Entering method: updateItemAvailableQuantity : param:- token= "+ token+", qty= "+ qty+", itemId= "+ itemId);
    MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
    map.add("token", token);
    map.add("itemId", itemId.toString());
    map.add("qty", qty.toString());
    return getRestTemplate().postForEntity("http://localhost:8080/merchant/api/merchant/update_item_qty",
    map, BaseResponse.class).getBody();
}
@RequestMapping(value = "/merchant/update_item_qty", method = RequestMethod.POST)
public @ResponseBody BaseResponse updateAvailableQuantity(@RequestParam("token") String token,
        @RequestParam("itemId") final Integer itemId, @RequestParam("qty") final Integer qty)
{

    return getItemSupport().updateAvailableQuantity(token, qty,itemId);
}
    13:21:41,851 ERROR [STDERR] org.springframework.web.client.HttpClientErrorException: 400 Bad Request

13:21:41,854 ERROR [STDERR]     at org.springframework.web.client.DefaultResponseErrorHandler.handle
Error(DefaultResponseErrorHandler.java:76)
13:21:41,865 ERROR [STDERR]     at org.springframework.web.client.RestTemplate.handleResponseError(R
estTemplate.java:486)
13:21:41,867 ERROR [STDERR]     at org.springframework.web.client.RestTemplate.doExecute(RestTemplat
e.java:443)
13:21:41,868 ERROR [STDERR]     at org.springframework.web.client.RestTemplate.execute(RestTemplate.
java:401)
13:21:41,869 ERROR [STDERR]     at org.springframework.web.client.RestTemplate.postForEntity(RestTem
plate.java:302)
13:21:41,870 ERROR [STDERR]     at com.tapgift.gift.client.impl.GiftClientImpl.updateItemAvailableQu
antity(GiftClientImpl.java:87)
13:21:41,871 ERROR [STDERR]     at com.tapgift.gift.support.impl.GiftSupportImpl.sendGiftToWinner(Gi
ftSupportImpl.java:152)
13:21:41,872 ERROR [STDERR]     at com.tapgift.gift.controller.GiftController.sendGiftToWinner(GiftC
ontroller.java:43)
13:21:41,873 ERROR [STDERR]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
13:21:41,874 ERROR [STDERR]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorI
mpl.java:57)
13:21:41,875 ERROR [STDERR]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodA
ccessorImpl.java:43)
13:21:41,876 ERROR [STDERR]     at java.lang.reflect.Method.invoke(Method.java:601)
13:21:41,877 ERROR [STDERR]     at org.springframework.web.method.support.InvocableHandlerMethod.inv
oke(InvocableHandlerMethod.java:212)
13:21:41,878 ERROR [STDERR]     at org.springframework.web.method.support.InvocableHandlerMethod.inv
okeForRequest(InvocableHandlerMethod.java:126)
13:21:41,879 ERROR [STDERR]     at org.springframework.web.servlet.mvc.method.annotation.ServletInvo
cableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
13:21:41,880 ERROR [STDERR]     at org.springframework.web.servlet.mvc.method.annotation.RequestMapp
ingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
13:21:41,881 ERROR [STDERR]     at org.springframework.web.servlet.mvc.method.annotation.RequestMapp
ingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
13:21:41,882 ERROR [STDERR]     at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodA
dapter.handle(AbstractHandlerMethodAdapter.java:80)
13:21:41,884 ERROR [STDERR]     at org.springframework.web.servlet.DispatcherServlet.doDispatch(Disp
atcherServlet.java:900)
13:21:41,884 ERROR [STDERR]     at org.springframework.web.servlet.DispatcherServlet.doService(Dispa
tcherServlet.java:827)
13:21:41,886 ERROR [STDERR]     at org.springframework.web.servlet.FrameworkServlet.processRequest(F
rameworkServlet.java:882)
13:21:41,887 ERROR [STDERR]     at org.springframework.web.servlet.FrameworkServlet.doPost(Framework
Servlet.java:789)
13:21:41,888 ERROR [STDERR]     at javax.servlet.http.HttpServlet.service(HttpServlet.java:754)
13:21:41,889 ERROR [STDERR]     at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
13:21:41,891 ERROR [STDERR]     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(
ApplicationFilterChain.java:324)
13:21:41,892 ERROR [STDERR]     at org.apache.catalina.core.ApplicationFilterChain.doFilter(Applicat
ionFilterChain.java:242)
13:21:41,894 ERROR [STDERR]     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrap
perValve.java:275)
13:21:41,895 ERROR [STDERR]     at org.apache.catalina.core.StandardContextValve.invoke(StandardCont
extValve.java:161)
13:21:41,896 ERROR [STDERR]     at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(Sec
urityAssociationValve.java:181)
13:21:41,897 ERROR [STDERR]     at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValv
e.event(CatalinaContext.java:285)
13:21:41,898 ERROR [STDERR]     at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValv
e.invoke(CatalinaContext.java:261)
13:21:41,900 ERROR [STDERR]     at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContext
Valve.java:88)
13:21:41,901 ERROR [STDERR]     at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.i
nvoke(SecurityContextEstablishmentValve.java:100)
13:21:41,902 ERROR [STDERR]     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostVal
ve.java:159)
13:21:41,904 ERROR [STDERR]     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportVal
ve.java:102)
13:21:41,905 ERROR [STDERR]     at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(Cac
hedConnectionValve.java:158)
13:21:41,906 ERROR [STDERR]     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngin
eValve.java:109)
13:21:41,907 ERROR [STDERR]     at org.jboss.web.tomcat.service.request.ActiveRequestResponseCacheVa
lve.invoke(ActiveRequestResponseCacheValve.java:53)
13:21:41,909 ERROR [STDERR]     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter
.java:362)
13:21:41,910 ERROR [STDERR]     at org.apache.coyote.http11.Http11Processor.process(Http11Processor.
java:877)
13:21:41,911 ERROR [STDERR]     at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.p
rocess(Http11Protocol.java:654)
13:21:41,912 ERROR [STDERR]     at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.jav
a:951)
13:21:41,913 ERROR [STDERR]     at java.lang.Thread.run(Thread.java:722)
13:21:41,914 INFO  [STDOUT] FATAL: com.tapgift.gift.support.impl.GiftSupportImpl - ERROR: 400 Bad Re
quest

共有1个答案

阎枫涟
2023-03-14

嗯,我终于找到了原因,这确实是一个非常有趣的解决方案。我所做的是更改RestTemplatebean的MessagesConverters的bean的顺序。

早些时候,

<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<property name="messageConverters">
    <list>
        <bean id="jsonViewResolver" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" >
           <property name="objectMapper" ref="JacksonObjectMapper" />               
           <property name="supportedMediaTypes">
                      <list>
                        <bean class="org.springframework.http.MediaType">
                           <constructor-arg value="application" />
                           <constructor-arg value="json" />
                           <constructor-arg value="#{T(java.nio.charset.Charset).forName('UTF-8')}"/>
                         </bean>
                      </list>
            </property>
        </bean> 
        <bean class="org.springframework.http.converter.FormHttpMessageConverter" />
        <bean class="org.springframework.http.converter.StringHttpMessageConverter" />                              
    </list>
</property>
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
   <constructor-arg ref="httpClientFactory"/> 

    <property name="messageConverters">
        <list>
            <bean class="org.springframework.http.converter.FormHttpMessageConverter" />
            <bean class="org.springframework.http.converter.StringHttpMessageConverter" /> 
            <bean id="jsonViewResolver" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" >
               <property name="objectMapper" ref="JacksonObjectMapper" />               
               <property name="supportedMediaTypes">
                          <list>
                            <bean class="org.springframework.http.MediaType">
                               <constructor-arg value="application" />
                               <constructor-arg value="json" />
                               <constructor-arg value="#{T(java.nio.charset.Charset).forName('UTF-8')}"/>
                             </bean>
                          </list>
                </property>
            </bean> 

        </list>
    </property>
</bean>

而且奏效了。这不是一个有趣的解决方案吗?

 类似资料:
  • 我是全新的RestTemboard和基本上在REST API也。我想通过Jira REST API检索我的应用程序中的一些数据,但取回401未经授权。在jira rest api留档上找到并发表文章,但不知道如何将其重写为java,因为示例使用curl的命令行方式。我将感谢任何建议或建议如何重写: 进入java使用SpringRest模板。其中ZnJlZDpmcmVk是用户名:密码的Bas64编码

  • 是否可以使用SpringRESTTemplate将原始JSON传递给RESTAPI? 我正在尝试以下几点: 当我调用这个请求时,我得到一个HTTP 400错误响应,这意味着错误的请求。但是,所有标头和JSON正文都与使用我拥有的HTTP客户端提交的相同。 相比之下,当我创建MyRequest对象并在HttpEntity上设置它时,下面的工作原理很好: 因此,我想知道如何使用字符串格式的原始JSON

  • 给Rest服务打电话 http://acme.com/app/widget/123 返回: 此客户端代码的工作原理是: 但是,http://acme.com/app/widget/456返回: 但此客户端代码引发了一个异常: 我试过: 第二次调用只是抛出了另一个HttpClientErrorException,而且它觉得调用服务两次是不对的。 是否有一种方法可以调用该服务一次,并在成功时将响应解析

  • web API以,但是消息的格式就像是JSON一样,例如。 在Spring中,此消息使用RestTemplate处理,JSON自动映射到POJO, 这会产生以下错误: org.springframework.web.client.RestClientExcture:无法提取响应:没有找到适合响应类型[class api的HttpMessageConzer。ModelDto]和内容类型[文本/普通;

  • 这就是我如何尝试使用RestTemplate调用服务器, 这就是我如何使用它(用于测试) 我的登录响应类及其子类, null null null null 公共类BaseResponse{ 受保护字符串StatusCode; 我的问题是,1。为什么调用服务器时会出现此错误 信息:org.springframework.beans.factory.support.defaultListableBea

  • 问题内容: 我一直在尝试从SO和其他站点上的大量示例中学习,但是我无法弄清楚为什么我一起学习的示例无法正常工作。我正在构建一个小型的概念验证应用程序,该应用程序可以识别语音并将其(文本)作为POST请求发送到node.js服务器。我确认了语音识别功能,并且服务器正在通过常规浏览器访问获得连接,因此我被认为是问题出在应用程序本身。我想念一些小而愚蠢的东西吗?没有引发任何错误,但是服务器从不识别连接。

  • 问题内容: 我想使用wget将图片(使用身份验证令牌“ AUTH_1624582364932749DFHDD”)上传到远程服务器到“ test”文件夹。 该命令不起作用(授权失败),我想确保它与语法无关: 有什么建议么? 问题答案: Wget当前仅支持x-www-form-urlencoded数据。不是用于将文件作为表单附件传输,而是需要格式为的数据。 并以相同的方式工作:唯一的不同是,您可以在命

  • 问题内容: 最终用户应该通过文件浏览器上传excel文件: 动作: 服务内容: 该文件应发送到使用Spring Boot接受的多部分文件构建的POST REST端点。端点无法识别这样发送文件 如何发布Excel文件? 编辑:我现在正在尝试发布文件列表: 但是data.append(’file’,files [i]); 总是返回未定义的 问题答案: 通过表单将文件传递到后端。 如果您现在检查发送到后