【问题标题】:Set Content-type in the request header with application.properties in Spring Reactive在 Spring Reactive 中使用 application.properties 在请求标头中设置 Content-type
【发布时间】:2021-08-02 18:04:39
【问题描述】:

我需要将休息调用的内容类型设置为 "application/json;charset=UTF-8" 。我可以通过将以下代码添加到 @RequestMapping

来完成

produces = "application/json;charset=UTF-8"

但我想知道这是否可以在不更改任何代码的情况下实现,只需在application.properties 中添加任何属性即可。尝试通过添加以下属性,但没有奏效:

server.servlet.encoding.charset=UTF-8
server.servlet.encoding.force-response=true

【问题讨论】:

    标签: spring-boot rest spring-webflux reactive


    【解决方案1】:

    假设你正在使用spring-webflux,参考Content Type Resolvers 您可以通过添加WebFluxConfigurer来配置响应媒体类型,并且可以使用@Valueapplication.properties中读取媒体类型

    @Configuration
    @EnableWebFlux
    public class WebConfig implements WebFluxConfigurer {
        @Value("${mediaType}")
        private String mediaType;
    
        public void configureContentTypeResolver(RequestedContentTypeResolverBuilder builder) {
            builder.fixedResolver(MediaType.parseMediaType(mediaType));
        }
    }
    

    还有application.properties

    mediaType=application/json;charset=UTF-8
    

    【讨论】:

      猜你喜欢
      • 2017-11-13
      • 2014-05-23
      • 1970-01-01
      • 2011-09-07
      • 2018-06-21
      • 2012-05-27
      相关资源
      最近更新 更多