【发布时间】:2015-11-03 09:37:01
【问题描述】:
我正在使用 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"}
},
...
【问题讨论】:
-
不够是什么意思?你期望什么,你得到什么? spring boot 添加了像 jackson 这样的依赖项,这可能会产生神奇的效果。
-
我编辑了我的问题,包括我得到的和我期望的。我已经尝试包含以下依赖项并且问题仍然存在:json-path、jackson-databind、jackson-core 和 jackson-annotations
-
好奇,不想用Spring Boot的原因是什么?
-
最初我使用的是 spring boot,但放弃它的原因之一是我在使用 @ControllerAdvice 进行错误处理时遇到了困难。
标签: spring spring-mvc spring-hateoas hal