1

Hello everybody i am new to the hibernate. I have a method that is purposed to get a single column.

@Override public List<User> getUsersByEmail(String email) { session = sessionFact.openSession(); Query query = session.getNamedQuery("User.findByEmail"); query.setParameter("email", email); return query.list(); } 

After that, the returned value is used by the controller that further returns JSON data. It is supposed to return a user data but returns nothing.

@RequestMapping(value = "/api/customer/customer-id/{email}", method = RequestMethod.GET) public @ResponseBody List<User> getUserId(@PathVariable String email) { return customerDaoImp.getUsersByEmail(email); } 

Entity Class User.java

 @Entity @Table(name = "tbl_user", catalog = "lifestyle", schema = "") @NamedQueries({ @NamedQuery(name = "User.findAll", query = "SELECT u FROM User u") , @NamedQuery(name = "User.findByUserId", query = "SELECT u FROM User u WHERE u.userId = :userId") , @NamedQuery(name = "User.findByFullName", query = "SELECT u FROM User u WHERE u.fullName = :fullName") , @NamedQuery(name = "User.findByAddress", query = "SELECT u FROM User u WHERE u.address = :address") , @NamedQuery(name = "User.findByContact", query = "SELECT u FROM User u WHERE u.contact = :contact") , @NamedQuery(name = "User.findByGender", query = "SELECT u FROM User u WHERE u.gender = :gender") , @NamedQuery(name = "User.findByDob", query = "SELECT u FROM User u WHERE u.dob = :dob") , @NamedQuery(name = "User.findByEmail", query = "SELECT u FROM User u WHERE u.email = :email") , @NamedQuery(name = "User.findByPassword", query = "SELECT u FROM User u WHERE u.password = :password") , @NamedQuery(name = "User.findByActive", query = "SELECT u FROM User u WHERE u.active = :active") , @NamedQuery(name = "User.findByCreatedDate", query = "SELECT u FROM User u WHERE u.createdDate = :createdDate")}) public class User implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "user_id") private Integer userId; @NotNull @Size(min = 1, max = 256) @Column(name = "full_name") private String fullName; @Size(max = 256) @Column(name = "address") private String address; @Size(max = 30) @Column(name = "contact") private String contact; @Size(max = 10) @Column(name = "gender") private String gender; @Column(name = "dob") @Temporal(TemporalType.DATE) private Date dob; // @Pattern(regexp="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", message="Invalid email")//if the field contains email address consider using this annotation to enforce field validation @NotNull @Size(min = 1, max = 256) @Column(name = "email") private String email; @NotNull @Size(min = 1, max = 256) @Column(name = "password") private String password; @NotNull @Column(name = "active", insertable = false) private short active; @Column(name = "created_date", insertable = false) @Temporal(TemporalType.TIMESTAMP) private Date createdDate; public User() { } public User(Integer userId) { this.userId = userId; } public User(Integer userId, String fullName, String email, String password, short active) { this.userId = userId; this.fullName = fullName; this.email = email; this.password = password; this.active = active; } //getter and setter 

http://localhost:8080/LifeStyle/api/customer/customer-id/[email protected]

This is the url i have entered to get a value

1
  • Can't you used post method instead ? Commented Dec 28, 2017 at 9:23

2 Answers 2

3

The email must be URL encoded

http://localhost:8080/LifeStyle/api/customer/customer-id/nishandhungana41%40hotmail.com 

So the @ char to be recognized

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

5 Comments

it still doesn't return anything
Check what you really get in the controller.
@StanishlavL - If i manually pass the email it returns a set of data but doesn't from url
Again. Print the @PathVariable String email in the controller to see real value obtained
it printed as in the url
1

I think that does not recognize .

Try change to this:

@RequestMapping(value = "/api/customer/customer-id/{email:.+}", method = RequestMethod.GET) public @ResponseBody List<User> getUserId(@PathVariable String email) { 

3 Comments

Missing URI template variable &#39;email&#39; for method parameter of type String
Thank you sir for your answer. Can you tell me the purpose of adding :.+ Now it returns me the data
please see this

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.