Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support DataBuffer as reactive handler method results [SPR-15148] #19714

Closed
spring-projects-issues opened this issue Jan 16, 2017 · 1 comment
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Jan 16, 2017

Mark Paluch opened SPR-15148 and commented

Reactive handler methods should be able to produce DataBuffer directly so the buffer is passed through as body (or chunk if a handler method produces multiple DataBuffer's).

Sample code:

 
@RestController
public class AvatarController {

  HttpClient httpClient = HttpClient.create();

  @GetMapping("user/avatar")
  Mono<ResponseEntity<Flux<DataBuffer>>> getAvatar() {

    NettyDataBufferFactory factory = new NettyDataBufferFactory(
        ByteBufAllocator.DEFAULT);

    return Mono
        .just("http://www.gravatar.com/avatar/c5f6e8dffbb19acf405198c8fb917337?s=48")
        .flatMap(httpClient::get)
        .map(resp -> {

          HttpHeaders headers = new HttpHeaders();

          Optional<String> optionalContentType = Optional.ofNullable(resp
              .responseHeaders().getAsString(HttpHeaders.CONTENT_TYPE));
          headers.add(HttpHeaders.CONTENT_TYPE,
              optionalContentType.orElse(MediaType.IMAGE_JPEG_VALUE));

          return new ResponseEntity<>(resp.receive().map(factory::wrap)
              .cast(DataBuffer.class), headers, HttpStatus.valueOf(resp
              .status().code()));

        }).next();
  }
}

Expected result:

HTTP/1.1 200 OK
Content-Type: …

(binary data goes here)

Current result:

HTTP/1.1 200 
Content-Type: text/event-stream

data:{"nativeBuffer":{"direct":true,"readOnly":false,"writable":false,"readable":true}}

data:{"nativeBuffer":{"direct":true,"readOnly":false,"writable":false,"readable":true}}

Affects: 5.0 M4

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Jan 17, 2017

Arjen Poutsma commented

Done, see 88c5f59

@spring-projects-issues spring-projects-issues added type: enhancement A general enhancement in: web Issues in web modules (web, webmvc, webflux, websocket) labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 5.0 M5 milestone Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants