JavaEar 专注于收集分享传播有价值的技术资料

Could not extract response: no suitable HttpMessageConverter found for response type [java.util.Map] and content type [application/javascript]

I'm consuming a web service that return un JSONP object like this:

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

I'm using RestTemplate object of Spring 3.1

This is my configurations

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

In my code, I invoke fron this way:

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

I get following error:

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)

I've tried to include application/javascript support:

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

I thought that will solve problem, but don't, I get this error:

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

Well, How I can configure for my web application can consume web services that return JSONP object.

Thanks in advance!

UPDATE 1

Following recommendations from @abaghel, I get this error (I suppose I have to create my own 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: org.apache.commons.httpclient.AutoCloseInputStream@34dfc6; 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: org.apache.commons.httpclient.AutoCloseInputStream@34dfc6; 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: org.apache.commons.httpclient.AutoCloseInputStream@34dfc6; 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

1个回答

    最佳答案
  1. Try rest client for JSONP like below.

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

    You should create MediaType like below to avoid error message.

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

    You can also refer SO post related to application/javascript and jsonp. This post is for Spring 4 but you will get info regarding jsonp and message converters.