Reputation: 2367
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.
Upvotes: 2
Views: 352
Reputation: 7531
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());
}
...
}
Upvotes: 1
Reputation: 689
Content-Type
is a request header and you can get it with the following code:
@RequestMapping("/your-endpoint")
public void endpoint(@RequestHeader("Content-Type") String contentType) {
}
See spring's @RequestHeader docs
Upvotes: 1