I have three resources:
@RequestMapping(value = "sample", method = RequestMethod.GET, headers = "Accept=text/html")
public ResponseEntity<String> sampleResourceHtml()
@RequestMapping(value = "sample", method = RequestMethod.GET, headers = "Accept=application/xml")
public ResponseEntity<String> sampleResourceXml()
@RequestMapping(value = "sample", method = RequestMethod.GET, headers = "Accept=application/json")
public ResponseEntity<String> sampleResourceJson()
When a HTTP client accesses the url with Accept=*/* the webapp returns a 404
In this case I want to invoke sampleResourceHtml()
Changing "Accept=text/html" to "Accept=text/html, */*" will make my webapp accept requests with Accept=*/* which is what I want, however it will also accept requests with Accept=foo/bar which is not what I want.
How do I modify my code to return a supported media type for requests containing wildcards without returning an unexpected media type for unsupported requests?
You might find it easier to configure this in your context with the AnnotationMethodHandlerAdapter, so that the Accept header is automatically handled and the conversion done by Spring and not programmatically.
For example, you could use the following configuration:
And modify the controller to return the object which Spring will convert to the required type.
Note: You will need the relevant libraries on the classpath.