Difference between APPLICATION_JSON and APPLICATION_JSON_VALUE

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

I think both represent the same application/json content type, but if I put MediaType.APPLICATION_JSON , some compiler errors will add @controller and @ResponseBody to my rest controller and when to use MediaType.APPLICATION_JSON

 @RequestMapping(value="/invite", method = POST, consumes = { MediaType.APPLICATION_JSON }) public @ResponseBody String sendInvite( ... ) { ... } 
+5
source share
1 answer

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

Java annotation attributes can only be one of a limited set of types. This prevents using MediaType as an annotation attribute. To overcome this, String constants on MediaType are used instead of String , including MediaType.APPLICATION_JSON_VALUE .

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

+7
source

All Articles