Announcement Announcement Module
Collapse
No announcement yet.
no suitable HttpMessageConverter MappingJacksonHttpMessageConverter Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • no suitable HttpMessageConverter MappingJacksonHttpMessageConverter

    Hi,

    I'm pretty new to Spring and having trouble with MappingJacksonHttpMessageConverter. I'm getting no suitable HttpMessageConverter found for response type application/json.

    In my code I tried to make sure the Media Type application/json was set for MappingJacksonHttpMessageConverter

    Code:
    RestTemplate rest = new RestTemplate();
    MappingJacksonHttpMessageConverter converter = new MappingJacksonHttpMessageConverter();
    converter.setSupportedMediaTypes(Arrays.asList(new MediaType("application", "json")));
    rest.getMessageConverters().add(converter);
    		
    MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
    map.add("grant_type", "urn:ietf:params:oauth:grant-type:jwt-bearer");
    map.add("assertion", theHeader + "." + theClaim + "." + theSignature);
    		
    AccessToken theToken = rest.postForObject("https://maxhammer.x.com/oauth/token", map, AccessToken.class);

    The code is in a method associated with a JUNIT Test Annotation - but I'm not sure if this is important.
    I even tried setting the MediaType as a bean

    Code:
    <bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
      <property name="messageConverters">
        <list>
    	<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
    		<property name="supportedMediaTypes">
    			<list>
    				<bean class="org.springframework.http.MediaType">
    					<constructor-arg value="application" />
    					<constructor-arg value="json" />
    				</bean>
    			</list>
    		</property>
    	</bean>
        </list>
      </property>
    </bean>
    but no luck. Any hints are greatly appreciated.

  • #2
    use brute force

    So you can always get there the long way. Instead I used ByteArrayHttpMessageConverter and then used the Jackson ObjectMapper. Code looks like:

    Code:
    RestTemplate rest = new RestTemplate();
    
    ByteArrayHttpMessageConverter converter = new ByteArrayHttpMessageConverter();
    converter.setSupportedMediaTypes(Arrays.asList(new MediaType("*", "*")));
    List<HttpMessageConverter<?>> list = new ArrayList<HttpMessageConverter<?>>();
    list.add(converter);
    
    MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
    map.add("grant_type", "urn:ietf:params:oauth:grant-type:jwt-bearer");
    map.add("assertion", theHeader + "." + theClaim + "." + theSignature);
    
    		
    byte[] theTokenB = rest.execute("https://maxhammer.x.com/oauth/token",
    				HttpMethod.POST, 
    				new FormRequestCallback(map),
    				new HttpMessageConverterExtractor(byte[].class , list)
    				);
    		
    String theTokenS = new String(theTokenB);
    ObjectMapper mapper = new ObjectMapper();
    Map<String, Integer> theTokenMap = mapper.readValue(theTokenS, Map.class );
    Object theTokenO = theTokenMap.get("access_token");
    Note that I tried the above code with a MappingJacksonMessageConverter setting the message content to */* and it didn't work

    Comment

    Working...
    X