Skip to content

ResourceHttpMessageConverter create incorrect type of Resource #29427

@ainGitHub

Description

@ainGitHub

ResourceHttpMessageConverter class method readInternal create InputStreamResource only when we use InputStreamResource in controller method, otherwice it create ByteArrayResource.
I think it's incorrect, and we need write something like this, see code below.
Or someone can explain why it created in that way?

    @Override
    protected Resource readInternal(Class<? extends Resource> clazz, HttpInputMessage inputMessage)
            throws IOException, HttpMessageNotReadableException {
        if (this.supportsReadStreaming && (InputStreamResource.class == clazz || Resource.class == clazz)) {
            return new InputStreamResource(inputMessage.getBody()) {
                @Override
                public String getFilename() {
                    return inputMessage.getHeaders().getContentDisposition().getFilename();
                }

                @Override
                public long contentLength() throws IOException {
                    long length = inputMessage.getHeaders().getContentLength();
                    return (length != -1 ? length : super.contentLength());
                }
            };
        } else if (ByteArrayResource.class.isAssignableFrom(clazz)) {
            byte[] body = StreamUtils.copyToByteArray(inputMessage.getBody());
            return new ByteArrayResource(body) {
                @Override
                @Nullable
                public String getFilename() {
                    return inputMessage.getHeaders().getContentDisposition().getFilename();
                }
            };
        } else {
            throw new HttpMessageNotReadableException("Unsupported resource class: " + clazz, inputMessage);
        }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: webIssues in web modules (web, webmvc, webflux, websocket)status: declinedA suggestion or change that we don't feel we should currently apply

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions