你需要确定Spring应该如何确定响应的媒体类型。这可以通过几种方式完成:
- 路径扩展(例如/image.jpg)
- URL参数(例如?format = jpg)
- HTTP接受标头(例如,接受:图片/ jpg)
默认情况下,Spring会查看扩展名而不是
Accept标题。如果你实现
@Configuration扩展的类,则可以更改此行为
WebMvcConfigurerAdapter(或者由于Spring 5.0只是实现WebMvcConfigurer。你可以在其中覆盖
configureContentNegotiation(ContentNegotiationConfigurer configurer)和配置
ContentNegotiationConfigurer你的需求,例如,通过调用
ContentNegotiationConfigurer#favorParameterContentNegotiationConfigurer#favorPathExtension
如果你都将都设置为
false,那么Spring将查看
Accept标题。由于你的客户可以说
Accept: image/*,
application/json并处理这两个消息,因此Spring应该能够返回图像或错误JSON。



