我想在调用执行器端点 /info
和 /health
时禁用 Content-Negotiation
这是我的配置文件
@Configuration
public class InterceptorAppConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(interceptor);
}
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.defaultContentType(MediaType.APPLICATION_XML)
.mediaType("json", MediaType.APPLICATION_JSON)
.mediaType("xml", MediaType.APPLICATION_XML);
}
}
当我 curl http://localhost:8081/health
我收到:
DefaultHandlerExceptionResolver Resolved [org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation]
然而,当我在 Chrome 中触发相同的 url 时,我收到了有效的响应。
在我的例子中,应该在没有标题的情况下调用执行器(没有 -H 'Accept: ...')
最佳答案
很遗憾,我只能提供一个次优的解决方案。
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer
.mediaType("json", MediaType.APPLICATION_JSON)
.mediaType("xml", MediaType.APPLICATION_XML)
.defaultContentTypeStrategy((webRequest) -> {
final String servletPath = ((HttpServletRequest) webRequest.getNativeRequest()).getServletPath();
final MediaType defaultContentType = Arrays.asList("/info", "/health").contains(servletPath)
? MediaType.APPLICATION_JSON : MediaType.APPLICATION_XML;
return Collections.singletonList(defaultContentType);
});
}
如果 /info
或 /health
端点被调用,则返回 application/json
。对于所有其他请求,使用默认的 application/xml
。
https://stackoverflow.com/questions/58999106/
相关文章:
php - 权限被拒绝 : copy/writing file (laravel/php)
typescript - 如何在 Visual Studio CE 2019 中使用#region/
npm - [错误 : Unspecified GSS failure. 次要代码可能提供更多信息:
react-native - react native 运行 ios 忽略模拟器选项
python - 将经过训练的 xgboost 分类器从 AWS s3 上传到 EMR
android - Firebase/Fabric Crashlytics 和 Proguard/R
javascript - 无法在 nuxt 中使用 axios 插件