bluelabel
bluelabel

Reputation: 2104

POST request with Spring RestTemplate- BadRequest 400

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.

RestTemplate in Base server

<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>

Actual usage of this resttemplate

@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();
}

This method does not execute, it gives me BadRequest error, Following is the end point of other web service.

@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);
}

This is the cosole error.

    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

This RestTemplate works for GET methods. Thanks

Upvotes: 1

Views: 10172

Answers (1)

bluelabel
bluelabel

Reputation: 2104

Well, i finally figured out reason for the faliure, It is a indeed a verfy funny solution. What i did was changed the order of the beans of messagesConvertersof restTemplate bean.

Earlier it was,

<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>

and i changed it to following order,

<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>

and it worked. Isn't it a funny solution?

Upvotes: 5

Related Questions