Issue
I want to make an endpoint which tells the user, which mediaTypes are registered for contentNegotiation.
These are my settings
configurer
.favorPathExtension(false)
.favorParameter(true)
.parameterName("mediaType")
.ignoreAcceptHeader(true)
.useJaf(false)
.defaultContentType(MediaType.APPLICATION_JSON_UTF8)
.mediaType("json", MediaType.APPLICATION_JSON_UTF8)
.mediaType("pdf", MediaType.APPLICATION_PDF)
.mediaType("html", MediaType.TEXT_HTML)
.mediaType("csv", new MediaType("text", "csv"));
How can I read them in controller? I am hoping for some function whateverService.getMediaTypes which gives back ["json", "pdf", "html", "csv"].
Edit:
Alternatively a method to get all AbstractHttpMessageConverter and their MediaTypes.
Solution
Try something like this:
@RestController
public class YourRest {
...
@Autowired
private ContentNegotiationManager contentNegotiationManager;
@RequestMapping(value = "types", method = RequestMethod.GET)
public Set<String> getConfiguredMediaTypes() {
return Optional.of(contentNegotiationManager)
.map(m -> m.getStrategy(ParameterContentNegotiationStrategy.class))
.map(s -> s.getMediaTypes().keySet())
.orElse(Collections.emptySet());
}
...
}
Answered By - Nikolai Shevchenko
Answer Checked By - Marilyn (JavaFixing Volunteer)