Set Content-type in the request header with application.properties in Spring Reactive

Viewed 384

I need to set content-type for a rest call as "application/json;charset=UTF-8" . I can do it with code by adding below to @RequestMapping

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

But I want to know if this can be achieved without any code change, just by adding any property in application.properties. Tried by adding below properties, but didn't worked:

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

Suppose you are working with spring-webflux, referring to Content Type Resolvers You can configure the response media type by adding a WebFluxConfigurer, and the media type can be read from application.properties using @Value

@Configuration
@EnableWebFlux
public class WebConfig implements WebFluxConfigurer {
    @Value("${mediaType}")
    private String mediaType;

    public void configureContentTypeResolver(RequestedContentTypeResolverBuilder builder) {
        builder.fixedResolver(MediaType.parseMediaType(mediaType));
    }
}

And application.properties

mediaType=application/json;charset=UTF-8
Related