-1

I am building a simple get rest call from MySQL database, the problem is that it returns an empty object.

The call itself is takes in an email (I know this is not the best approach), here is my code:

Entity:

@Entity
public class User implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "user_id")
    private int id;
    private String email;
    private String password;
    private String firstName;
    private String userName;
    private String lastName;
    private boolean active;
    @Temporal(TemporalType.DATE)
    private Date createDate;
    @Temporal(TemporalType.DATE)
    private Date updateDate;
    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
    private Collection<Role> roles;

// constructor
// get and setter

}

Repository:

public interface UserRepository extends JpaRepository<User, Long> {
//    User findById (Integer Id);

    @Query("SELECT u.id FROM User u where u.id = :id") 
    User findById(@Param("id") Integer id);

    User findByEmail (String email);
}

Service:

@Service("userService")
public class UserService {

    private String status, message;
    private final HashMap map = new HashMap();

    @Autowired
    private UserRepository userRepository;
//    @Autowired
//    private RoleRepository roleRepository;

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

}

Controller:

@RestController("userControllerService")
@RequestMapping("/user/account")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/test-get/{email}")
    public User jj(@PathVariable("email") String email){
        return userService.findByUserEmail(email);
    }
}

And my database happens to have the following data:

enter image description here

And here is the response I get after hitting the URL

enter image description here

I have no clue why my response is empty!

halfer
  • 19,824
  • 17
  • 99
  • 186
DESH
  • 530
  • 2
  • 11
  • 29
  • Please, update question, so that the request url in postman could be seen – Serg Vasylchak Jun 29 '18 at 09:56
  • I've downvoted for the significant amount of editing work you are creating when you post new questions, including this one. Quote blocks are for quotes, i.e. something that someone else said or wrote elsewhere. They are not for material in your own voice, and not for introductory titles for code/logs/images. – halfer Jun 29 '18 at 18:44
  • It looks like at least two users have repaired this sort of formatting before. Please refrain from using the quote block in this way, except in the cases where your excerpt really is a quote. – halfer Jun 29 '18 at 18:56

1 Answers1

0

You cannot have the @in the URI path. Encode it with %40.

Reference: Can I use an at symbol (@) inside URLs?

Also, right way is to use as a query param as that's more of a good identifier and allows @ as it parses as string

    @GetMapping("/test-get")
    public User jj(@RequestParam("email") String email){
        return userService.findByUserEmail(email);
    }

Either ways, hit as encoded url as /test-get/email=a@b.com ? or /test-get/a%40b.com for your previous code.

Karthik R
  • 5,523
  • 2
  • 18
  • 30