2016-09-14 143 views
0

我消耗的web服務返回未JSONP對象是這樣的:無法提取響應:沒有合適HttpMessageConverter找到的響應類型[java.util.Map]和內容類型[應用/ JavaScript的]

jQueryxxx_xx({ 
    "status": "OK", 
    "results": [ 
    { 
     "ubicacion": "SAN JUAN, LUCANAS, AYACUCHO", 
     "ubigeo": "050616", 
     "x": "-74.1991371264406", 
     "y": "-14.6514315472295" 
    }, 
    { 
     "ubicacion": "SAN JUAN, SIHUAS, ANCASH", 
     "ubigeo": "021909", 
     "x": "-77.5820769245717", 
     "y": "-8.64612162557643" 
    }, 
    { 
     "ubicacion": "SAN JUAN, CASTROVIRREYNA, HUANCAVELICA", 
     "ubigeo": "090410", 
     "x": "-75.6349774079376", 
     "y": "-13.2032560496248" 
    }, 
    { 
     "ubicacion": "SAN JUAN, CAJAMARCA, CAJAMARCA", 
     "ubigeo": "060112", 
     "x": "-78.5005693425229", 
     "y": "-7.2909846278869" 
    }, 
    { 
     "ubicacion": "SAN LUIS, LIMA, LIMA", 
     "ubigeo": "150134", 
     "x": "-76.9972475943101", 
     "y": "-12.0740682897166" 
    }, 
    { 
     "ubicacion": "SAN JOSE, LAMBAYEQUE, LAMBAYEQUE", 
     "ubigeo": "140311", 
     "x": "-79.9661756947746", 
     "y": "-6.76677043459326" 
    }, 
    { 
     "ubicacion": "SAN LUIS, SAN PABLO, CAJAMARCA", 
     "ubigeo": "061203", 
     "x": "-78.8683707221198", 
     "y": "-7.15717727393363" 
    }, 
    { 
     "ubicacion": "SAN LUIS, CARLOS FERMIN FITZCARRALD, ANCASH", 
     "ubigeo": "020701", 
     "x": "-77.3291574143902", 
     "y": "-9.09439276715693" 
    }, 
    { 
     "ubicacion": "SAN LUIS, CAÑETE, LIMA", 
     "ubigeo": "150514", 
     "x": "-76.4282649580734", 
     "y": "-13.0502707531236" 
    }, 
    { 
     "ubicacion": "SAN JOSE, PACASMAYO, LA LIBERTAD", 
     "ubigeo": "130705", 
     "x": "-79.4564509393214", 
     "y": "-7.34696446146118" 
    } 
    ], 
    "elapsed": 0.036 
},"elapsed":0.036}) 

我使用Spring 3.1

的RestTemplate對象這是我的配置

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:oxm="http://www.springframework.org/schema/oxm" 
    xsi:schemaLocation=" 
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc.xsd 
http://www.springframework.org/schema/oxm 
http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd 
http://www.springframework.org/schema/beans 

http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.1.xsd 
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"> 
    <!-- Scans the classpath of this application for @Components to deploy as 
     beans --> 
    <context:component-scan base-package="xxxx" /> 

    <!-- Configures the @Controller programming model --> 
    <mvc:annotation-driven /> 
    <aop:aspectj-autoproxy /> 

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
     <property name="locations"> 
      <list> 
       <value>classpath:servicios.properties</value> 
       <value>classpath:other.properties</value> 
      </list> 
     </property> 
    </bean> 

    <!-- Resolves view names to protected .jsp resources within the /WEB-INF/views 
     directory --> 

    <bean id="viewResolver" 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/jsp/" /> 
     <property name="suffix" value=".jsp" /> 
    </bean> 

    <bean id="reHttpConnectionManager" 
     class="org.apache.commons.httpclient.MultiThreadedHttpConnectionManager"> 
     <property name="params"> 
      <bean 
       class="org.apache.commons.httpclient.params.HttpConnectionManagerParams"> 
       <property name="soTimeout" value="60000" /> 
       <property name="connectionTimeout" value="90000" /> 
       <property name="maxTotalConnections" value="500" /> 
       <property name="defaultMaxConnectionsPerHost" value="500" /> 
      </bean> 
     </property> 
    </bean> 

    <bean id="reRequestFactory" 
     class="org.springframework.http.client.CommonsClientHttpRequestFactory"> 
     <property name="readTimeout" value="90000" /> 
     <constructor-arg> 
      <bean id="httpClient" class="org.apache.commons.httpclient.HttpClient"> 
       <property name="httpConnectionManager" ref="reHttpConnectionManager" /> 
      </bean> 
     </constructor-arg> 
    </bean> 

    <bean id="restTemplate" class="org.springframework.web.client.RestTemplate"> 
     <constructor-arg ref="reRequestFactory" /> 
     <property name="messageConverters"> 
      <list> 
       <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/> 
      </list> 
     </property> 
    </bean> 

    <bean 
     class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> 
     <property name="order" value="1" /> 
     <property name="mediaTypes"> 
      <map> 
       <entry key="json" value="application/json" /> 
       <entry key="xml" value="application/xml" /> 
       <!-- <entry key="jsonp" value="application/javascript" /> --> 
      </map> 
     </property> 

     <property name="defaultViews"> 
      <list> 
       <!-- JSON View --> 
       <bean 
        class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" /> 

       <!-- XML View --> 
       <bean class="org.springframework.web.servlet.view.xml.MarshallingView"> 
        <constructor-arg> 
         <bean class="org.springframework.oxm.xstream.XStreamMarshaller"> 
          <property name="autodetectAnnotations" value="true" /> 
         </bean> 
        </constructor-arg> 
       </bean> 


      </list> 
     </property> 
    </bean> 

    <import resource="config.xml"/> 

</beans> 

在我的代碼,我調用弗朗這樣:

Object respuesta = restTemplate.getForObject(urlSearch, Map.class, parametros); 
//parametros is of type Map<String, Object> 

我獲得以下錯誤:

09:45:22,668 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [java.util.Map] and content type [application/javascript] 

09:45:22,668 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:84) 

09:45:22,668 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:454) 

09:45:22,668 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:417) 

09:45:22,668 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:214) 

我一直在努力,包括應用程序/ JavaScript的支持:

MappingJackson2HttpMessageConverter converterJSONP = new MappingJackson2HttpMessageConverter(); 
      converterJSONP.setSupportedMediaTypes(Arrays.asList(new MediaType("application/javascript"))); 
      restTemplate.getMessageConverters().add(converterJSONP); 

我以爲會解決問題,但不這樣做,我得到這個錯誤:

Invalid token character '/' in token "application/javascript" 

那麼,我可以如何配置我的web應用程序可以con sume返回JSONP對象的Web服務。

在此先感謝!

更新1

從@abaghel以下建議,我得到這個錯誤(我想我要創造我自己的HttpMessageConverter):

09:54:24,208 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Unrecognized token 'jQuery111107193651702763904_1469109722644': was expecting ('true', 'false' or 'null') 
09:54:24,211 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at [Source: [email protected]; line: 1, column: 43]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'jQuery111107193651702763904_1469109722644': was expecting ('true', 'false' or 'null') 
09:54:24,214 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at [Source: [email protected]; line: 1, column: 43] 

09:54:24,216 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readInternal(MappingJackson2HttpMessageConverter.java:126) 

09:54:24,218 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:153) 

09:54:24,220 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:81) 

09:54:24,222 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:454) 

09:54:24,223 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:417) 

09:54:24,225 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:214) 

09:54:24,226 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at pe.com.equifax.ws.tm.service.impl.MapCityServiceImpl.searchDistricts(MapCityServiceImpl.java:55) 

09:54:24,228 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at pe.com.equifax.ws.tm.business.impl.MapCityBusinessImpl.searchDistricts(MapCityBusinessImpl.java:26) 

09:54:24,230 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at pe.com.equifax.ws.tm.controller.MapCityController.searchDistricts(MapCityController.java:33) 

09:54:24,231 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 

09:54:24,233 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 

09:54:24,234 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 

09:54:24,236 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at java.lang.reflect.Method.invoke(Method.java:601) 

09:54:24,237 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219) 

09:54:24,239 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132) 

09:54:24,241 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:100) 

09:54:24,242 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:604) 

09:54:24,243 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:565) 

09:54:24,245 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80) 

09:54:24,246 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923) 

09:54:24,247 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852) 

09:54:24,248 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882) 

09:54:24,249 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789) 

09:54:24,250 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at javax.servlet.http.HttpServlet.service(HttpServlet.java:754) 

09:54:24,251 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) 

09:54:24,252 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) 

09:54:24,253 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) 

09:54:24,254 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) 

09:54:24,255 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) 

09:54:24,256 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) 

09:54:24,257 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) 

09:54:24,258 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) 

09:54:24,259 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) 

09:54:24,260 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) 

09:54:24,261 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) 

09:54:24,262 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) 

09:54:24,264 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) 

09:54:24,265 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at java.lang.Thread.run(Thread.java:722) 

09:54:24,266 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) Caused by: com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'jQuery111107193651702763904_1469109722644': was expecting ('true', 'false' or 'null') 
09:54:24,267 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at [Source: [email protected]; line: 1, column: 43] 

09:54:24,268 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1576) 

09:54:24,269 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:533) 

09:54:24,270 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._reportInvalidToken(UTF8StreamJsonParser.java:3448) 

09:54:24,271 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._handleUnexpectedValue(UTF8StreamJsonParser.java:2607) 

09:54:24,273 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._nextTokenNotInObject(UTF8StreamJsonParser.java:841) 

09:54:24,274 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at com.fasterxml.jackson.core.json.UTF8StreamJsonParser.nextToken(UTF8StreamJsonParser.java:737) 

09:54:24,275 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:3742) 

09:54:24,276 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3687) 

09:54:24,277 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2798) 

09:54:24,278 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readInternal(MappingJackson2HttpMessageConverter.java:123) 

09:54:24,279 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) ... 37 more 

回答

0

嘗試其他客戶端的JSONP像下面。

RestTemplate restTemplate = new RestTemplate(); 
String result = restTemplate.getForObject(url, String.class, params); 
System.out.println(result); 

您應該像下面那樣創建MediaType以避免錯誤消息。

MediaType mt = new MediaType("application", "javascript"); 

您還可以參考SO與應用程序/ JavaScript和jsonp相關的帖子。這篇文章是針對Spring 4的,但你會得到關於jsonp和消息轉換器的信息。

+0

我按照你的意見......但我得到一個新的錯誤。我想我必須實現我自己的HttpMessageConverter。它是否正確? –

+0

這應該與您爲響應對象使用字符串類型一樣工作。你可以使用restTemplate.getMessageConverters()檢查你的MessageConverters是否可用於restTemplate?如果您使用Postman進行呼叫,webservice是否會返回正確的響應? – abaghel

相關問題