Skip to content

Support additional mime types in MediaType class [SPR-13873] #18446

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

Closed
spring-projects-issues opened this issue Jan 18, 2016 · 7 comments
Closed
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) in: web Issues in web modules (web, webmvc, webflux, websocket)

Comments

@spring-projects-issues
Copy link
Collaborator

Sachin Walia opened SPR-13873 and commented

Following mime types are common place in web environment. However Spring MVC MediaType class just throws exception if we try to use their correct mime type. Moreover using application/octet-stream is also not an acceptable solution

  1. SVG: image/svg+xml
  2. WEBP: image/webp

Moreover there should be some way to define custom media type for any additional mime types.


No further details from SPR-13873

@spring-projects-issues
Copy link
Collaborator Author

Rossen Stoyanchev commented

Not acceptable just means there is no HttpMessageConverter implementation that supports writing those media types.

@spring-projects-issues
Copy link
Collaborator Author

Sachin Walia commented

I guess, not acceptable is a little strong word. The point I was making is that both these mime types are supported by browsers (webp by chrome only) and SVG by all modern browsers so natively supporting these two mime types is helpful. Otherwise we have to make use of http headers to set the content type which works but not the most preferred way.

application/octet-stream is typically used in file download scenario where browser cannot render the file and you want to use native application to handle viewing that file.

@spring-projects-issues
Copy link
Collaborator Author

Rossen Stoyanchev commented

I meant "not acceptable" as in HTTP status code 406. Can you provide an example of what you're trying to do? Not a full-blown example, just snippet of controller code.

@spring-projects-issues
Copy link
Collaborator Author

Rossen Stoyanchev commented

Sachin Walia, your comments please? Also what are you using to support conversion to this image type?

@spring-projects-issues
Copy link
Collaborator Author

Sachin Walia commented

Hi, sorry for the late reply. Here is the workflow:

  1. External utility (linux command triggered by Java ProcessBuilder) generates a WebP image.
  2. Controller locates the file and streams it to the front end. Following is the code snippet to render this to front-end:
		InputStreamResource inputStreamResource = //Get some inputstream resource;
		long contentLength = //get file length
		String mediaType = "image/webp" //different depending upon file type
		if(inputStreamResource != null){
			return ResponseEntity.ok().contentLength(contentLength).header("Content-type",mediaType).body(inputStreamResource );
		} else {
			response.messageCode = "SOME_ERROR_CODE";
			response.message = applicationContext.getMessage(response.messageCode, null, null);
			return ResponseEntity.ok(response);
		}

Because MediaType class doesn't support mime "image/webp" so I have to use HttpHeader to set this content type else ResponseEntity will be returned as:

return ResponseEntity.ok().contentLength(contentLength).contentType(MediaType.parseMediaType(mediaType)).body(inputStreamResource );

@spring-projects-issues
Copy link
Collaborator Author

Rossen Stoyanchev commented

Okay I see now I was thrown off originally by the word support. I guess you just mean adding more constants to MediaType? There are literally thousands of these. Currently the cutoff is roughly at the ones we support serialization for (JSON, XML, etc.) Why not create some constants of your own?

@spring-projects-issues
Copy link
Collaborator Author

Sébastien Deleuze commented

Resolving as incomplete given the lack of feedback.

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

No branches or pull requests

2 participants