Skip to content

Regression: SpringHandlerInstantiator breaks stand-alone support in the MVC Test framework [SPR-13375] #17957

@spring-projects-issues

Description

@spring-projects-issues

Sam Brannen opened SPR-13375 and commented

The SpringHandlerInstantiator introduced in #15394 always attempts to create new beans using its AutowireCapableBeanFactory. In the case of a StubWebApplicationContext, however, this results in an UnsupportedOperationException being thrown.

This behavior breaks support for JSON serialization in conjunction with the default configuration of the MockMvcBuilders.standaloneSetup() support in the Spring MVC Test framework since it internally uses a StubWebApplicationContext.

See the linked discussion on Stack Overflow for details.

Analysis

The following code in WebMvcConfigurationSupport.addDefaultHttpMessageConverters() is what causes this behavior:

if (jackson2Present) {
	ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().applicationContext(this.applicationContext).build();
	messageConverters.add(new MappingJackson2HttpMessageConverter(objectMapper));
}

The fact that it supplies the ApplicationContext results in a SpringHandlerInstantiator being created in Jackson2ObjectMapperBuilder.configure(), even though a SpringHandlerInstantiator makes no sense for the stand-alone test setup in MVC Test.


Affects: 4.1.3

Reference URL: http://stackoverflow.com/questions/32101611/spring-mockmvc-unsupportedoperationexception-after-upgrading-to-new-spring-versi/32140898

Referenced from: commits spring-attic/spring-framework-issues@028ace9

Backported to: 4.1.8

0 votes, 6 watchers

Metadata

Metadata

Assignees

Labels

in: testIssues in the test modulein: webIssues in web modules (web, webmvc, webflux, websocket)status: backportedAn issue that has been backported to maintenance branchestype: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions