Search code examples
jsonspringannotationsresponsebson

How send bson via Spring @response body


I am using @Responsebody String to send JSON. part of my code is below.(I am sorry for open it all)

@RequestMapping(value = "/getSomeList", method = RequestMethod.GET ,
            headers="Accept=application/json", produces = "text/plain;charset=UTF-8")
    public @ResponseBody String getThumbList(
            @RequestParam("con_id") String con_id) throws Exception{
return json;

}

And actually it sends Json. But my client requests Bson type. How can I change Json to Bson without editing global format(my json is actually just string and I heard that spring can not response bson. Is that right?).


Solution

  • You need to register a HttpMessageConverter that can convert to BSON.

    <mvc:annotation-driven>
         <mvc:message-converters>
              <bean class="MyBsonHttpMessageConverter"/>
         </mvc:message-converters>
    </mvc:annotation-driven>
    

    Unfortunaly Spring has no Bson Http Message Converter, so you have to implement your own. (Or have more luck then me while google for one).