3

我正在使用 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"}
},
...
4

3 回答 3

0

如果您使用 Spring Data JPA 进行数据访问,您可以通过添加EnableSpringDataWebSupport. 这将注册一堆ArgumentResolversforController方法。简而言之,您可以PagedResourcesAssembler向您的方法添加参数并使用其toResource方法将Pageed 实体转换为资源,如下所示:

    @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":{...}}
于 2015-11-03T12:03:11.953 回答
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上的完整项目

于 2015-11-12T13:30:45.737 回答
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);
}
于 2015-11-10T19:19:12.333 回答