2017-03-03 32 views
0

我使用Spring數據休息,我面臨着以下:春數據休息ID或鏈接的href

@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是角色實體的表示,但是,從主叫方客戶端。

現在roleId不存在於json響應中。我不知道爲什麼,因爲我不使用默認名稱ID。

我希望RoleView對象以某種方式擁有該id。通過roleId或者通過鏈接_self - > href。

我寧願鏈接_自己的方式,因爲這似乎更接近JSON響應而不是exposingId的。

有人能指出我正確的方向嗎?

回答

0

現在我打算解決方案:

@Configuration 
public class MyConfiguration extends RepositoryRestConfigurerAdapter { 

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

雖然我期待着其他類型的解決方案