如何在春季从 rest url 获取包含 dot(.) 的参数

2022-09-03 17:41:52

我正在使用Spring REST和休眠创建一个Web应用程序。在这里,我使用来自url的唯一用户名从数据库获取记录。但问题是,如果我写的是简单的字符串,那么它工作正常,但是当在用户名中,我写的是dot(.)时,那么没有结果来自数据库。

例如

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

但是当我正在使用这个网址

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();
}

答案 1

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

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


答案 2

作为@Jkikes答案的交替,您通常可以使用以下命令来转换此行为:

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

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

}

现在,您可以在任何地方使用点:D