Issue
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
Solution
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)
Answered By - Taimur
Answer Checked By - Marie Seifert (JavaFixing Admin)