我已经使用Gradle设置了spring boot应用程序。现在,我确实知道@EnableAutoConnfiguration基于类路径中的依赖项配置应用程序。我很高兴避免所有麻烦,但事情开始发生,但我希望不会。
这是我的依赖项:
dependencies {
compile('org.springframework.boot:spring-boot-starter-web:1.2.3.RELEASE')
compile 'org.springframework.hateoas:spring-hateoas:0.17.0.RELEASE'
compile 'org.springframework.plugin:spring-plugin-core:1.2.0.RELEASE'
compile 'org.springframework.boot:spring-boot-starter-data-jpa'
compile 'com.google.guava:guava:18.0'
compile 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310'
compile 'commons-beanutils:commons-beanutils:1.9.2'
runtime 'org.hsqldb:hsqldb:2.3.2'
testCompile 'org.springframework.boot:spring-boot-starter-test'
testCompile 'com.jayway.jsonpath:json-path:2.0.0'
}
我的应用程序类:
@ComponentScan("org.home.project")
@SpringBootApplication
//@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
来自UserController的代码段:
@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public HttpEntity<ResourceSupport> create(@Valid @RequestBody UserCreateRequest ucr, BindingResult bindingResult) {
if (bindingResult.hasErrors()) throw new InvalidRequestException("Bad Request", bindingResult);
Long userId = userService.create(ucr);
ResourceSupport resource = new ResourceSupport();
resource.add(linkTo(UserEndpoint.class).withSelfRel());
resource.add(linkTo(methodOn(UserEndpoint.class).update(userId, null, null)).withRel(VIEW_USER));
resource.add(linkTo(methodOn(UserEndpoint.class).delete(userId)).withRel(DELETE_USER));
return new ResponseEntity(resource, HttpStatus.CREATED);
}
UserController.java有两个注释:
@RestController
@RequestMapping(value = "/users", produces = MediaType.APPLICATION_JSON_VALUE)
首先-请注意已注释掉的@EnableHyperdiaSupport批注-尽管已在请求中生成了媒体类型和媒体类型,但ResourceSupport实例中的链接仍被序列化为hal + json格式。当在依赖项中引入“ org.springframework.plugin:spring-plugin-core:1.2.0.RELEASE”时,会自动发生这种情况。如何进行显式配置?
另一个问题是单元测试。
通过:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MockServletContext.class)
@WebAppConfiguration
public class UserControllerTest {
...ommited for brevity...
@InjectMocks
private UserController testObject;
@Before
public void setUp() throws Exception {
initMocks(this);
mockMvc = standaloneSetup(testObject).build();
}
@Test
public void testUserCreatedLinks() throws Exception {
mockMvc.perform(post("/users").contentType(MediaType.APPLICATION_JSON).content(data))
.andExpect(status().isCreated())
.andExpect(content().contentType(MediaType.APPLICATION_JSON)).andExpect(jsonPath("$.links.[*].rel", hasItem("self")));
}
...ommited fro brevity...
}
发布请求在测试中返回标准的JSON响应-不是HAL + JSON。有没有一种方法可以重新配置它,以便使用MockServletContext对@RestController进行单元测试将产生HAL + JSON或回到问题编号1-如何显式配置响应格式,以便Jackson序列化器不会产生hal + json?
您正在使用Spring MVC Test来运行测试standaloneSetup
,该测试仅需最少的配置即可启动并运行控制器。该配置与运行整个应用程序时将使用的配置不同。
如果要使用相同的配置,可以使用webAppContextSetup
:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
public class SomeTests {
@Autowired
private WebApplicationContext context;
private MockMvc mockMvc;
@Before
public void setUp() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();
}
}
另外,您可以在独立设置中复制Spring HATEOAS的配置。注意,这冒着测试配置偏离应用程序配置的风险。您将这样创建MockMvc
实例:
TypeConstrainedMappingJackson2HttpMessageConverter messageConverter =
new TypeConstrainedMappingJackson2HttpMessageConverter(ResourceSupport.class);
messageConverter.setSupportedMediaTypes(Arrays.asList(MediaTypes.HAL_JSON));
ObjectMapper objectMapper = messageConverter.getObjectMapper();
objectMapper.registerModule(new Jackson2HalModule());
objectMapper.setHandlerInstantiator(
new Jackson2HalModule.HalHandlerInstantiator(new DefaultRelProvider(), null));
MockMvc mockMvc = MockMvcBuilders.standaloneSetup(testObject)
.setMessageConverters(messageConverter).build();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句