java 如何从spring中的rest url获取包含dot(.)的参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31876942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get parameter which is containing dot(.) from rest url in spring
提问by artle
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.
我正在使用 Spring REST 和 hibernate 创建一个 Web 应用程序。在这里,我使用来自 url 的唯一用户名从数据库中获取记录。但问题是,如果我正在编写简单的字符串,那么它工作正常,但是当我在用户名中编写 dot(.) 时,则没有来自数据库的结果。
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(.)
它不起作用,因为它包含 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 DAOclass
这是我的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();
}
回答by Jkike
change your mapping to /somepath/{variable:.+}
将您的映射更改为 /somepath/{variable:.+}
or add a slash at the end /somepath/{variable}/
或在末尾添加斜线 /somepath/{variable}/
回答by d0x
As alternativ to @Jkikes answer you can generally turn this behavior of with:
作为@Jkikes 回答的替代方案,您通常可以将这种行为变为:
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
configurer.setUseSuffixPatternMatch(false);
}
}
Now you can use dots everywhere :D
现在你可以在任何地方使用点:D