关于Struts下集成rest调用外部接口时的问题
前提:
项目原为Struts框架,未集成相关的webservice。
集成了RestTemplate来调用接口,并引入了相关JAR包,对WEB.XML做了修改,并添加了相应的rest-servlet.xml.
RestTemplate相关jar包:
RestTemplate.jpg
WEB.XML:
<servlet>
//配置一个Servlet,有这个Servlet统一调度页面的请求
<servlet-name>springRest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/platform/rest-servlet.xml</param-value>
</init-param>
// <async-supported>true</async-supported>
<load-on-startup>1</load-on-startup>
</servlet>
rest-servlet.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
<context:component-scan base-package="com.tobacco" />
<mvc:annotation-driven />
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter"/>
</list>
</property>
</bean>
</beans>
需求:
调用外部接口。接口为public String xxx(String inJson) throws IOException{……}
引用了spring 的RestTemplate作为调用接口方式。
RestTemplate rest = new RestTemplate();//创建rest对象
String str = rest.postForObject(url, inJson, String.class).toString();// 调用接口
正确输出了接口地址和参数。
[19-9-29 11:03:50:180 CST] 00000092 SystemOut O http://10.29.3.17/crm/restful/CUST_ACC_INFO_SYNC_SERVICE/updateCustAccInfoLn
[19-9-29 11:03:50:180 CST] 00000092 SystemOut O {"HEAD":{"SOURCE":"东软结算"},"BODY":{"DATASET":[{"CUST_ID":"211002100025","COM_ID":"11112110","BANK_ID":"03","PAYMENT_MODE":"1"}]}}
报错:
[19-9-29 11:03:50:181 CST] 00000092 ServletWrappe E com.ibm.ws.webcontainer.servlet.ServletWrapper service SRVE0014E: 未捕获到 service() 异常的根本原因 /jsp/visitsales/bank/customerPaySave.jsp:com.ibm.websphere.servlet.error.ServletErrorReport: java.lang.NoSuchMethodError: org/springframework/util/CollectionUtils.unmodifiableMultiValueMap(Lorg/springframework/util/MultiValueMap;)Lorg/springframework/util/MultiValueMap;
原因为: RestTemplate--的相关支持jar包与spring-2.0.4冲突,且后者优先级更高,导致在调用RestTemplate的实体对象中的方法时找不到,故而有java.lang.NoSuchMethodError异常。