希望Spring大师可以帮助我, 我开发了一个多Web服务应用程序,所有这些Web服务都基于一个称为基于服务器的Jar,它获得了所有可以在需要时继承的基类。 所以这个基础服务器项目我有一个BaseClient类,它有一个Spring RestTemplate属性。 当我尝试在实际实现的Web服务类中使用此客户端(原因继承)时,当我尝试执行POST请求时,它会给我http 400 Bad Request错误。但它的工作原理没有给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>
此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();
}
此方法不执行,它给出了BadRequest错误,以下是其他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方法。 感谢
答案 0 :(得分:5)
好吧,我终于弄明白了这个故事的原因,这确实是一个非常有趣的解决方案。我所做的是改变了messagesConverters
restTemplate
的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>
它有效。 这不是一个有趣的解决方案吗?