This is continuation
to my previous post, you can download previous application from this link.
In
previous application, we exposed REST endpoints to 'EmployeeRepository'
@RepositoryRestResource(path="myemployees")
public interface EmployeeRepository extends CrudRepository<Employee, Integer> {
// SELECT * FROM employee WHERE salary=X
public List<Employee> findBySalary(double salary);
// SELECT * FROM employee WHERE lastName=X
public List<Employee> findByLastName(String lastName);
// SELECT * FROM employee WHERE age>X
public List<Employee> findByAgeGreaterThan(int age);
// SELECT * FROM employee WHERE age=X AND salary=Y
public List<Employee> findByAgeAndSalary(int age, double salary);
}
When you
hit 'http://localhost:8080/api/myemployees' API, it generates below kind of
response.
{
"_embedded" : {
"employees" : [ {
"firstName" : "Ram",
"lastName" : "Gurram",
"age" : 32,
"salary" : 100000.23,
"_links" : {
"self" : {
"href" : "http://localhost:8080/api/myemployees/1"
},
"employee" : {
"href" : "http://localhost:8080/api/myemployees/1"
}
}
}, {
"firstName" : "Joel",
"lastName" : "Chelli",
"age" : 43,
"salary" : 60000.0,
"_links" : {
"self" : {
"href" : "http://localhost:8080/api/myemployees/2"
},
"employee" : {
"href" : "http://localhost:8080/api/myemployees/2"
}
}
}, {
"firstName" : "Gopi",
"lastName" : "Battu",
"age" : 45,
"salary" : 1000000.0,
"_links" : {
"self" : {
"href" : "http://localhost:8080/api/myemployees/3"
},
"employee" : {
"href" : "http://localhost:8080/api/myemployees/3"
}
}
}, {
"firstName" : "Bomma",
"lastName" : "Srikanth",
"age" : 39,
"salary" : 60000.0,
"_links" : {
"self" : {
"href" : "http://localhost:8080/api/myemployees/4"
},
"employee" : {
"href" : "http://localhost:8080/api/myemployees/4"
}
}
}, {
"firstName" : "Surendra",
"lastName" : "Sami",
"age" : 32,
"salary" : 100000.23,
"_links" : {
"self" : {
"href" : "http://localhost:8080/api/myemployees/5"
},
"employee" : {
"href" : "http://localhost:8080/api/myemployees/5"
}
}
} ]
},
"_links" : {
"self" : {
"href" : "http://localhost:8080/api/myemployees"
},
"profile" : {
"href" : "http://localhost:8080/api/profile/myemployees"
},
"search" : {
"href" : "http://localhost:8080/api/myemployees/search"
}
}
}
As you see
the immediate child of '_embedded' property, name 'employees' is given to the
collection. If you want to change the name, you can do that by passing 'collectionResourceRel'
attribute to the @RepositoryRestResource annotation.
@RepositoryRestResource(path="myemployees",
collectionResourceRel="emps")
public
interface EmployeeRepository extends CrudRepository<Employee, Integer> {
....
}
Once you
update EmployeeRepository interface with collectionResourceRel attribute,
collection name will be given as emps.
{
"_embedded" : {
"emps" : [ {
"firstName" : "Ram",
"lastName" : "Gurram",
"age" : 32,
"salary" : 100000.23,
"_links" : {
"self" : {
"href" : "http://localhost:8080/api/myemployees/1"
},
"employee" : {
"href" : "http://localhost:8080/api/myemployees/1"
}
}
}, {
"firstName" : "Joel",
"lastName" : "Chelli",
"age" : 43,
"salary" : 60000.0,
"_links" : {
"self" : {
"href" : "http://localhost:8080/api/myemployees/2"
},
"employee" : {
"href" : "http://localhost:8080/api/myemployees/2"
}
}
}, {
"firstName" : "Gopi",
"lastName" : "Battu",
"age" : 45,
"salary" : 1000000.0,
"_links" : {
"self" : {
"href" : "http://localhost:8080/api/myemployees/3"
},
"employee" : {
"href" : "http://localhost:8080/api/myemployees/3"
}
}
}, {
"firstName" : "Bomma",
"lastName" : "Srikanth",
"age" : 39,
"salary" : 60000.0,
"_links" : {
"self" : {
"href" : "http://localhost:8080/api/myemployees/4"
},
"employee" : {
"href" : "http://localhost:8080/api/myemployees/4"
}
}
}, {
"firstName" : "Surendra",
"lastName" : "Sami",
"age" : 32,
"salary" : 100000.23,
"_links" : {
"self" : {
"href" : "http://localhost:8080/api/myemployees/5"
},
"employee" : {
"href" : "http://localhost:8080/api/myemployees/5"
}
}
} ]
},
"_links" : {
"self" : {
"href" : "http://localhost:8080/api/myemployees"
},
"profile" : {
"href" : "http://localhost:8080/api/profile/myemployees"
},
"search" : {
"href" : "http://localhost:8080/api/myemployees/search"
}
}
}
Total
project structure looks like below.
You can
download complete working application from this link.
No comments:
Post a Comment