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

  • no suitable HttpMessageConverter found

    Hi,
    I'm using spring-android 1.0.0.RC1 in my app and while calling POST method with application/x-www-form-urlencoded I'm getting "no suitable HttpMessageConverter found" exception.
    So I registered new FormHttpMessageConverter, also I bind it with appropriate MediaType, but exception is still there. What I'm doing wrong?

    Code:
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.setContentType(new MediaType("application", "x-www-form-urlencoded"));
    HttpEntity<CheckLoginRequest> requestEntity = new HttpEntity<CheckLoginRequest>(request, requestHeaders);
    			
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
    			
    FormHttpMessageConverter converter = new FormHttpMessageConverter();
    List<MediaType> mediaTypes = new ArrayList<MediaType>();
    mediaTypes.add(new MediaType("application", "x-www-form-urlencoded"));
    converter.setSupportedMediaTypes(mediaTypes);
    restTemplate.getMessageConverters().add(converter);
    Thanks in advance!
    Last edited by shmoula; Jun 9th, 2012, 07:32 AM.

  • #2
    The default media types for FormHttpMessageConverter are application/x-www-form-urlencoded and multipart/form-data, so you don't have to manually set either of these. Are you using a MultiValueMap to send the form data? And have you verified the server endpoint accepts that mime type?

    Comment


    • #3
      Hi Roy, thanks for response!
      I updated to newest version (1.0 RELEASE) to try it on it, but it seems a little bit worse - there are no predefined converters in restTemplate.

      Code:
      HttpHeaders requestHeaders = new HttpHeaders();
      requestHeaders.setContentType(new MediaType("application", "x-www-form-urlencoded"));
      HttpEntity<CheckLoginRequest> requestEntity = new HttpEntity<CheckLoginRequest>(request, requestHeaders);
      			
      RestTemplate restTemplate = new RestTemplate();
      restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
      
      /*			
      FormHttpMessageConverter converter = new FormHttpMessageConverter();
      List<MediaType> mediaTypes = new ArrayList<MediaType>();
      mediaTypes.add(new MediaType("application", "x-www-form-urlencoded"));
      converter.setSupportedMediaTypes(mediaTypes);
      restTemplate.getMessageConverters().add(converter);
      */
      
      for(HttpMessageConverter conv : restTemplate.getMessageConverters()) {
          printout(conv.toString());
      }
      Prints out nothing. When uncommented, it prints out org.springframework.http.converter.FormHttpMessage Converter@12345, which is okay. But my problem persists :-(.

      I tried it call with apache's HttpClient PostMethod before this, so I'm sure that server endpoint accepts that mimetype.

      My calling does not use MultiValueMap, because it's not submitting form data, server side is badly implemented - it should listen (and return) for application/json, but it accepts only x-www-form-urlencoded instead, but I cant do anything with that :-(. My calling looks like this:
      Code:
      final String url = "http://foo.bar/herp";
      ResponseEntity<CheckLoginResponse> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, CheckLoginResponse.class);
      CheckLoginResponse result = responseEntity.getBody();

      Comment


      • #4
        You are correct, the 1.0 release was changed to not include any converters by default. However, the default set from previous releases is still available if you pass true when creating a RestTemplate object. This was done for performance reasons on Android.

        Code:
        RestTemplate rt = new RestTemplate(true);
        If you are not sending a MultiValueMap, then that is going to be a problem with the FormHttpMessageConverter. It checks to see if the object to write to the http request is a MultiValueMap. Because of this, you'll receive the RestClientException you mentioned earlier.

        Comment


        • #5
          Thank you Roy, you helped me a lot!

          My problem was following:
          * Server accepts only Content-Type: application/x-www-form-urlencoded
          * Body of this request was JSON in request parameter (ie body: "request={foo:bar}")
          * Server responses with Content-Type: application/json
          * Server response body looks like pure JSON: "{param:value}"

          So I added MultiValueMap<String, String> toMap(); method to my request bean and my request looks like this now and it works:

          Code:
          HttpHeaders requestHeaders = new HttpHeaders();
          requestHeaders.setContentType(new MediaType("application", "x-www-form-urlencoded"));
          			
          RestTemplate restTemplate = new RestTemplate(true);
          restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
          			
          final String url = "http://dev.taplist.cz/maker-dev/api/v1/tlCheckLogin";
          CheckLoginResponse result = restTemplate.postForObject(url, request.toMap(), CheckLoginResponse.class);
          Last edited by shmoula; Jun 9th, 2012, 07:31 AM. Reason: Suddenly it works! :)

          Comment


          • #6
            Great! Glad it is working now. I was about to suggest an alternate method to try, but if you are getting the behavior you need/want, then your solution works too.

            Comment

            Working...
            X