Java 如何将JAXB转换为JSON?

Java 如何将JAXB转换为JSON?,java,json,web-services,rest,jaxb,Java,Json,Web Services,Rest,Jaxb,我需要为返回JAXB对象的基于XML的web服务创建一个web服务前端。我的前端需要返回RESTful格式。有没有办法将其转换为JSON?我的班级路径中有jackson mapper asl和jackson core asl 作为一个简单的测试,我返回一个Book bean,它以JSON的形式输出,但是JAXB对象(交付时间表)保持XML格式 import org.springframework.beans.factory.annotation.Autowired; import org.spr

我需要为返回JAXB对象的基于XML的web服务创建一个web服务前端。我的前端需要返回RESTful格式。有没有办法将其转换为JSON?我的班级路径中有jackson mapper asljackson core asl

作为一个简单的测试,我返回一个Book bean,它以JSON的形式输出,但是JAXB对象(交付时间表)保持XML格式

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;
    }


}

可以显示配置文件(XML或类)吗

另外,我将假设您使用的是HttpMessageConverter()类,而不是ContentNegotingViewResolver

这非常简单,大多数情况下是因为我们忘记添加所需的配置“mvc:annotation-driven”


如果您使用内容协商视图,这个示例非常好

您只需将@products(“application/xml”)或headers部分添加到@requestMapping中,内容类似于:headers={Const.HEADER\u content\u TYPE\u application\u JSON}感谢您的响应。我有一个SpringXML文件,但内容都与Spring相关(组件扫描和属性配置)。我有以下资料: