我在Spring启动应用程序中遇到了spring数据休息的不同行为 当我从对象存储库类查询时,通过匹配电子邮件ID来检索User对象的自定义查询,并且电子邮件ID是唯一的。我得到空值并且对象存在。 我从邮递员那里调用Rest URI。 --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);
}
}
- 控制器
@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);
}
}
在这方面需要帮助
答案 0 :(得分:1)
请添加更多详细信息。将项目添加到GitHub或其他东西。您的用户模型如何?您注入服务实现而不是IUserService,这可能是问题所在。
答案 1 :(得分:0)