POST request with Spring RestTemplate- BadRequest 400

一曲冷凌霜 提交于 2019-12-07 02:22:27

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?

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!