In my project I've implemented Spring 7 versioning.
But MockMvc tests and springdoc REST OpenApi documentation failed with exception MissingApiVersionException
In my project I've implemented Spring 7 versioning.
But MockMvc tests and springdoc REST OpenApi documentation failed with exception MissingApiVersionException
According REST API Versioning for MockMvc it is needed to configurate ApiVersionInserter and use .apiVersion() or just add .header() into MockHttpServletRequestBuilder builder
@SpringBootTest
@Transactional
@AutoConfigureMockMvc
@ActiveProfiles("test")
//https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-testing-spring-boot-applications-testing-with-mock-environment
public abstract class AbstractControllerTest {
@Autowired
private MockMvc mockMvc;
protected ResultActions perform(MockHttpServletRequestBuilder builder) throws Exception {
builder.header(WebConfig.VERSION_HEADER, WebConfig.CURRENT_VERSION);
return mockMvc.perform(builder);
}
}
For springdoc it is needed add in version configuration
@Configuration
public class WebConfig implements WebMvcConfigurer {
public static final String VERSION_HEADER = "API-Version";
public static final String CURRENT_VERSION = "1.0";
@Override
public void configureApiVersioning(ApiVersionConfigurer configurer) {
configurer
.useRequestHeader(VERSION_HEADER)
.setVersionRequired(false) <--- this or
.setDefaultVersion(CURRENT_VERSION) <--- this or both
.addSupportedVersions(CURRENT_VERSION);
}
}