【问题标题】:Spring data rest link nameSpring数据休息链接名称
【发布时间】:2014-11-26 05:21:46
【问题描述】:

我有一个与其他实体有多个双向多对一关联的类 Category-

public class Category implements Serializable {

    @Id
    @Column(name = "CATEGORY_ID", unique = true, nullable = false)
    @TableGenerator(name = Category.TABLE_NAME, table = "LMC_GENERATED_KEYS", pkColumnName = "ID", valueColumnName = "LAST_VALUE", pkColumnValue = Category.TABLE_NAME, allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.TABLE, generator = Category.TABLE_NAME)
    private Long categoryId;


    // bi-directional many-to-one association to LmcCategoryImage
    @OneToOne(mappedBy = "categoryImage")
    @JsonManagedReference
    private CategoryImage categoryImage;

    // bi-directional many-to-one association to LmcCategoryProductXref
    @OneToMany(mappedBy = "categoryProductXref")
    @JsonManagedReference
    private Set<CategoryProductXref> categoryProductXrefs;

    // bi-directional many-to-one association to LmcCategoryXref
    @OneToMany(mappedBy = "categoryxref", fetch = FetchType.LAZY)
    @JsonManagedReference
    private Set<CategoryXref> categoryxrefs;

}

这已通过以下存储库公开为存储库。

public interface CategoryRepository extends PagingAndSortingRepository<Category, Long> {

}

此存储库生成以下 json-

{
  "_links": {
    "self": {
      "href": "http://localhost:8080/lmc-persistence/jpa/categories{?page,size,sort}",
      "templated": true
    }
  },
  "_embedded": {
    "categories": [
      {
        "activeEndDate": "2014-11-25T04:40:52.000+0000",
        "activeStartDate": "2014-11-25T04:40:37.000+0000",
        "archived": false,
        "createdBy": "SYSTEM",
        "modifedBy": "SYSTEM",
        "dateCreated": "2014-11-25T04:40:37.000+0000",
        "dateModified": "2014-11-25T04:40:37.000+0000",
        "description": "DESCRIPTION",

      },
      "categoryImage": null,
      "categoryProductXrefs": [

      ],
      "productFeatureds": [

      ],
      "_links": {
        "self": {
          "href": "http://localhost:8080/lmc-persistence/jpa/categories/10001"
        },
        "categoryXrefs": {
          "href": "http://localhost:8080/lmc-persistence/jpa/categories/10001/categoryXrefs"
        },
        "parentCategory": {
          "href": "http://localhost:8080/lmc-persistence/jpa/categories/10001/parentCategory"
        },
        "categoryAttributes": {
          "href": "http://localhost:8080/lmc-persistence/jpa/categories/10001/categoryAttributes"
        },
        "productProductFeatured": {
          "href": "http://localhost:8080/lmc-persistence/jpa/categories/10001/productProductFeatured"
        },
        "categoryProductFeature": {
          "href": "http://localhost:8080/lmc-persistence/jpa/categories/10001/categoryProductFeature"
        },
        "categoryImage": {
          "href": "http://localhost:8080/lmc-persistence/jpa/categories/10001/categoryImage"
        },
        "categoryProductXref": {
          "href": "http://localhost:8080/lmc-persistence/jpa/categories/10001/categoryProductXref"
        }
      }
    }
  ]
},
"page": {
  "size": 20,
  "totalElements": 1,
  "totalPages": 1,
  "number": 0
}

在这个 json 中,所有 _link 都是自动创建的,因为我已经在这个实体中通过关系映射。

这些 url 已根据给定的属性名称生成为驼峰式大小写。有什么办法,我可以覆盖这些名称并给我自己的自定义名称,如 categoryImage 作为 categoryimg 和 categoryProductXref 作为 categoryproductxref

【问题讨论】:

    标签: rest spring-data-rest


    【解决方案1】:

    在我的基础资源实体类中添加 @RestResource 注释对我来说是成功的:

    @RestResource(rel = "usertype", path = "usertypes")
        private List<Usertype> userTypes;
    

    所以:

    "_links": {
    "self": {
      "href": "http://localhost:8080/api/users/1"
    },
    "userTypes": {
      "href": "http://localhost:8080/api/users/1/userTypes"
    }
    

    }

    变成:

    "_links": {
    "self": {
      "href": "http://localhost:8080/api/users/1"
    },
    "usertype": {
      "href": "http://localhost:8080/api/users/1/usertypes"
    }
    

    }

    【讨论】:

      【解决方案2】:

      (没有足够的声誉来添加评论)。你试过了吗

      @OneToOne(mappedBy = "fkCategoryImage" )
      @JsonManagedReference
      @RestResource(rel="mycategoryimage")
      private CategoryImage categoryImage;
      

      所以省略 RestResource 的“路径”部分。这成功地为我命名了 _link 并且 URL 是可导航的

      【讨论】:

        【解决方案3】:

        你试过吗?我在猜测,但我认为更改属性也可能导致链接被重命名

        // bi-directional many-to-one association to LmcCategoryProductXref
        @OneToMany(mappedBy = "categoryProductXref")
        @JsonManagedReference
        @JsonPropertyName("categoryproductxrefs")
        private Set<CategoryProductXref> categoryProductXrefs;
        

        【讨论】:

        • @JsonPropertyName 在类路径中不可用,因为我使用的是使用 jackson-annotations 的 spring data rest。我试过@JsonProperty,但也没有用。
        【解决方案4】:

        我看到我可以使用@RestResource(path="mycategoryimage" ,rel="mycategoryimage" )

        // bi-directional many-to-one association to LmcCategoryImage
            @OneToOne(mappedBy = "fkCategoryImage" )
            @JsonManagedReference
            **@RestResource(path="mycategoryimage" ,rel="mycategoryimage" )**
            private CategoryImage categoryImage;
        

        它会生成我想要的 url。

        "mycategoryimage": {
        "href": "http://localhost:8080/lmc-persistence/jpa/categories/10001/mycategoryimage"
        },
        

        但此 URL 无法显示空白页。如果我删除 @RestResource 并创建一个代表 CategoryImage 实体的存储库,如下所示。

         @RepositoryRestResource(path = "/mycategoryimage")
            public interface CategoryImageRepository extends PagingAndSortingRepository<CategoryImage, Long> {
        
            }
        

        Spring data rest 生成一个 Url 并且可以完美地工作,但它仍然是驼峰式的。

        "categoryImage": {
        "href": "http://localhost:8080/letmecall-persistence/jpa/categories/10001/categoryImage"
        }
        

        【讨论】:

          猜你喜欢
          • 2018-09-15
          • 2017-02-03
          • 2016-03-23
          • 2015-10-21
          • 1970-01-01
          • 1970-01-01
          • 2018-06-03
          • 1970-01-01
          • 2016-08-06
          相关资源
          最近更新 更多