Results 1 to 2 of 2

Thread: Json and POJO using the single service

  1. #1
    Join Date
    Aug 2007
    Posts
    4

    Question Json and POJO using the single service

    Greetings,

    My controller is declared to return a POJO:

    Code:
    @RequestMapping("/btc")
        private ResponseEntity<BtcDto> touchClaims(BtcDto example) {
            return new ResponseEntity<BtcDto>(bankruptcyService.foo(example), HttpStatus.OK);
        }
    Sometimes clients need the POJOs, so they use

    Code:
    restTpl.postForEntity("http://localhost:9999/btc", ex, BtcDto.class, new HashMap<String, Object>()).getBody();
    which works as a charm. Next step it to make

    Code:
    restTpl.postForEntity("http://localhost:9999/btc", ex, String.class, new HashMap<String, Object>()).getBody();
    to return Json representation of the return object (some clients don't need POJOs, so I think Spring should be clever enough not to un-marshal Json payload back to POJO). Right now I'm getting Jackson framework exception "I/O error: Can not deserialize instance of java.lang.String out of START_OBJECT token"

    I'd also like to add that the current working version of application leverages Jersey Web App, not Spring, and seems that Jersey makes both these scenarios work somehow. What I'm trying to do is to replace Jersey app with Spring and Jackson.

    Any pointers on how to address this situation would be highly appreciated!
    Alex

  2. #2
    Join Date
    Aug 2007
    Posts
    4

    Default

    I've finally got this working by adding StringHttpMessageConverter to REST template bean definition

    Code:
        <bean class="org.springframework.web.client.RestTemplate">
            <property name="messageConverters">
                <list>
                    <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
                    <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
                </list>
            </property>
        </bean>

Posting Permissions

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