Spring Data REST入门(三):自定义配置

一、基础配置 
Spring Data REST的基础配置定义在RepositoryRestConfiguration(org.springframework.data.rest.core.config.RepositoryRestConfiguration)类中。

这里写图片描述 
可以通过继承 
@Component 
public class CustomizedRestMvcConfiguration extends RepositoryRestConfigurerAdapter {

@Override 
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) { 
configuration.setBasePath(“/api”) 


或者

@Configuration
class CustomRestMvcConfiguration {

  @Bean
  public RepositoryRestConfigurer repositoryRestConfigurer() {

    return new RepositoryRestConfigurerAdapter() {

      @Override
      public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
        configuration.setBasePath("/api")
      }
    };
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

这两种方式来配置相应的信息。如果使用的是Spring Boot,则可以在application.properties中直接进行配置。

spring.data.rest.basePath=/api
  • 1

这里只配置了basePath,其他配置同理 
二、自定义输出字段

1、隐藏某个字段

public class User {

    /**
     * 指定id为主键,并设置为自增长
     */
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @GenericGenerator(name = "increment", strategy = "increment")
    private long id;
    private String name;
    @JsonIgnore
    private String password;
    private int age;
    private boolean sex;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

比如在实体对象User中,我们不希望password 序列化未JSON,在上篇博客中说到,Spring Data REST默认使用的是JackSon,则我们就可以使用在需要隐藏的字段添加@JsonIgnore即可 
2、@Projections

@Projection(name="list",types=User.class)
public interface ListUser {
    String getName();
    long getId();
}
  • 1
  • 2
  • 3
  • 4
  • 5

也可以通过@Projection注解实现1中的效果, 
请求URL为:127.0.0.1:8080/user?projection=list 
返回数据:

{
  "_embedded": {
    "users": [
      {
        "name": "小白鱼",
        "id": 1,
        "_links": {
          "self": {
            "href": "http://127.0.0.1:8080/user/1" },
          "user": {
            "href": "http://127.0.0.1:8080/user/1{?projection}",
            "templated": true }
        }
      },
      {
        "name": "小白",
        "id": 2,
        "_links": {
          "self": {
            "href": "http://127.0.0.1:8080/user/2" },
          "user": {
            "href": "http://127.0.0.1:8080/user/2{?projection}",
            "templated": true }
        }
      },
      {
        "name": "小 鱼 ",
        "id": 3,
        "_links": {
          "self": {
            "href": "http://127.0.0.1:8080/user/3" },
          "user": {
            "href": "http://127.0.0.1:8080/user/3{?projection}",
            "templated": true }
        }
      },
      {
        "name": "white yu",
        "id": 4,
        "_links": {
          "self": {
            "href": "http://127.0.0.1:8080/user/4" },
          "user": {
            "href": "http://127.0.0.1:8080/user/4{?projection}",
            "templated": true }
        }
      }
    ]
  },
  "_links": {
    "self": {
      "href": "http://127.0.0.1:8080/user"
    },
    "profile": {
      "href": "http://127.0.0.1:8080/profile/user"
    }
  },
  "page": {
    "size": 20,
    "totalElements": 4,
    "totalPages": 1,
    "number": 0
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72

@Projection还可以用来建立虚拟列

@Projection(name="virtual",types=User.class)
public interface VirtualUser {

    @Value("#{target.name} #{target.age}") 
    String getFullInfo();

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

这里把User中的name和age合并成一列,这里需要注意String getFullInfo();方法名前面一定要加get,不然无法序列化为JSON数据 
url:http://127.0.0.1:8080/user?projection=virtual 
返回数据:

{
  "_embedded": {
    "users": [
      {
        "fullUser": "小白鱼 25",
        "_links": {
          "self": {
            "href": "http://127.0.0.1:8080/user/1" },
          "user": {
            "href": "http://127.0.0.1:8080/user/1{?projection}",
            "templated": true }
        }
      },
      {
        "fullUser": "小白鱼 25",
        "_links": {
          "self": {
            "href": "http://127.0.0.1:8080/user/2" },
          "user": {
            "href": "http://127.0.0.1:8080/user/2{?projection}",
            "templated": true }
        }
      }
    ]
  },
  "_links": {
    "self": {
      "href": "http://127.0.0.1:8080/user"
    },
    "profile": {
      "href": "http://127.0.0.1:8080/profile/user"
    }
  },
  "page": {
    "size": 20,
    "totalElements": 2,
    "totalPages": 1,
    "number": 0
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

@Projection定义的数据格式还可以直接配置到Repository之上,就像下面代码中的这样

@RepositoryRestResource(path="user",excerptProjection=ListUser.class)
public interface UserRepository extends JpaRepository<User, Long>{

}
  • 1
  • 2
  • 3
  • 4

配置之后返回的JSON数据会按照ListUser定义的数据格式进行输出 
三、屏蔽自动化方法 
在实际生产环境中,不会轻易的删除用户数据,此时我们不希望DELETE的提交方式生效,可以添加@RestResource注解,并设置exported=false,即可屏蔽Spring Data REST的自动化方法 
比如我们不想轻易的暴露按主键删除的方法,只需要写如下代码

@RepositoryRestResource(path="user",excerptProjection=ListUser.class)
public interface UserRepository extends JpaRepository<User, Long>{

    @RestResource(exported = false)
    @Override
    public void delete(Long id);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值