Expose all IDs when using Spring Data Rest

前端 未结 12 1827
北海茫月
北海茫月 2020-11-30 06:26

I\'d like to expose all IDs using a Spring Rest interface.

I know that per default an ID like this will not be exposed via the rest interface:

    @I         


        
12条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-30 07:16

    I discovered that if you name the @Id field 'Id' it will display in the JSON if you have a public getter for the Id. The Id will show up as a JSON key called 'id'

    For example: @Id @Column(name="PERSON_ROLE_ID") private Long Id;

    This also works for @EmbeddedId fields called 'Id' as well as long as it has a public getter. In this case the fields of the Id will show up as a JSON object.

    For example: @EmbeddedId private PrimaryKey Id;

    Surprisingly this is case sensitive, calling id 'id' doesn't work even though it would be a more conventional name for a Java field.

    I should say that I discovered this completely by accident so I don't know if this is an accepted convention or will work with previous or future versions of Spring Data and REST. Therefore I have included the relevant parts of my maven pom just incase it's sensittive to versions...

    
        org.springframework.boot
        spring-boot-starter-parent
        1.4.0.RELEASE
         
    
    
    
        UTF-8
        UTF-8
        1.8
    
    
    
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        
    
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.boot
            spring-boot-starter-data-rest
        
        
            com.oracle
            ojdbc7
            12.1.0.2
        
        
            com.h2database
            h2
        
    
    

提交回复
热议问题