最近在阅读Spring实战第五版中文版,书中第6章关于Spring HATEOAS部分代码使用的是Spring HATEOAS 0.25的版本,而最新的Spring HATEOAS 1.0对旧版的API做了升级,导致在使用新版Spring Boot(截至文章发布日最新的Spring Boot版本为2.2.4)加载的Spring HATEOAS 1.0.3无法正常运行书中代码,所以我决定在此对书中的代码进行迁移升级。

在线阅读书中这一部分:https://potoyang.gitbook.io/spring-in-action-v5/di-6-zhang-chuang-jian-rest-fu-wu/6.2-qi-yong-chao-mei-ti

Spring HATEOAS 1.0 版本的变化

封装结构的最大变化是通过引入超媒体类型注册API来实现的,以支持Spring HATEOAS中的其他媒体类型。这导致客户端API和服务器API(分别命名的包)以及包中的媒体类型实现的明确分离 mediatype

最大的变化就是将原来的资源表示为模型,具体变化如下。

ResourceSupportResourceResourcesPagedResources组类从来没有真正感受到适当命名。毕竟,这些类型实际上并不表示资源,而是表示模型,可以通过超媒体信息和提供的内容来丰富它们。这是新名称映射到旧名称的方式:

  • ResourceSupport 就是现在 RepresentationModel

  • Resource 就是现在 EntityModel

  • Resources 就是现在 CollectionModel

  • PagedResources 就是现在 PagedModel

因此,ResourceAssembler已被重命名为RepresentationModelAssembler和及其方法toResource(…),并分别toResources(…)被重命名为toModel(…)toCollectionModel(…)。名称更改也反映在中包含的类中TypeReferences

  • RepresentationModel.getLinks()现在公开了一个Links实例(通过List<Link>),该实例公开了其他API,以Links使用各种策略来连接和合并不同的实例。同样,它已经变成了自绑定的泛型类型,以允许向实例添加链接的方法返回实例本身。

  • LinkDiscovererAPI已移动到client包。

  • LinkBuilderEntityLinksAPI已经被移到了server包。

  • ControllerLinkBuilder已移入server.mvc,不推荐使用替换WebMvcLinkBuilder

  • RelProvider已重命名为LinkRelationProvider并返回LinkRelation实例,而不是String

  • VndError已移至mediatype.vnderror套件。

另外注意 ResourceProcessor 接口被 RepresentationModelProcessor 取代

更多变化请参考Spring HATEOAS文档:https://spring.io/projects/spring-hateoas

代码迁移升级

书中程序清单6.4 为资源添加超链接

    @GetMapping("/recent")
public CollectionModel<EntityModel<Taco>> recentTacos() {
PageRequest page = PageRequest.of(
0, 12, Sort.by("createdAt").descending()); List<Taco> tacos = tacoRepo.findAll(page).getContent();
CollectionModel<EntityModel<Taco>> recentResources = CollectionModel.wrap(tacos); recentResources.add(
new Link("http://localhost:8080/design/recent", "recents"));
return recentResources;
}

消除URL硬编码

    @GetMapping("/recent")
public CollectionModel<EntityModel<Taco>> recentTacos() {
PageRequest page = PageRequest.of(
0, 12, Sort.by("createdAt").descending()); List<Taco> tacos = tacoRepo.findAll(page).getContent();
CollectionModel<EntityModel<Taco>> recentResources = CollectionModel.wrap(tacos); recentResources.add(
linkTo(methodOn(DesignTacoController.class).recentTacos()).withRel("recents"));
return recentResources;
}
程序清单6.5 能够携带领域数据和超链接列表taco资源
public class TacoResource extends RepresentationModel<TacoResource> {

    @Getter
private String name; @Getter
private Date createdAt; @Getter
private List<Ingredient> ingredients; public TacoResource(Taco taco) {
this.name = taco.getName();
this.createdAt = taco.getCreatedAt();
this.ingredients = taco.getIngredients();
}
}
程序清单6.6 装配taco资源的资源装配器
public class TacoResourceAssembler extends RepresentationModelAssemblerSupport<Taco, TacoResource> {
/**
* Creates a new {@link RepresentationModelAssemblerSupport} using the given controller class and resource type.
*
* @param controllerClass DesignTacoController {@literal DesignTacoController}.
* @param resourceType TacoResource {@literal TacoResource}.
*/
public TacoResourceAssembler(Class<?> controllerClass, Class<TacoResource> resourceType) {
super(controllerClass, resourceType);
} @Override
protected TacoResource instantiateModel(Taco taco) {
return new TacoResource(taco);
} @Override
public TacoResource toModel(Taco entity) {
return createModelWithId(entity.getId(), entity);
}
}

之后对recentTacos()的调整

@GetMapping("/recentNew")
public CollectionModel<TacoResource> recentTacos() {
PageRequest page = PageRequest.of(
0, 12, Sort.by("createdAt").descending());
List<Taco> tacos = tacoRepo.findAll(page).getContent(); CollectionModel<TacoResource> tacoResources =
new TacoResourceAssembler(DesignTacoController.class, TacoResource.class).toCollectionModel(tacos); tacoResources.add(linkTo(methodOn(DesignTacoController.class)
.recentTacos())
.withRel("recents"));
return tacoResources;
}

创建 IngredientResource 对象

@Data
public class IngredientResource extends RepresentationModel<IngredientResource> {
public IngredientResource(Ingredient ingredient) {
this.name = ingredient.getName();
this.type = ingredient.getType();
} private final String name;
private final Ingredient.Type type;
}
IngredientResourceAssembler 对象
public class IngredientResourceAssembler extends RepresentationModelAssemblerSupport<Ingredient, IngredientResource> {
/**
* Creates a new {@link RepresentationModelAssemblerSupport} using the given controller class and resource type.
*
* @param controllerClass IngredientController {@literal IngredientController}.
* @param resourceType IngredientResource {@literal IngredientResource}.
*/
public IngredientResourceAssembler(Class<?> controllerClass, Class<IngredientResource> resourceType) {
super(controllerClass, resourceType);
} @Override
protected IngredientResource instantiateModel(Ingredient entity) {
return new IngredientResource(entity);
} @Override
public IngredientResource toModel(Ingredient entity) {
return createModelWithId(entity.getId(), entity);
}
}

对 TacoResource 对象的修改

public class TacoResource extends RepresentationModel<TacoResource> {
private static final IngredientResourceAssembler
ingredientAssembler = new IngredientResourceAssembler(IngredientController.class, IngredientResource.class); @Getter
private String name; @Getter
private Date createdAt; @Getter
private CollectionModel<IngredientResource> ingredients; public TacoResource(Taco taco) {
this.name = taco.getName();
this.createdAt = taco.getCreatedAt();
this.ingredients = ingredientAssembler.toCollectionModel(taco.getIngredients()); }
}

程序清单6.7

@RepositoryRestController
public class RecentTacosController {
private TacoRepository tacoRepo; public RecentTacosController(TacoRepository tacoRepo) {
this.tacoRepo = tacoRepo;
} /**
* 虽然@GetMapping映射到了“/tacos/recent”路径,但是类级别的@Repository RestController注解会确保这个路径添加
* Spring Data REST的基础路径作为前缀。按照我们的配置,recentTacos()方法将会处理针对“/api/tacos/recent”的GET请求。
* */
@GetMapping(path="/tacos/recent", produces="application/hal+json")
public ResponseEntity<CollectionModel<TacoResource>> recentTacos() {
PageRequest page = PageRequest.of(
0, 12, Sort.by("createdAt").descending());
List<Taco> tacos = tacoRepo.findAll(page).getContent(); CollectionModel<TacoResource> tacoResources =
new TacoResourceAssembler(DesignTacoController.class, TacoResource.class).toCollectionModel(tacos); tacoResources.add(
linkTo(methodOn(RecentTacosController.class).recentTacos())
.withRel("recents"));
return new ResponseEntity<>(tacoResources, HttpStatus.OK);
} }
程序清单6.8 为Spring Data REST端点添加自定义的链接
    @Bean
public RepresentationModelProcessor<PagedModel<EntityModel<Taco>>> tacoProcessor(EntityLinks links) { return new RepresentationModelProcessor<PagedModel<EntityModel<Taco>>>() {
@Override
public PagedModel<EntityModel<Taco>> process(PagedModel<EntityModel<Taco>> resource) {
resource.add(
links.linkFor(Taco.class)
.slash("recent")
.withRel("recents"));
return resource;
}
};
}

另一种写法

如果你觉得写使用资源装配器有点麻烦,那么你还可以采用这种方法。

    @GetMapping("/employees")
public ResponseEntity<CollectionModel<EntityModel<Taco>>> findAll() {
PageRequest page = PageRequest.of(
0, 12, Sort.by("createdAt").descending());
List<EntityModel<Taco>> employees = StreamSupport.stream(tacoRepo.findAll(page).spliterator(), false)
.map(employee -> new EntityModel<>(employee,
linkTo(methodOn(DesignTacoController.class).findOne(employee.getId())).withSelfRel(),
linkTo(methodOn(DesignTacoController.class).findAll()).withRel("employees")))
.collect(Collectors.toList()); return ResponseEntity.ok(
new CollectionModel<>(employees,
linkTo(methodOn(DesignTacoController.class).findAll()).withSelfRel()));
}
@GetMapping("/employees/{id}")
public ResponseEntity<EntityModel<Taco>> findOne(@PathVariable long id) { return tacoRepo.findById(id)
.map(employee -> new EntityModel<>(employee,
linkTo(methodOn(DesignTacoController.class).findOne(employee.getId())).withSelfRel(), //
linkTo(methodOn(DesignTacoController.class).findAll()).withRel("employees"))) //
.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}

参考来源:https://github.com/spring-projects/spring-hateoas-examples/tree/master/simplified

END

将Spring实战第5版中Spring HATEOAS部分代码迁移到Spring HATEOAS 1.0的更多相关文章

  1. 【spring实战第五版遇到的坑】第14章spring.cloud.config.uri和token配置项无效

    本文使用的Spring Boot版本为:2.1.4.RELEASE Spring Cloud版本为:Greenwich.SR1 按照书上的做法,在application.yml中配置配置服务器的地址和 ...

  2. Spring实战(第4版).pdf - 百度云资源

    http://www.supan.vip/spring%E5%AE%9E%E6%88%98 Spring实战(第4版).pdf 关于本书 Spring框架是以简化Java EE应用程序的开发为目标而创 ...

  3. Spring实战第4版PDF下载含源码

    下载链接 扫描右侧公告中二维码,回复[spring实战]即可获取所有链接. 读者评价 看了一半后在做评论,物流速度挺快,正版行货,只是运输过程有点印记,但是想必大家和你关注内容,spring 4必之3 ...

  4. Spring in action(Spring实战) 第四版中文翻译

    第一部分 Spring核心 Spring提供了非常多功能,可是全部这些功能的基础是是依赖注入(DI)和面向方面编程(AOP). 第一章 Springing into action 本章包含: Spri ...

  5. Spring 实战 第4版 读书笔记

    第一部分:Spring的核心 1.第一章:Spring之旅 1.1.简化Java开发 创建Spring的主要目的是用来替代更加重量级的企业级Java技术,尤其是EJB.相对EJB来说,Spring提供 ...

  6. 【spring实战第五版遇到的坑】4.2.3中LDAP内嵌服务器不启动的问题

    按照4.2.3中的指导一步一步的去做,在登录界面进行登录时,报错了,报错信息是LDAP服务器连接不上. 后来查了一些资源发现还需要加入一些其他的依赖,如下: <dependency> &l ...

  7. 【spring实战第五版遇到的坑】3.2中配置关系映射时,表名和3.1中不一样

    3.2章中按照书中的步骤写好相应类的映射关系,发现启动时,之前在3.1章中建的表全部被删重新建立了,并且Ingredient表的数据没了,由于使用了JPA,默认使用的是hibernate,在启动时会删 ...

  8. 【spring实战第五版遇到的坑】3.1中的例子报错

    按照书中的例子,一直做到第3.1章使用JDBC读写数据时,在提交设计的taco表单时,报了如下的异常信息: Failed to convert property value of type java. ...

  9. Spring实战 (第3版)——AOP

    在软件开发中,分布于应用中多处的功能被称为横切关注点.通常,这些横切关注点从概念上是与应用的 业务逻辑相分离的(但是往往直接嵌入到应用的业务逻辑之中).将这些横切关注点与业务逻辑相分离正是 面向切面编 ...

随机推荐

  1. Hyperledger Fabric1.4 安装

    Hyperledger Fabric 依赖的软件版本查看官方 github 地址 https://github.com/hyperledger/fabric 下文件 /docs/source/prer ...

  2. linux修改环境变量的三种方法【转】

    [环境变量配置的三个方法] 如想将一个路径加入到$PATH中,可以像下面这样做:  1. 控制台中,不赞成使用这种方法,因为换个shell,你的设置就无效了,因此这种方法仅仅是临时使用,以后要使用的时 ...

  3. 做前端的你还没用这些软件?? out 啦

    1. 编辑器 写代码只是生产软件过程中的一环.无论是数据结构.编译原理.操作系统还是组成原理都是编码的重要基础,试问没有学过编译原理的人能够针对性地进行编译优化吗?不懂操作系统的人能玩得转linux吗 ...

  4. ES 服务器 索引、类型仓库基类 BaseESStorage

    /******************************************************* * * 作者:朱皖苏 * 创建日期:20180508 * 说明:此文件只包含一个类,具 ...

  5. C++ 排序引用的优化

    链接:https://www.nowcoder.com/acm/contest/83/B来源:牛客网 题目描述 第一次期中考终于结束啦!沃老师是个语文老师,他在评学生的作文成绩时,给每位学生的分数都是 ...

  6. Springboot + 持久层框架JOOQ

    简介 官网链接 JOOQ是一套持久层框架,主要特点是: 逆向工程,自动根据数据库结构生成对应的类 流式的API,像写SQL一样 提供类型安全的SQL查询,JOOQ的主要优势,可以帮助我们在写SQL时就 ...

  7. [bzoj5507] [洛谷P5305] [gzoi2019]旧词

    Descriptioin 浮生有梦三千场 穷尽千里诗酒荒 徒把理想倾倒 不如早还乡 温一壶风尘的酒 独饮往事迢迢 举杯轻思量 泪如潮青丝留他方 --乌糟兽/愚青<旧词> 你已经解决了五个问 ...

  8. 编程基础--XML约束

    2020年新年第一天,不写一篇博客纪念一下都感觉对不起这个跨年 为什么会写一篇关于xml的博客呢?xml在编程中用的又不多,再多也用不着自己写约束文件,只要能看懂就行了不是吗?嗯,没别的原因,就是想研 ...

  9. SpringBoot项目的parent依赖和配置文件*.properties、*.yml详解

    1.idea创建SpringBoot项目 idea创建SpringBoot项目应该对很多人来说已经是菜到不能到菜的操作了,但是对于初学者小白来说,还是要讲解一下的.打开idea,然后选择Spring ...

  10. CGI fastCgi php-fpm PHP-CGI 辨析

    CGI fastCgi php-fpm PHP-CGI 辨析 LNMP环境中的nginx是不支持php的,需要通过fastcgi插件来处理有关php的请求.而php需要php-fpm这个组件提供该功能 ...