以下代码在返回 JSON 时工作正常

public Mono<ServerResponse> postUpload(ServerRequest request) {

final Flux<Object> stringFlux = request.body(BodyExtractors.toParts())
            .flatMap(filePart -> filePart.content().map(dataBuffer -> {
                byte[] bytes = new byte[dataBuffer.readableByteCount()];
                dataBuffer.read(bytes);
                DataBufferUtils.release(dataBuffer);
                byte[] resMessage = new byte[1024];
                resMessage = helper.postUpload(bytes, headers);
                String resposeMessage = new String(resMessage, StandardCharsets.UTF_8);
                return resposeMessage;
            }).doOnError(e -> LOGGER.error("Error on Processing file", e)));
            
    return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(stringFlux, Object.class);
    }

但是如果我返回 XML(用 XML 替换最后一行中的 JSON),我会得到错误

return ServerResponse.ok().contentType(MediaType.APPLICATION_XML).body(stringFlux, Object.class);

我得到的错误消息是bodyType=java.lang.Object 不支持 Content type 'application/xml'

SpringBoot Webflux cannot return application/xml and javax.xml.bind.JAXBException 未在模块路径或类路径上找到 JAXB-API 的实现,因此更改了我的代码以使用 JAXB


JAXBContext jc = null;
            try {
                jc =JAXBContext.newInstance(new Class[]{com.apple.ist.realestate.sm.rqm.model.TestXml.class});
            } catch (JAXBException e1) {
                e1.printStackTrace();
            }
return ServerResponse.ok().contentType(MediaType.APPLICATION_XML).body(BodyInserters.fromValue(jc));

我仍然收到不受支持的错误: bodyType=com.sun.xml.bind.v2.runtime.JAXBContextImpl 不支持内容类型“应用程序/xml”

任何帮助或讨论表示赞赏。

  ask by mobileDev translate from so

本文未有回复,本站智能推荐:

1回复

我可以使用异步Aalto-XML作为JAXB实现吗?

Is it possible to use aalto-xml as JAXB implementation?是否可以使用aalto-xml作为JAXB实现? I tried to pass AsyncXMLStreamReader to JAXB context:我试图将AsyncXMLStrea
1回复

Webflux-jaxb会影响反应式应用程序的性能吗?

I have webflux application which mainly parse xml and returns json response. 我有主要解析xml并返回json响应的webflux应用程序。 Max xml size which app handles is about
1回复

将XML请求正文内容解码为Mono或Flux对象时,SpringBootWebflux处理程序错误

I have been trying to implement Spring reactive routers and handler instead of MVC controllers.我一直在尝试实现 Spring 反应式路由器和处理程序,而不是 MVC 控制器。 Seemed simple
1回复

SpringBootWebflux无法返回application/xml

In my reactive REST API I'm trying to return an XML response. 在我的反应性REST API中,我试图返回XML响应。 However, I always get a JSON , namely 406 NOT_ACCEPTABLE .
1回复

Jersey:阅读内容类型:简单/文本响应为XML

I'm receiving a HTTP response from an external server that contains XML in the body. 我从外部服务器收到HTTP响应,该服务器包含正文中的XML 。 However, the response header sa
1回复

使用sax解析xml响应

I have been following this tutorial to use sax parser. 我一直在按照本教程使用sax解析器。 If my input is using xml file, then the below line is working fine. 如果我的输入使