0

I want to sort items by ID and by "Role" like that:

Spring thinks that i want to refer to the Long type of ID when I'm referring to "Roles" and colapses logically. I tried a few options related to similar errors but none works for me... Here is the code:

//My repository public interface EmployeeRepository extends JpaRepository<Employee, Long> { Optional<Employee> findById(Long id); List<Employee> findByRole(RoleList role); } 
public class Employee { private @Id @GeneratedValue Long id; private String name; private RoleList role; Employee() { } public Employee(String name, RoleList role) { this.name = name; this.role = role; } //Getter and setters............. @Override public String toString() { return "Employee [id = " + getId() + ", name = " + getName() + ", role = " + getRole().getNameRole() + ", with salary of = " + getRole().getSalaryRole() + "]"; } } 
public enum RoleList { BURGLAR("burglar", 1500), THIEF("thief", 2000), MAGE("mage", 3500); private String role; private int salaryRole; private RoleList(String role, int salaryRole) { this.role = role; this.salaryRole = salaryRole; } public int getSalaryRole() { return salaryRole; } public String getNameRole() { return role; } 
//initial database class LoadDatabase { @Bean CommandLineRunner initDatabase(EmployeeRepository repository) { return args -> { log.info("Preloading " + repository.save(new Employee("Bilbo Baggins", RoleList.BURGLAR))); log.info("Preloading " + repository.save(new Employee("Frodo Baggins", RoleList.THIEF))); log.info("Preloading " + repository.save(new Employee("Gandalf the Grey", RoleList.MAGE))); }; } } 
// Single item by id @GetMapping("/employees/{id}") Employee one(@PathVariable Long id) { return repository.findById(id).orElseThrow(() -> new EmployeeNotFoundException(id)); } 

//... This works as expected... Then I have few options, none of this works...

// Items by role @GetMapping("employees/role/{role}") List<Employee> getRoles(@PathVariable (value="role")String role) { List<Employee> listRolesEmployees = repository.findByRole(RoleList.valueOf(role)); return listRolesEmployees; } 

//...or...

@RequestMapping(value = "employee/{role}", method = RequestMethod.GET) List<Employee> getRoles(@PathVariable String role) { List<Employee> listRolesEmployees = repository.findByRole(RoleList.valueOf(role)); return listRolesEmployees; } 

//...or...

 @RequestMapping(method = RequestMethod.GET) public List<Employee> getRoles(@RequestParam(value="role") String role) { List<Employee> listRolesEmployees = repository.findByRole(RoleList.valueOf(role)); return listRolesEmployees; } 

Sorry for my english and thanks!

2
  • What error are you getting? Compile-time or runtime? Can you please list is here. Commented Feb 20, 2020 at 19:37
  • This is not a minimal example; you should try the lookup based on a fixed inline enum value to see whether you can make it work. Note also that Spring has extensive conversion capabilities, so you can use just @PathVariable RoleList role or even @PathVariable("id") Employee employee. Commented Feb 20, 2020 at 19:48

3 Answers 3

4

In your endpoint, you use:

@RequestMapping(value = "employee/{role}", method = RequestMethod.GET) 

but in the request http://localhost:8080/employees/BURGLAR This is not the same path (extra character s).

You fall into wrong endpoint:

@GetMapping("/employees/{id}") Employee one(@PathVariable Long id) { 

but BURGLAR is not a Long. This is results in conversion error.

Sign up to request clarification or add additional context in comments.

Comments

0

Finally i got it! I used the GetMapping option for http://localhost:8080/employees/role/{role} and http://localhost:8080/employees/id/{id} for the id and it works! Thanks y'all!

Comments

-1

maybe you need to add a custom method to search by the name, cuz the enums are longs, and search by the enum position is other history.

idk your java version to use lamda, so try add to your enum :

 public static RoleList getByRole(String role){ for(RoleList r: RoleList.values()){ if(r.role.equals(role)) return r; } return null; } 

after this, add to your controller :

List<Employee> listRolesEmployees = repository.findByRole(RoleList.getByRole(role)); 

and try to call :

http://localhost:8080/employees/burglar

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.