如何从rest url中获取包含点(.)的参数



我正在使用Spring REST和hibernate创建一个web应用程序。在这里,我从数据库中抓取记录使用唯一的用户名,这是来自url。但问题是,如果我写简单的字符串,那么它工作得很好,但当在用户名我写点(.),然后没有结果来自数据库。

对前女友

http://localhost:8080/WhoToSubscribe/subscribe/anshul007

但是当我使用这个url

http://localhost:8080/WhoToSubscribe/subscribe/nadeem.ahmad095

不工作,因为它包含点(.)

这是我的控制器

@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;
}
这是我的DAO
@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();
}

将您的映射更改为/somepath/{variable:.+}

或在末尾添加斜杠/somepath/{variable}/

作为@Jkikes答案的替代选项,您通常可以使用:

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
  @Override
  public void configurePathMatch(PathMatchConfigurer configurer) {
    configurer.setUseSuffixPatternMatch(false);
  }
}
D

最新更新