I am creating a web application using Spring REST and hibernate. Here i am fetching record from database using unique username which is coming from url. But the problem is that if i am writing simple string then it is working fine but when in username i am writing dot(.) then no result is coming from database.
For ex.
http://localhost:8080/WhoToSubscribe/subscribe/anshul007
but when i am using this url
http://localhost:8080/WhoToSubscribe/subscribe/nadeem.ahmad095
it is not working because it is containing dot(.)
Here is my controller
@RequestMapping(value = "/{uname}", method = RequestMethod.GET)
public @ResponseBody
List<Profession> getSubscriber(@PathVariable("uname") String uname) {
List<Profession> pro = null;
try {
pro = subscribeService.getProfessionById(uname);
} catch (Exception e) {
e.printStackTrace();
}
return pro;
}
Here is my DAO class
@SuppressWarnings("unchecked")
public List<Profession> getProfessionById(String uname) throws Exception {
session = sessionFactory.openSession();
session.beginTransaction();
String queryString = "from Profession where username = :uname";
Query query = session.createQuery(queryString);
query.setString("uname", uname);
//List<Profession> queryResult = (List<Profession>) query.uniqueResult();
session.getTransaction().commit();
return query.list();
}
To check if a url has query parameters, call the indexOf() method on the url, passing it a question mark, and check if the result is not equal to -1 , e.g. url. indexOf('? ') !== -1 .
While the query parameters appear on the right side of the '? ' in the URL, path parameters come before the question mark sign. Secondly, the query parameters are used to sort/filter resources. On the other hand, path parameters are used to identify a specific resource or resources.
change your mapping to /somepath/{variable:.+}
or add a slash at the end /somepath/{variable}/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With