【发布时间】:2015-04-20 16:36:25
【问题描述】:
我已经使用 Gradle 设置了 spring boot 应用程序。现在我明白了 @EnableAutoConfiguration 根据类路径中的依赖关系配置应用程序。我很高兴避免所有的管道,但我希望不会发生的事情开始发生。
这是我的依赖项:
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 的 sn-p:
@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...
}
post 请求在测试中返回标准 JSON 响应 - 而不是 HAL+JSON。有没有办法重新配置它,以便使用 MockServletContext 对 @RestController 进行单元测试会产生 HAL+JSON 或回到问题 1 - 如何显式配置响应格式以使 Jackson 序列化程序不会产生 hal+json?
【问题讨论】:
-
你见过
org.springframework.hateoas.MediaTypes.HAL_JSON吗? -
嗯,但是@JoaoEvangelista 那么为什么在运行时向控制器发出明确定义的媒体类型请求:“应用程序/json”返回一个媒体类型为“应用程序/json”的响应,但仍然是“应用程序/hal+json" 风味?如果我从映射配置中删除生成值,则响应具有 Media-Type: "application/hal-json"。
标签: java spring spring-boot spring-hateoas