Announcement Announcement Module
Collapse
No announcement yet.
How to override the paths under '_links'? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to override the paths under '_links'?

    I've created 2 objects 'Setting' and 'SettingType'. 'Setting' has ManyToOne relationship with 'SettingType'.
    I've also created RestRepository classes for each of above objects such as below:

    Code:
    @RepositoryRestResource(collectionResourceRel = "setting", path = "setting")
    public interface SettingRepository extends PagingAndSortingRepository<Setting, Long> {
    
    }
    
    @RepositoryRestResource(collectionResourceRel = "setting_type", path = "setting_type")
    public interface SettingTypeRepository extends PagingAndSortingRepository<SettingType, Long> {
    
    }
    When I call GET request of Setting, under the '_links', it has relationship with SettingType. How can I change the SettingType path from 'http://localhost:8080/setting/1/segmentType' to 'http://localhost:8080/setting/1/segment_type' (lower case with underscore).

    Code:
    > curl http://localhost:8080/setting
    
    {
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/setting{?page,size,sort}",
          "templated" : true
        }
      },
      "_embedded" : {
        "setting" : [ {
          "name" : "Application Name",
          "description" : "Name of the application.",
          "value" : null,
          "_links" : {
            "self" : {
              "href" : "http://localhost:8080/setting/1"
            },
            "settingType" : {
              "href" : "http://localhost:8080/setting/1/settingType"
            }
          }
        } ]
      },
      "page" : {
        "size" : 20,
        "totalElements" : 1,
        "totalPages" : 1,
        "number" : 0
      }
    }
    Appreciate for any help. Thank you.

    Regards,
    Taufek

  • #2
    Found the answer. I need to annotate my Setting class as below:

    Code:
    @Entity
    public class Setting {
    
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private long id;
    
        private String name;
        private String description;
        private String value;
    
        @ManyToOne
        @JoinColumn(name="type_id")
        @RestResource(path="setting_type")
        private SettingType settingType;
    WIth these I will get the following:
    Code:
    curl http://localhost:8080/setting
    {
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/setting{?page,size,sort}",
          "templated" : true
        }
      },
      "_embedded" : {
        "setting" : [ {
          "name" : "Application Name",
          "description" : "Name of the application.",
          "value" : null,
          "_links" : {
            "self" : {
              "href" : "http://localhost:8080/setting/1"
            },
            "settingType" : {
              "href" : "http://localhost:8080/setting/1/setting_type"
            }
          }
        } ]
      },
      "page" : {
        "size" : 20,
        "totalElements" : 1,
        "totalPages" : 1,
        "number" : 0
      }
    }

    Comment

    Working...
    X