Spring的MockMVC响应不匹配浏览器响应 [英] Spring's MockMVC Responses Don't Match Browser Response
问题描述
出于某种原因,如果我通过浏览器或通过我的MockMVC测试类访问它,我的Spring控制器会返回不同的响应。 有人可以帮助我找出原因吗?
For some reason, my Spring controller is returning different responses if I access it via a browser or via my MockMVC test class. Could someone help me spot why?
首先是控制器方法:
@RequestMapping(value = APPLICATIONS_ROOT, method = GET)
public HttpEntity<ApplicationsListResource> listApplications(@PageableDefault(page = DEFAULT_START,
size = DEFAULT_HITS_PER_PAGE) Pageable pageable) {
Page<Application> applications = applicationRepository.findAll(pageable);
ApplicationsListResource applicationListResource = new ApplicationsListResource(applications, pageable);
return new ResponseEntity<ApplicationsListResource>(applicationListResource, HttpStatus.OK);
}
显然,那里有一些未知的类。 ApplicationListResource
extends ResourceSupport
并包含一个名为 ApplicationResource
的列表应用
。此 ApplicationResource
还扩展 ResourceSupport
。
Obviously there's a few unknown classes in there. ApplicationListResource
extends ResourceSupport
and contains a list of ApplicationResource
called applications
. This ApplicationResource
also extends ResourceSupport
.
当我访问时通过浏览器的代码,我会得到以下内容:
When I access the code via the browser, I'll get something along the lines of:
{
"_links": {
"self": {
"href": "http://localhost:10000/applications{?page,size,sort}",
"templated": true
}
},
"_embedded": {
"applications": [{
"displayname": "One",
"description": "My Test Application!",
"locations": ["http://foo.com"],
"_links": {
"self": { "href": "http://localhost:10000/applications/one" }
}
}, {
...
}]
},
"page": {
"size": 20,
"totalElements": 7,
"totalPages": 1,
"number": 0
}
}
看起来HATEOAS符合我的要求。但是当我通过MockMVC请求时......
Looks HATEOAS compliant to me. But when I go via a MockMVC request...
getMockMvc().perform(get(APPLICATIONS_ROOT)).andExpect(status().isOk()).andExpect(content().contentType(MediaTypes.HAL_JSON)).andExpect(jsonPath("$._embedded.applcations", hasSize(5))).andReturn();
响应中没有符合HATEOAS标准的元素,因此我的测试在jsonPath检查上失败:
The responses have no HATEOAS compliant elements in them so my tests fail on the jsonPath check:
{
"page" : 0,
"size" : 10,
"sort" : null,
"total" : 5,
"applications" : [ {
"name" : "one",
"version" : "1.0",
...
我尝试更改MockMVC方法的GET请求上的ContentType但它使得没有不同。在浏览器中,我没有设置任何特定的内容类型,标题等。
I've tried changing the ContentType on the GET request for the MockMVC method but it makes no difference. In the browser, I'm not setting any specific content type, headers etc.
我知道MockMVC类使得HTTP请求与通常的RestTemplate存在某些差异,所以也许这是这样的吗?任何人都可以看到我遗漏的任何明显的东西吗?
I know the MockMVC class makes it HTTP requests with certain differences from the usual RestTemplate so perhaps it's something like this? Can anyone see anything obvious I am missing?
如果需要,我会添加额外的代码,但它会让问题比现在更加冗长。
I will add additional code if needs be but it would have made the question even more long winded than it is currently.
推荐答案
Spring HATEOAS添加了正确渲染hal的其他配置,请查看详细信息: http://docs.spring.io/spring-hateoas/docs/0.19.0.RELEASE/ reference / html / #configuration 。
Spring HATEOAS adds additional configuration for rendering hal properly, check this for details: http://docs.spring.io/spring-hateoas/docs/0.19.0.RELEASE/reference/html/#configuration.
简而言之,它增加了 MixIn
添加的 Jackson2HalModule
和 HalHandlerInstantiator
到 ObjectMapper
。它全部配置在 HypermediaSupportBeanDefinitionRegistrar.java
( https://github.com/spring-projects/spring-hateoas/blob/master/src/main/java/org/springframework/ hateoas / config / HypermediaSupportBeanDefinitionRegistrar.java )
In a nutshell it adds proper MixIn
s added by Jackson2HalModule
and HalHandlerInstantiator
to the ObjectMapper
. It's all configured in HypermediaSupportBeanDefinitionRegistrar.java
(https://github.com/spring-projects/spring-hateoas/blob/master/src/main/java/org/springframework/hateoas/config/HypermediaSupportBeanDefinitionRegistrar.java)
如果您使用的是独立的mockMvc配置,则必须配置 ObjectMapper
手动模仿spring的行为。我遇到了同样的问题,最后在我的测试中添加了以下配置:
If you're using standalone mockMvc configuration you have to configure the ObjectMapper
manually to mimic spring's behaviour. I ran into same problem and ended up adding following configuration to my tests:
mockMvc = MockMvcBuilders.standaloneSetup(controller)
.setMessageConverters(
new MappingJackson2HttpMessageConverter(configureObjectMapper()))
.build();
和
private ObjectMapper configureObjectMapper() {
return Jackson2ObjectMapperBuilder.json()
.modules(new Jackson2HalModule())
.handlerInstantiator(new Jackson2HalModule.HalHandlerInstantiator(
new DelegatingRelProvider(
OrderAwarePluginRegistry.create(Arrays.asList(
new EvoInflectorRelProvider(),
new AnnotationRelProvider()))),
null))
.build();
}
这篇关于Spring的MockMVC响应不匹配浏览器响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!