Skip to content

Instantly share code, notes, and snippets.

@Sam-Kruglov
Sam-Kruglov / UserController.java
Last active January 11, 2018 10:14
DTO Alter "getUsername" to have links
@RepositoryRestController
public class UserController {
private final RepositoryEntityLinks links;
private final UserRepository userRepository;
// all arg constructor
@GetMapping("users/search/username")
public ResponseEntity<Resource<UserUsername>> addLinksToUserUsername(@RequestParam("id") Long id){
@Sam-Kruglov
Sam-Kruglov / REST_response.json
Last active January 11, 2018 04:10
DTO GET localhost:8080/users/search
{
"_links": {
"getUsername": {
"href": "http://localhost:8080/users/search/username{?id}",
"templated": true
},
"self": {
"href": "http://localhost:8080/users/search"
}
}
@Sam-Kruglov
Sam-Kruglov / REST_response.json
Created January 10, 2018 16:17
GET localhost:8080/users/1
{
"username": "jsmith",
"firstName": "John",
"lastName": "Smith",
"_links": {
"self": {
"href": "http://localhost:8080/users/1"
},
"user": {
"href": "http://localhost:8080/users/1"
@Sam-Kruglov
Sam-Kruglov / REST_response.json
Last active January 11, 2018 05:12
DTO GET localhost:8080/users/search/username?id=1
{
"username": "jsmith"
}
@Sam-Kruglov
Sam-Kruglov / UserRepository.java
Last active January 11, 2018 10:13
Integrate the DTO to Spring Data
public interface UserRepository extends CrudRepository<User, Long> {
@RestResource(path = "username", rel = "getUsername")
UserUsername findUsernameById(@Param("id") Long id);
}
@Sam-Kruglov
Sam-Kruglov / UserUsername.java
Last active January 11, 2018 10:26
DTO for User
/**
* Only defines a {@link com.samkruglov.entities.User#username username}.
*/
public class UserUsername {
private final String username; // getter & consctuctor
// equals & hashcode
}
@Sam-Kruglov
Sam-Kruglov / REST_response.json
Last active January 11, 2018 04:13
Initial Response for GET localhost:8080/groups/1
{
"name": "A",
"_links": {
"self": {
"href": "http://localhost:8080/groups/1"
},
"group": {
"href": "http://localhost:8080/groups/1"
},
"users": {
@Sam-Kruglov
Sam-Kruglov / REST_response.json
Last active January 11, 2018 04:13
Initial Response for GET localhost:8080
{
"_links" : {
"groups" : {
"href" : "http://localhost:8080/groups"
},
"users" : {
"href" : "http://localhost:8080/users"
},
"profile" : {
"href" : "http://localhost:8080/profile"
@Sam-Kruglov
Sam-Kruglov / GroupRepository.java
Created January 9, 2018 11:45
Group repository
public interface GroupRepository extends CrudRepository<Group, Long> {}
@Sam-Kruglov
Sam-Kruglov / UserRepository.java
Created January 9, 2018 11:44
User repository
public interface UserRepository extends CrudRepository<User, Long> {}