EdgeCase
EdgeCase

Reputation: 4827

How Can I Convert JAXB to JSON?

I need to create a web service front-end to an XML-based web service that returns a JAXB object. My front-end needs to return a RESTful format. Is there a way to convert that to JSON? I have jackson-mapper-asl and jackson-core-asl in my class path.

As a simple test, I return a bean of Book, and it gets output as JSON, but the JAXB object (delivery schedule) stays in an XML format.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class DSWLIClient {

    @Autowired
    private DeliveryScheduleWS wliService;
    private BookService bs;

    @RequestMapping(value = "/DSDetails/{ds-number}", method = RequestMethod.GET)
    public @ResponseBody
    DeliverySchedule getDSDetails(@PathVariable("ds-number") String dsNumber) {
        DeliveryScheduleResponse dsDetails = wliService.getDeliveryScheduleWSSoap().getDSDetails(dsNumber);
        DeliverySchedule deliverySchedule = dsDetails.getDeliverySchedule();
        return deliverySchedule;
    }


    @RequestMapping(value = "/BookDetails/{isbn-number}", method = RequestMethod.GET)
    public @ResponseBody
    Book getBookDetails(@PathVariable("isbn-number") String isbnNumber) {
        bs = new BookService();
        Book b = bs.getBookByIsbn(isbnNumber);
        System.out.println(b.getAuthor());
        return b;
    }


}

Upvotes: 3

Views: 878

Answers (1)

Rodrigo
Rodrigo

Reputation: 493

Can you show your configuration file (XML or class)?

Also I will assume that you are using the HttpMessageConverter (http://www.ibm.com/developerworks/web/library/wa-restful/) class and not the ContentNegotiatingViewResolver.

That is dead easy, most of the time is because we forget to add the required configuration "mvc:annotation-driven"

In case you use the content negotiation view this example is great http://www.mkyong.com/spring-mvc/spring-3-mvc-contentnegotiatingviewresolver-example/

Upvotes: 1

Related Questions