简体   繁体   English

SpringBoot POST端点返回`No HttpMessageConverter`

[英]SpringBoot POST Endpoint returning `No HttpMessageConverter`

I have a task to create a controller with a POST endpoint.我的任务是创建一个带有 POST 端点的 controller。 The endpoint will take some data as an InputStream.端点会将一些数据作为 InputStream。 The intent is to process that data and return a stream in the response.目的是处理该数据并在响应中返回 stream。 The response could be several megabytes in size.响应的大小可能是几兆字节。

Here's my controller::这是我的 controller::

@PostMapping(value = "/somePath")
public ResponseEntity<InputStream> processData(final InputStream data) {
    InputStream processed = processor.process(data);
    return ResponseEntity.ok().body(processed);
}

And I'm using the following to test it::我正在使用以下内容对其进行测试::

@Autowired RestTemplate restTemplate;

private ResponseEntity<InputStream> request(){

    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(singletonList(APPLICATION_OCTET_STREAM));
    
    ResourceHttpMessageConverter converter = new ResourceHttpMessageConverter();
    converter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM));
    restTemplate.getMessageConverters().add(converter);

    UriComponentsBuilder builder = fromUriString(format("http://localhost:%s/%s", serverPort, "/somePath"));
    
    UriComponents uriComponents = builder.build().encode(UTF_8);

    HttpEntity<InputStream> entity = new HttpEntity<InputStream>("Hello".getBytes(), headers);

    return restTemplate.exchange(uriComponents.toUri(), HttpMethod.POST, entity, InputStream.class);
}

However, I'm getting::但是,我得到::

org.springframework.web.client.RestClientException: No HttpMessageConverter for [B

I've tried adding a ByteArrayHttpMessageConverter to my RestTemplate, but when I do that I get::我尝试将 ByteArrayHttpMessageConverter 添加到我的 RestTemplate,但是当我这样做时,我得到::

org.springframework.web.client.HttpServerErrorException$InternalServerError: 500 : [<!doctype html><html lang="en"><head><title>HTTP Status 500 – Internal Server Error</title><style type="text/css">body {font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {color:white;background-colo... (455 bytes)]

Any help, would be much appreciated!任何帮助将非常感激!

You already fixed the client code by adding a ByteArrayHttpMessageConverter , which makes sense since you're giving it a byte[] .您已经通过添加ByteArrayHttpMessageConverter修复了客户端代码,这是有道理的,因为您给它一个byte[]

You already know about the ResourceHttpMessageConverter , which is registered by default on the server, so why not use it, ie give a Resource as the payload?您已经知道ResourceHttpMessageConverter ,它默认注册在服务器上,那么为什么不使用它,即提供一个Resource作为有效负载?

Since you have an InputStream , for which there are no message converters by default, use an InputStreamResource :由于您有一个InputStream ,默认情况下没有消息转换器,因此请使用InputStreamResource

return ResponseEntity.ok().body(new InputStreamResource(processed));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM