spring-framework
spring-framework copied to clipboard
Default content type in ContentNegotiationConfigurer not taken into account
I have a @GetMapping @RestController that could produce both xml and json.
If Content-Type is missing in the clients' request, by default json should be returned.
I therefor tried to set ContentNegotiationConfigurer, but without any effect:
.w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotWritableException: No converter for [class java.util.ImmutableCollections$Set12] with preset Content-Type 'null']
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.defaultContentType(MediaType.APPLICATION_JSON);
}
}
@RestController
public class ExampleServlet {
@GetMapping(value = "/test", produces = {MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE})
public Set<String> test() {
return Set.of("test");
}
}
When I change the code to produces=APPLICATION_JSON_VALUE only, everything works as expected. So the servlet in general should be fine.
spring-boot-2.7.4