HAL与Spring Hateoas和Mvc(无启动)

时间:2015-11-03 09:37:01

标签: spring spring-mvc spring-hateoas hal

我正在使用spring mvc和hateoas构建一个宁静的api。以前,我使用带有hateoas的spring boot,服务器按照我的预期呈现了响应。现在,我没有使用spring boot,服务器无法正确呈现响应。根据我的研究,我发现有些人在谈论涉及消息转换器配置的解决方案。那么,除了注释@EnableHypermediaSupport之外,在没有弹簧启动的情况下设置媒体类型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"}
},
...

3 个答案:

答案 0 :(得分:0)

如果您正在使用Spring Data JPA进行数据访问,则可以通过添加EnableSpringDataWebSupport以声明方式启用其Web支持。这将为ArgumentResolvers方法注册一堆Controller。简而言之,您可以向方法添加PagedResourcesAssembler参数,并使用其toResource方法将Page ed实体转换为资源,如下所示:

    @RequestMapping(method = GET)
    public ResponseEntity getAll(PagedResourcesAssembler ass) {
        Page<User> users = userRepository.findAll(new PageRequest(0, 10));
        PagedResources pagedResources = ass.toResource(users, assembler);

        return ok(pagedResources);
    }

这个json输出:

{"_embedded":{...},"_links":{"self":{"href":"http://localhost:8080/users"}},"page":{...}}

答案 1 :(得分:0)

一个选项是让您的模型扩展 org.springframework.hateoas.ResourceSupport

public class MyModelClass extends ResourceSupport

然后

@RequestMapping(value="/myresource", method=RequestMethod.GET)
public ResponseEntity<Iterable<MyModelClass>> getAll() {
Iterable<MyModelClass> all= repository.findAll();
for(MyModelClass p : all) {
p.add(linkTo(methodOn(MyController.class).getAll()).slash(p.getId()).withSelfRel());
}
return new ResponseEntity<>(all, HttpStatus.OK);
}

答案 2 :(得分:0)

基本上我使用EntityLinks来构建链接,而不是从任何依赖于框架的类扩展我的Pojo。

@RestController
@ExposesResourceFor(ProjectServiceResponse.class) 
public class ProjectController {
  @Autowired
  private org.springframework.hateoas.EntityLinks entityLinks;

  public HttpEntity<Resource<ProjectServiceResponse>> get(...){
    Resource resource = new Resource<ProjectServiceResponse>(response);
    resource.add(entityLinks.linkToCollectionResource(ProjectServiceResponse.class).withSelfRel());
    return new ResponseEntity<Resource<ProjectServiceResponse>>(resource, HttpStatus.OK);
  }
}

响应:

"_links": {
"self": {
  "href": "http://localhost:8080/project"
}

在上面的代码中,ProjectServiceResponse.class是POJO。

您可以在github

上推荐完整的项目