Springboot - empty get rest response
Springboot - empty get rest response
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:
And here is the response I get after hitting the URL
I have no clue why my response is empty!
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 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 at 18:56
1 Answer
1
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.
I recieve an "error": "Not Found"...
– 666DESH666
Jun 29 at 10:17
for which one. Can you try it as request param. It works as I just tested it
– Karthik R
Jun 29 at 10:45
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Please, update question, so that the request url in postman could be seen
– Serg Vasylchak
Jun 29 at 9:56