spring-framework icon indicating copy to clipboard operation
spring-framework copied to clipboard

Default content type in ContentNegotiationConfigurer not taken into account

Open membersound opened this issue 3 years ago • 0 comments

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

membersound avatar Oct 11 '22 13:10 membersound