Long-time reader, first-time poster.
I need help. I can't figure out URL parameters ↔ DataBase.
Situation: I put .../users/?role=admin&title=manager into postman
Expected: a json of all the users who are managers and admins.
Actual: My computer blows up.
@RestController
@RequestMapping(path = USERS_PATH)
@Log4j2
public class UserController
// other code...
@GetMapping
public ResponseEntity<List<User>> getUserQuery(
@RequestParam( required = false, name = "name") String name,
@RequestParam( required = false, name = "title") String title,
@RequestParam( required = false, name = "roles") String roles,
@RequestParam( required = false, name = "email") String email,
@RequestParam( required = false, name = "password") String password
) {
log.info("Request received for getUserQuery");
return new ResponseEntity<>(userService.doSomething???(), HttpStatus.OK); // stuff I don't understand yet)
}
My Question: After the controller, what goes into the UserService and UserRepository?
Extra info: I'm using Spring Boot and H2 but may need to swap to PostgreSQL DB later.
I have the code below as a check for unique emails during puts and posts on the service layer, but I couldn't get something similar to work for this issue.
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
@Query("SELECT u FROM User u WHERE u.email = ?1")
Optional<User> findUserByEmail(String email);
}
Rant: This seems like a super common thing, and I should already know how to do this. I don't. I've been reading the internet for like 5 hours. Still no answer. I learned stuff about Specifications, Querydsl, @ModelAtribute, DAO, and mapping RequestParams. However, I couldn't find how to connect the dots. So, I went full caveman and tried making an @Query parameter using StringBuilder and a bunch of logic...I'm starting to spiral.
Anyway, I would like to avoid specifications & Querydsl. Just use @Query and JPA, but more importantly, I want a clean solution/ best practice.