spring-projects / spring-projects/spring-data-rest
Allow overriding which field gets exposed in the URI
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 958
- Forks
- 568
- PR merge metrics
- No merged PRs in 30d
Description
From what I understand, it is the @Id field in the entity that always gets exposed as the URI.
This is very inconvenient. Usually every object has a low-level DB Id (i.e. the PK) and a business-friendly id (e.g. customerId)
It's a much nicer URL to do
GET /customer/CUST1234 (a customer ID of 'CUST1234')
vs
GET /customer/23434224 (the DB PK of 2343224)
Using the low-level @Id as the URI has 2 major showstopper issues:
a) it is hard to move config data from one environment to another (the PKs in a test env will be totally different from a production one for the same logical entity).
b) it is impossible to write consistent unit tests for the returned JSON, since the "links" section will vary on every run (hence you cannot compare an entire expected JSON vs actual JSON)
It would be great to add a new annotation like @RestId so you could explicitly define which of the entity fields get used in the URI, e.g.
@Entity
public class Customer {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@RestId
private String customerId;
....
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
No files, tests, or entry points are named. Start by tracing how entity @Id fields become resource URIs and determine how a business-friendly field could be selected instead. Done means the requested override can expose customerId rather than the generated database id, with the behavior covered by tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring
- Domain
- api, backend
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100