We are not creating any controller or endpoint for accessing the resource, by using spring data rest based on entity we do CRUD operation without creating controller or endpoint.
DB value on start up
ID | NAME | |
---|---|---|
1 | [email protected] | Praveen |
2 | [email protected] | Ram |
3 | [email protected] | Dileep |
4 | [email protected] | Anil |
After starting the application go to http://localhost:8081/
{
"_links" : {
"users" : {
"href" : "http://localhost:8081/users{?page,size,sort}",
"templated" : true
},
"profile" : {
"href" : "http://localhost:8081/profile"
}
}
}
@RepositoryRestResource(collectionResourceRel = "users", path = "users")
public interface UserRepository extends PagingAndSortingRepository<ApplicationUser, Integer> {
}
@RepositoryRestResource specifies the path for auto generated endpoint. Without this endpoint generated will be same as entity class name like below.
{
"_links" : {
"applicationUsers" : {
"href" : "http://localhost:8081/applicationUsers{?page,size,sort}",
"templated" : true
},
"profile" : {
"href" : "http://localhost:8081/profile"
}
}
}
GET http://localhost:8081/users
{
"_embedded" : {
"users" : [ {
"name" : "Praveen",
"email" : "[email protected]",
"_links" : {
"self" : {
"href" : "http://localhost:8081/users/1"
},
"applicationUser" : {
"href" : "http://localhost:8081/users/1"
}
}
}, {
"name" : "Ram",
"email" : "[email protected]",
"_links" : {
"self" : {
"href" : "http://localhost:8081/users/2"
},
"applicationUser" : {
"href" : "http://localhost:8081/users/2"
}
}
}, {
"name" : "Dileep",
"email" : "[email protected]",
"_links" : {
"self" : {
"href" : "http://localhost:8081/users/3"
},
"applicationUser" : {
"href" : "http://localhost:8081/users/3"
}
}
}, {
"name" : "Anil",
"email" : "[email protected]",
"_links" : {
"self" : {
"href" : "http://localhost:8081/users/4"
},
"applicationUser" : {
"href" : "http://localhost:8081/users/4"
}
}
} ]
},
"_links" : {
"self" : {
"href" : "http://localhost:8081/users"
},
"profile" : {
"href" : "http://localhost:8081/profile/users"
}
},
"page" : {
"size" : 20,
"totalElements" : 4,
"totalPages" : 1,
"number" : 0
}
}
GET http://localhost:8081/users/2
{
"name" : "Ram",
"email" : "[email protected]",
"_links" : {
"self" : {
"href" : "http://localhost:8081/users/2"
},
"applicationUser" : {
"href" : "http://localhost:8081/users/2"
}
}
}
POST http://localhost:8081/users
{
"id": "5",
"name": "Emy",
"email": "[email protected]"
}
ID | NAME | |
---|---|---|
1 | [email protected] | Praveen |
2 | [email protected] | Ram |
3 | [email protected] | Dileep |
4 | [email protected] | Anil |
5 | [email protected] | Emy |
PUT http://localhost:8081/users/1
{
"id": "4",
"name": "Anil",
"email": "[email protected]"
}
ID | NAME | |
---|---|---|
1 | [email protected] | Praveen |
2 | [email protected] | Ram |
3 | [email protected] | Dileep |
4 | [email protected] | Anil |
5 | [email protected] | Emy |
DELETE http://localhost:8081/users/5
ID | NAME | |
---|---|---|
1 | [email protected] | Praveen |
2 | [email protected] | Ram |
3 | [email protected] | Dileep |
4 | [email protected] | Anil |
GET http://localhost:8081/users?page=0&size=2
{
"_embedded" : {
"users" : [ {
"name" : "Praveen",
"email" : "[email protected]",
"_links" : {
"self" : {
"href" : "http://localhost:8081/users/1"
},
"applicationUser" : {
"href" : "http://localhost:8081/users/1"
}
}
}, {
"name" : "Ram",
"email" : "[email protected]",
"_links" : {
"self" : {
"href" : "http://localhost:8081/users/2"
},
"applicationUser" : {
"href" : "http://localhost:8081/users/2"
}
}
} ]
},
"_links" : {
"first" : {
"href" : "http://localhost:8081/users?page=0&size=2"
},
"self" : {
"href" : "http://localhost:8081/users?page=0&size=2"
},
"next" : {
"href" : "http://localhost:8081/users?page=1&size=2"
},
"last" : {
"href" : "http://localhost:8081/users?page=1&size=2"
},
"profile" : {
"href" : "http://localhost:8081/profile/users"
}
},
"page" : {
"size" : 2,
"totalElements" : 4,
"totalPages" : 2,
"number" : 0
}
}
GET http://localhost:8081/users?page=2&size=1
{
"_embedded" : {
"users" : [ {
"name" : "Dileep",
"email" : "[email protected]",
"_links" : {
"self" : {
"href" : "http://localhost:8081/users/3"
},
"applicationUser" : {
"href" : "http://localhost:8081/users/3"
}
}
} ]
},
"_links" : {
"first" : {
"href" : "http://localhost:8081/users?page=0&size=1"
},
"prev" : {
"href" : "http://localhost:8081/users?page=1&size=1"
},
"self" : {
"href" : "http://localhost:8081/users?page=2&size=1"
},
"next" : {
"href" : "http://localhost:8081/users?page=3&size=1"
},
"last" : {
"href" : "http://localhost:8081/users?page=3&size=1"
},
"profile" : {
"href" : "http://localhost:8081/profile/users"
}
},
"page" : {
"size" : 1,
"totalElements" : 4,
"totalPages" : 4,
"number" : 2
}
}
We can add our own method to get by value field.
ApplicationUser findByEmail(@Param("email") String email);
GET http://localhost:8081/users/search/[email protected]
{
"name" : "Praveen",
"email" : "[email protected]",
"_links" : {
"self" : {
"href" : "http://localhost:8081/users/1"
},
"applicationUser" : {
"href" : "http://localhost:8081/users/1"
}
}
}