spring hateoas 1.0改动

spring hateoas 1.0

前言

因为在看spring实战第五版敲代码时候发现改组件已经更新,书中没改,旧方法已在新版本弃用

Resource

改为EntityModel

变化

和原来用法位置一样,改了名而已

在这里插入图片描述

Resources

改为CollectionModel

变化

和原来用法位置一样,改了名而已

在这里插入图片描述

ResourceSupport

改为RepresentationModel

变化

使用方法和原来无差别

public class TacoModel extends RepresentationModel<TacoModel> {
    @Getter
    private final String name;

    @Getter
    private final Date createAt;

    @Getter
    private final List<Ingredient> ingredients;

    public TacoModel(Taco taco) {
        this.name = taco.getName();
        this.createAt = taco.getCreatedAt();
        this.ingredients = taco.getIngredients();
    }
}

ResourceAssemblerSuppor

改为RepresentationModelAssemblerSuppor

变化

使用起来和原先没什么太大差异,只是映射名称和方法名变了

public class TacoModelAssembler extends RepresentationModelAssemblerSupport<Taco, TacoModel> {
    public TacoModelAssembler() {
        super(DesignTacoApiController.class, TacoModel.class);
    }

    @Override
    public TacoModel toModel(Taco taco) {
        return createModelWithId(taco.getId(), taco);
    }

    @Override
    protected TacoModel instantiateModel(Taco taco) {
        return new TacoModel(taco);
    }
}
public CollectionModel<TacoModel> recentTacos() {
    PageRequest page = PageRequest.of(0, 12, Sort.by("createdAt").descending());
    List<Taco> tacos = tacoRepository.findAll(page).getContent();

    CollectionModel<TacoModel> recentCollectionModel = new TacoModelAssembler().toCollectionModel(tacos);

    recentCollectionModel.add(
            linkTo(methodOn(DesignTacoApiController.class).recentTacos())
                    .withRel("recents")
    );
    return recentCollectionModel;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 引入新的依赖 在pom.xml文件中引入Spring Boot 2.4.3版本的依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.4.3</version> </dependency> ``` 2. Application.properties文件更改 在application.properties文件中,需要更改的地方如下: ```properties spring.main.allow-bean-definition-overriding=true ``` 这个属性是Spring Boot 2.4.3中新增的,用于允许Bean定义的覆盖。在Spring Boot 2.3.10中默认是false。 3. 日志配置更改 在Spring Boot 2.4.3中,日志框架从Logback改为了Log4j2。如果之前使用的是Logback,需要将其依赖移除,并添加Log4j2的依赖: ```xml <!--移除Logback依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <!--添加Log4j2依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> <version>2.4.3</version> </dependency> ``` 同时,需要将application.properties文件中的日志配置改为Log4j2的日志配置。 4. 引入新的特性 Spring Boot 2.4.3中引入了一些新的特性,比如: - 优化了响应式编程的支持; - 支持Java 15; - 支持JDBC的异步执行; - 支持配置文件中的if条件判断等。 可以根据实际情况,选择使用这些新特性。 5. 单元测试更改 在Spring Boot 2.4.3中,JUnit 5成为了默认的测试框架,因此需要修改测试类的引入: ```java import org.junit.jupiter.api.Test; ``` 同时,需要将Gradle或Maven中的JUnit依赖改为JUnit 5的依赖。 6. 其他更改 在Spring Boot 2.4.3中,一些类和方法进行了更改,比如: - DataSourceProperties类中的url属性改为jdbc-url; - RestTemplateBuilder类中的build()方法改为buildRestTemplate()方法。 需要根据实际情况进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值