Skip to content

MediaType property binding issue in native builds #30491

@mockxe

Description

@mockxe

Affects: 6.0.8


issue description:
When setting a org.springframework.http.MediaType via config properties (e.g. application/json) it works well when running in jvm, for example by ./gradlew bootRun. But when running natively, for example by ./gradlew nativeRun it fails on startup with a property binding issue: failed to convert java.lang.String to org.springframework.http.MediaType (caused by java.lang.IllegalArgumentException: Invalid token character '/' in token "application/json") (full error below).
It is possible to fix the native version by specifying the media type as application-json but this let's the jvm version fail on startup with a similar issue.

This makes it hard to develop and debug on jvm and deploy as native image, when you use some general default config for both.

reproduce:
I encountered this when working with Spring Data REST, when I was setting spring.data.rest.default-media-type, but it also can be reproduced by setting up your own config properties which accept a MediaType.

full error when starting native build with application/json (bootRun works fine)

***************************
APPLICATION FAILED TO START
***************************

Description: Failed to bind properties under 'spring.data.rest.default-media-type' to org.springframework.http.MediaType:
Property: spring.data.rest.default-media-type
Value: "application/json"
Origin: class path resource [application.yml] - 13:27
Reason: failed to convert java.lang.String to org.springframework.http.MediaType (caused by java.lang.IllegalArgumentException: Invalid token character '/' in token "application/json")
Action: Update your application's configuration 

full error when starting jvm build with application-json (nativeRun works fine)

***************************
APPLICATION FAILED TO START
***************************

Description: Failed to bind properties under 'spring.data.rest.default-media-type' to org.springframework.http.MediaType:
Property: spring.data.rest.default-media-type
Value: "application-json"
Origin: class path resource [application.yml] - 13:27
Reason: failed to convert java.lang.String to org.springframework.http.MediaType (caused by org.springframework.util.InvalidMimeTypeException: Invalid mime type "application-json": does not contain '/')
Action: Update your application's configuration

possible workarounds:

  • in general, having multiple configs for developing (jvm) and deployment/production (native)
  • for this specific case with Spring Data REST, setting the media type programmatically via config.setDefaultMediaType()

If you like I can prepare a repository with a minimal test case.

Thanks for your work

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)theme: aotAn issue related to Ahead-of-time processingtype: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions