Skip to content

Add property to configure Spring MVC default content types #44040

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.ClassUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.DefaultMessageCodesResolver;
import org.springframework.validation.MessageCodesResolver;
import org.springframework.validation.Validator;
Expand Down Expand Up @@ -271,8 +272,12 @@ public void configureContentNegotiation(ContentNegotiationConfigurer configurer)
if (contentnegotiation.getParameterName() != null) {
configurer.parameterName(contentnegotiation.getParameterName());
}
Map<String, MediaType> mediaTypes = this.mvcProperties.getContentnegotiation().getMediaTypes();
Map<String, MediaType> mediaTypes = contentnegotiation.getMediaTypes();
mediaTypes.forEach(configurer::mediaType);
List<MediaType> defaultContentTypes = contentnegotiation.getDefaultContentTypes();
if (!CollectionUtils.isEmpty(defaultContentTypes)) {
configurer.defaultContentType(defaultContentTypes.toArray(new MediaType[0]));
}
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
package org.springframework.boot.autoconfigure.web.servlet;

import java.time.Duration;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.boot.context.properties.ConfigurationProperties;
Expand Down Expand Up @@ -317,6 +319,12 @@ public static class Contentnegotiation {
*/
private String parameterName;

/**
* The default content types to be used when no specific content type is
* requested.
*/
private List<MediaType> defaultContentTypes = new ArrayList<>();

public boolean isFavorParameter() {
return this.favorParameter;
}
Expand All @@ -341,6 +349,14 @@ public void setParameterName(String parameterName) {
this.parameterName = parameterName;
}

public List<MediaType> getDefaultContentTypes() {
return this.defaultContentTypes;
}

public void setDefaultContentTypes(List<MediaType> defaultContentTypes) {
this.defaultContentTypes = defaultContentTypes;
}

}

public static class Pathmatch {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -79,13 +79,15 @@
import org.springframework.format.support.FormattingConversionService;
import org.springframework.http.CacheControl;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.accept.ContentNegotiationManager;
import org.springframework.web.accept.FixedContentNegotiationStrategy;
import org.springframework.web.accept.ParameterContentNegotiationStrategy;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
Expand Down Expand Up @@ -577,6 +579,22 @@ void customMediaTypes() {
});
}

@Test
void defaultContentTypes() {
this.contextRunner
.withPropertyValues("spring.mvc.contentnegotiation.default-content-types:application/json,application/xml")
.run((context) -> {
RequestMappingHandlerAdapter adapter = context.getBean(RequestMappingHandlerAdapter.class);
ContentNegotiationManager contentNegotiationManager = (ContentNegotiationManager) ReflectionTestUtils
.getField(adapter, "contentNegotiationManager");
assertThat(contentNegotiationManager).isNotNull();
assertThat(contentNegotiationManager.getStrategy(FixedContentNegotiationStrategy.class))
.extracting(FixedContentNegotiationStrategy::getContentTypes)
.asInstanceOf(InstanceOfAssertFactories.list(MediaType.class))
.containsExactly(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML);
});
}

@Test
void formContentFilterIsAutoConfigured() {
this.contextRunner.run((context) -> assertThat(context).hasSingleBean(OrderedFormContentFilter.class));
Expand Down