【发布时间】: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