2013-04-01 305 views
1

希望春天的大師可以幫助我, Iam開發一個多Web服務應用程序,所有這些Web服務都基於一個名爲基礎服務器的Jar,它獲得所有基類可以在需要時繼承。 所以這個基礎服務器項目我有一個BaseClient類,它有一個RestTemplate屬性。當我試圖使用這個客戶端(的原因繼承)在一個實際實現的Web服務類,當我嘗試做POST請求它給了我HTTP 400錯誤請求錯誤。然而,它對GET請求沒有任何麻煩。 Appriciate如果有人可以指出我錯了什麼地方。POST請求與Spring RestTemplate- BadRequest 400

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> 

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

此方法不執行的實際使用情況,它給了我錯誤請求錯誤,以下是其他Web服務的終點。

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

這是cosole錯誤。

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 

此RestTemplate適用於GET方法。 謝謝

回答

5

好吧,我終於找出了失敗的原因,這確實是一個很有趣的解決方案。我做的是改變豆的順序messagesConvertersrestTemplate 豆。

此前據,

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

和它的工作。 這不是一個有趣的解決方案嗎?

相關問題