【问题标题】:Spring Data Rest id's or link hrefSpring Data Rest id 或链接 href
【发布时间】:2017-03-03 17:55:45
【问题描述】:

我正在使用 Spring 数据休息,我面临以下问题:

我有一个实体

@Entity
public class Role {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long roleId;

    @Column(unique = true, nullable = false)
    private String name;

}

我正在使用 RepositoryRestResource:

@RepositoryRestResource
public interface RoleRepository extends CrudRepository<Role, Long> {

    Role findByRoleId(@Param("roleId") final Long roleId);

    Role findByName(@Param("roleName") final String roleName);
}

当我通过浏览器 (http://localhost:9000/roles/1) 拨打电话时,我看到了这个

{
name: "ROLE_SOMETHING",
_links: {
self: {
href: "http://localhost:9000/roles/1"
},
role: {
href: "http://localhost:9000/roles/1"
},
users: {
href: "http://localhost:9000/roles/1/users"
}
}
}

客户端使用

   ResponseEntity<RoleView> forEntity = restTemplate.getForEntity("http://localhost:9000/roles/1", RoleView.class);
   RoleView body = forEntity.getBody();
   System.out.println(body.getName());

RoleView 是 Role 实体的表示,但在调用方客户端。

现在 json 响应中不存在 roleId。我不知道为什么,因为我没有使用默认名称 id。

我希望 RoleView 对象以某种方式具有 id。通过 roleId 或者通过链接 _self -> href。

我更喜欢链接 _self 方式,因为这似乎更接近 json 响应而不是暴露 ID。

有人能指出正确的方向吗?

【问题讨论】:

    标签: json spring-data pojo spring-rest


    【解决方案1】:

    现在我正在寻找解决方案:

    @Configuration
    public class MyConfiguration extends RepositoryRestConfigurerAdapter {
    
        @Override
        public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
            config.exposeIdsFor(Role.class);
        }
    }
    

    虽然我期待其他类型的解决方案

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-14
      • 2016-05-23
      • 1970-01-01
      • 1970-01-01
      • 2016-12-07
      • 1970-01-01
      • 2017-03-18
      • 2015-03-14
      相关资源
      最近更新 更多