difference between 'APPLICATION_JSON' and 'APPLICATION_JSON_VALUE'

PrabaharanKathiresan :

I am new to spring development and want to know what is the difference between MediaType.APPLICATION_JSON_VALUE and MediaType.APPLICATION_JSON?

I have a thought of both are representing same application/json content type but if I put MediaType.APPLICATION_JSON some compiler errors are shown to add @controller and @ResponseBody annotations to my rest controller and When to use MediaType.APPLICATION_JSON?

@RequestMapping(value="/invite", method = POST, consumes = { MediaType.APPLICATION_JSON })
public @ResponseBody String sendInvite( ... ) { ... }
Andy Wilkinson :

To quote the javadoc, MediaType.APPLICATION_JSON is a "public constant media type for application/json", whereas MediaType.APPLICATION_JSON_VALUE is a "String equivalent of MediaType.APPLICATION_JSON".

Attributes on Java annotations can only be one of a limited set of types. This prevents MediaType from being used as an annotation attribute. To overcome this, a String is used instead along with the various String constants on MediaType including MediaType.APPLICATION_JSON_VALUE.

Outside of an annotation, if you want to refer to a media type you should use the more strongly typed MediaType rather than passing around a String that may or may not actually be a media type. So, for example, you'd use MediaType.APPLICATION_JSON rather than MediaType.APPLICATION_JSON_VALUE.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=448906&siteId=1