1

I came across different behavior of spring data rest in spring boot application when I query from object repository class,having custom query to retrieve User object by matching email id,and email id is unique.I get null value and the object exist. I am calling Rest URI from postman. --UserRepository

@Repository
@Transactional
public interface UserRepository extends  JpaRepository<User, Long> {    

    @Query(value ="select u from User u where u.email = :email")
    User findByEmail(@Param("email") String email);


}

--UserServiceImpl

@Service("userService")
@Transactional
public class UserServiceImpl implements IUserService{

    @Autowired
    private UserRepository userRepository;


    @Override
    public User findByEmail(String email) {
        return userRepository.findByEmail(email);
    }
}

--Controller

@RestController
@RequestMapping("/api")
public class UserControllerRest {

@Autowired(required = true)
UserServiceImpl userService;    
@RequestMapping(value = "/user/email/{email}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
            public ResponseEntity<User> getUserByEmail(@PathVariable("email") String email) {
                System.out.println("Fetching User with id " + email);
                User user = userService.findByEmail(email);
                if (user == null) {
                    System.out.println("User with email " + email + " not found");
                    return new ResponseEntity<User>(HttpStatus.NOT_FOUND);
                }
                return new ResponseEntity<User>(user, HttpStatus.OK);
            }

}

Help required in this regard

7
  • 1
    How could we help? You just need to start debugging: is the email you're passing to the query the one you think it is (no trailing or leading white space, etc.)? does the email actually exists in the table? Has the transaction that inserted it been committed? Are you sure the app uses the database or schema you think it's using? Does it work for another existing email? Add traces in the code. Use your debugger. There's little we can do here. Commented Mar 26, 2017 at 9:47
  • Yes email exist in db,I also check with trim ,When I try to get list of objects ,it show me that specific object having email id ,but when i try to retrieve as single object I am unable to get it. Commented Mar 26, 2017 at 10:08
  • I have checked with like condition in sql query and it return data,might be this is issue of leading spaces. @Query(value = "select firstName,lastName from User u where u.email like %:email%") ,but with like condition is not the requirement because it will return list of objects Commented Mar 26, 2017 at 10:41
  • Where should I add check of leading spaces to be ignored ,I tried to trim param string of email but it through exception while processing user = userService.findByEmail(email.toString().trim() with Exception of :"java.lang.IllegalArgumentException: Name for parameter binding must not be null or empty! For named parameters you need to use @Param for query method parameters on Java versions < 8. Commented Mar 26, 2017 at 10:46
  • 1
    Just to be clear : Does everything work fine if you just change the method signature to List<User> findByEmail(@Param("email") String email). If not, did you tried it with different email addresses after they are trimmed. Commented Mar 26, 2017 at 11:22

2 Answers 2

1

Please, add more details. Add project to GitHub or something. How your model of User looks like? You inject service implementation instead of IUserService, maybe this is the problem.

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

1 Comment

I have fixed service injection,by replacing service implementation with IUserService
1

after alot of debugging I finally reached upon this observation that the email id was not able to reach controller mapped method and for [email protected] ,it was read as test@gmail and period and com value were removed out ,the work around was to change URI mapping and add / at the end of parameter so the updated URI is now
@RequestMapping(value = "/user/email/{email}/", produces = MediaType.APPLICATION_JSON_VALUE)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.