Spring数据休息链接名称

时间:2014-11-26 05:21:47

标签: rest spring-data-rest

我有一个类别类别,它与其他实体有多个双向多对一关联 -

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都是自动创建的,因为我已经通过这个实体中的关系图进行了映射。

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

4 个答案:

答案 0 :(得分:1)

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

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

因此省略RestResource的'path'部分。这成功地为我命名了_link并且URL是可导航的

答案 1 :(得分: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 :(得分:0)

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

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

答案 3 :(得分:0)

我发现我可以使用@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;

它会生成我想要的网址。

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

但此网址无法显示空白页面。如果我删除@RestResource并创建一个代表CategoryImage实体的Repository,如下所示。

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

    }

Spring数据休息生成一个Url并且工作正常,但仍处于驼峰状态。

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