title: POST request with Spring RestTemplate- BadRequest 400
body: <p>Hope a Spring guru could help me on this,
  Iam developing a multi web servces application, All these Web Services are based on a Jar called based-server, which got all base classes which can be inherited where ever required.
  So this base-server project i have a BaseClient class which has a spring RestTemplate property.
  when i try to use this client (of cause inheriting) in a actual implemented web service class, when i try to do POST request it gives me http 400 Bad Request error. However it works without giving any trouble for GET requests.
  Appriciate if someone can point out where i got it wrong.</p>
  
  <p>RestTemplate in Base server</p>
  
  <pre><code>&lt;bean id="restTemplate" class="org.springframework.web.client.RestTemplate"&gt;
     &lt;constructor-arg ref="httpClientFactory"/&gt; 
  
      &lt;property name="messageConverters"&gt;
          &lt;list&gt;
              &lt;bean id="jsonViewResolver" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" &gt;
                 &lt;property name="objectMapper" ref="JacksonObjectMapper" /&gt;               
                 &lt;property name="supportedMediaTypes"&gt;
                            &lt;list&gt;
                              &lt;bean class="org.springframework.http.MediaType"&gt;
                                 &lt;constructor-arg value="application" /&gt;
                                 &lt;constructor-arg value="json" /&gt;
                                 &lt;constructor-arg value="#{T(java.nio.charset.Charset).forName('UTF-8')}"/&gt;
                               &lt;/bean&gt;
                            &lt;/list&gt;
                  &lt;/property&gt;
              &lt;/bean&gt; 
              &lt;bean class="org.springframework.http.converter.FormHttpMessageConverter" /&gt;
              &lt;bean class="org.springframework.http.converter.StringHttpMessageConverter" /&gt;                              
          &lt;/list&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  &lt;bean id="JacksonObjectMapper" class="org.codehaus.jackson.map.ObjectMapper" /&gt;
  
   &lt;bean id="httpClient" class="org.apache.commons.httpclient.HttpClient"&gt;
      &lt;constructor-arg ref="httpClientParams"/&gt;
  &lt;/bean&gt;
  
   &lt;bean id="httpClientParams" class="org.apache.commons.httpclient.params.HttpClientParams"&gt;
  
      &lt;property name="connectionManagerClass"
                value="org.apache.commons.httpclient.MultiThreadedHttpConnectionManager"/&gt;
  &lt;/bean&gt;
  
  &lt;bean id="httpClientFactory" class="org.springframework.http.client.CommonsClientHttpRequestFactory"&gt;
      &lt;constructor-arg ref="httpClient"/&gt;
  &lt;/bean&gt;
  
   &lt;bean id="baseClient" class="com.tapgift.base.client.BaseClientImpl" &gt;
      &lt;property name="restTemplate" ref="restTemplate" /&gt;
   &lt;/bean&gt;
  </code></pre>
  
  <p>Actual usage of this resttemplate</p>
  
  <pre><code>@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&lt;String, String&gt; map = new LinkedMultiValueMap&lt;String, String&gt;();
      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();
  }
  </code></pre>
  
  <p>This method does not execute, it gives me BadRequest error, Following is the end point of other web service.</p>
  
  <pre><code>@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);
  }
  </code></pre>
  
  <p>This is the cosole error.</p>
  
  <pre><code>    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
  </code></pre>
  
  <p>This RestTemplate works for GET methods.
  Thanks</p>