I have a class Category which has multiple bi-directional many-to-one association with other entities-
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;
}
This has been exposed as repository via below repository.
public interface CategoryRepository extends PagingAndSortingRepository<Category, Long> {
}
This repository generates following 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
}
In this json all _link have been created automatically because I have mapped by relation ship in this entity.
These urls have been generated as camel case based on given attributes name. Is there any way, I can override these name and give my own customize name like categoryImage as categoryimg and categoryProductXref as categoryproductxref
(Not enough reputation to add a comment). Had you tried just
@OneToOne(mappedBy = "fkCategoryImage" )
@JsonManagedReference
@RestResource(rel="mycategoryimage")
private CategoryImage categoryImage;
So omit the 'path' part of the RestResource. That successfully names the _link for me and the URL is navigable
Putting @RestResource annotation in my base resource entity class did the trick for me:
@RestResource(rel = "usertype", path = "usertypes")
private List<Usertype> userTypes;
So:
"_links": {
"self": {
"href": "http://localhost:8080/api/users/1"
},
"userTypes": {
"href": "http://localhost:8080/api/users/1/userTypes"
}
}
Becomes:
"_links": {
"self": {
"href": "http://localhost:8080/api/users/1"
},
"usertype": {
"href": "http://localhost:8080/api/users/1/usertypes"
}
}
Have you tried this? I'm guessing but I reckon that changing the property might also cause the links to be renamed
// bi-directional many-to-one association to LmcCategoryProductXref
@OneToMany(mappedBy = "categoryProductXref")
@JsonManagedReference
@JsonPropertyName("categoryproductxrefs")
private Set<CategoryProductXref> categoryProductXrefs;
I see that I can use @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;
It generates url which I want.
"mycategoryimage": {
"href": "http://localhost:8080/lmc-persistence/jpa/categories/10001/mycategoryimage"
},
But this URL is not working showing blank page. If I remove @RestResource and create a Repository which represent CategoryImage entity like below.
@RepositoryRestResource(path = "/mycategoryimage")
public interface CategoryImageRepository extends PagingAndSortingRepository<CategoryImage, Long> {
}
Spring data rest generate a Url and work perfectly but it is still in camel case.
"categoryImage": {
"href": "http://localhost:8080/letmecall-persistence/jpa/categories/10001/categoryImage"
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.