14

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();
}
artle
  • 377
  • 1
  • 3
  • 14

2 Answers2

31

change your mapping to /somepath/{variable:.+}

or add a slash at the end /somepath/{variable}/

Jkike
  • 807
  • 6
  • 12
  • 1
    I tried but i had an exception "'The inline constraint resolver of type 'DefaultInlineConstraintResolver' was unable to resolve the following inline constraint: '.+'.' " – Başar Kaya Sep 15 '18 at 11:40
2

As alternativ to @Jkikes answer you can generally turn this behavior of with:

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

  @Override
  public void configurePathMatch(PathMatchConfigurer configurer) {
    configurer.setUseSuffixPatternMatch(false);
  }

}

Now you can use dots everywhere :D

d0x
  • 11,040
  • 17
  • 69
  • 104