我正在使用 spring mvc 和 hateoas 来构建一个宁静的 api。以前,我将 spring boot 与 hatoas 一起使用,服务器按预期呈现响应。现在,我没有使用 spring boot,服务器没有正确呈现响应。根据我的研究,我发现有些人在谈论涉及配置消息转换器的解决方案。那么,除了注解@EnableHypermediaSupport,在没有spring boot的情况下设置媒体类型HAL还需要什么?
ApiConfiguration.java:
@Configuration
@ComponentScan
@EnableWebMvc
@EnableHypermediaSupport(type = { HypermediaType.HAL })
public class ApiConfiguration {
}
ApiInitializer.java:
public class ApiInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return null;
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { ApiConfiguration.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
pom.xml:
...
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>4.2.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.hateoas</groupId>
<artifactId>spring-hateoas</artifactId>
<version>0.19.0.RELEASE</version>
</dependency>
...
AdaptationRestController.java:
@RestController
@ExposesResourceFor(Adaptation.class)
@RequestMapping(value = "/adaptations")
public class AdaptationRestController {
...
@RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = "application/vnd.xpto-adaptation+json")
public ResponseEntity<AdaptationResource> getAdaptation(@PathVariable("id") String adaptationId) {
Adaptation adaptation = adaptationGateway.getAdaptation(adaptationId);
AdaptationResource adaptationResource = adaptationResourceAssembler.toResource(adaptation);
return new ResponseEntity<AdaptationResource>(adaptationResource, HttpStatus.OK);
}
}
AdaptationResource.java:
public class AdaptationResource extends Resource<Adaptation> {
public AdaptationResource(Adaptation adaptation) {
super(adaptation);
}
}
我得到什么:
"links": [
{
"rel": "self",
"href": "http://xpto.com"
}
],
...
应该是什么:
"_links": {
"self": {"href": "http://xpto.com"}
},
...