Results 1 to 4 of 4

Thread: Spring restTemplate and application/xml;charset=UTF-8

  1. #1
    Join Date
    Sep 2011
    Location
    Dublin Ireland
    Posts
    16

    Default Spring restTemplate and application/xml;charset=UTF-8

    Hi,

    I am using 3.0.6.RELEASE and trying to get the XML marshaller to run but I can't seem to get a match to the HTTP header.


    For a test I get a JSON response ok:

    12:37:22,338 INFO [Jaxb2Marshaller] Creating JAXBContext with classes to be bound [class uk.co.thehub.ota.lib.rest.model.ResultEntity,class uk.co.thehub.ota.lib.rest.model.ResultListEntity,c lass uk.co.thehub.ota.lib.rest.model.ResultBinaryEntity]
    12:37:23,069 INFO [SimpleRestClient] [application/json;charset=UTF-8]
    12:37:23,069 INFO [SimpleRestClient] [application/xml;charset=UTF-8]
    12:37:23,105 DEBUG [RestTemplate] Created GET request for "http://localhost:9090/ota-service-validator/rest/generate/kiosk/123"
    12:37:23,145 DEBUG [RestTemplate] Setting request Accept header to [application/json]
    12:37:23,167 WARN [HttpMethodDirector] Required credentials not available for BASIC <any realm>@localhost:9090
    12:37:23,168 WARN [HttpMethodDirector] Preemptive authentication requested but no default credentials available
    12:37:23,232 DEBUG [RestTemplate] GET request for "http://localhost:9090/ota-service-validator/rest/generate/kiosk/123" resulted in 200 (OK)
    12:37:23,233 DEBUG [RestTemplate] Reading [uk.co.thehub.ota.lib.rest.model.ResultEntity] as "application/json;charset=UTF-8" using [org.springframework.http.converter.json.MappingJac ksonHttpMessageConverter@3445378f]

    This fails with XML response:

    12:38:31,190 INFO [SimpleRestClient] [application/json;charset=UTF-8]
    12:38:31,190 INFO [SimpleRestClient] [application/xml;charset=UTF-8]
    12:38:31,225 DEBUG [RestTemplate] Created GET request for "http://localhost:9090/ota-service-validator/rest/generate/kiosk/123.xml"
    12:38:31,268 DEBUG [RestTemplate] Setting request Accept header to [application/json]
    12:38:31,291 WARN [HttpMethodDirector] Required credentials not available for BASIC <any realm>@localhost:9090
    12:38:31,291 WARN [HttpMethodDirector] Preemptive authentication requested but no default credentials available
    12:38:32,287 DEBUG [RestTemplate] GET request for "http://localhost:9090/ota-service-validator/rest/generate/kiosk/123.xml" resulted in 200 (OK)
    Exception in thread "main" org.springframework.web.client.RestClientException : Could not extract response: no suitable HttpMessageConverter found for response type [uk.co.thehub.ota.lib.rest.model.ResultEntity] and content type [application/xml;charset=UTF-8]

    I have tried with supportedMediaTypes default but still can't get the client to pickup the response, is there an issue with the charset=UTF-8 or ?

    Thank you in advance.

    Paul

  2. #2
    Join Date
    Sep 2011
    Location
    Dublin Ireland
    Posts
    16

  3. #3
    Join Date
    Sep 2011
    Location
    Dublin Ireland
    Posts
    16

    Default

    Right I wonder if the error message is a bit misleading. The error maybe due to the fact the I can't Unmarshall the response XML:

    <?xml version="1.0" encoding="UTF-8"?>
    <result-entity>
    <response>NEW</response>
    <response-value>KI-131589539514798681</response-value>
    <opcode>generateKioskName</opcode>
    <id>a78118c4-b2a7-4626-b2b2-5faf848547d7</id>
    </result-entity>

    javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"result-entity"). Expected elements are <{http://thehub.co.uk/ota/lib/rest/model}requestBinaryEntity>

  4. #4
    Join Date
    Sep 2011
    Location
    Dublin Ireland
    Posts
    16

    Default

    Problem solved, using :

    ResponseEntity<ResultEntity> re = restTemplate.getForEntity(url, ResultEntity.class);

    Gives me good JAXB messages.

    So the issue was all about mapping XML->Entity NOT mapping HTTP headers->org.springframework.http.converter.xml.Marshallin gHttpMessageConverter

    I also switch to Castor for the XML so that I can map the result-entity->ResultEntity

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •