Spring 3 中 JSON 请求出现 404 错误
我正在尝试在 Spring 3 中设置一个控制器来处理来自前端的 AJAX 请求。我查看了此页面,它非常简洁地解释了它: http://blog.springsource.com/2010/01/25/ajax-simplifications-in-spring-3-0/
我能够使示例应用程序正常工作。然而,在我自己的应用程序中,当我向应该是的 url 发出请求时,它只是收到 404...
这是我拥有的 Java 代码(现在只接受一个 String 参数并返回it):
@RequestMapping(value="/test", method=RequestMethod.GET)
public @ResponseBody String test(@RequestParam String teacherId) {
return teacherId;
}
当我尝试通过 jQuery 的 getJSON 方法调用它时,我收到 404。
jQuery.getJSON("test", {teacherId:"123"});
我的 app-servlet.xml 中有该标签,并且 Maven 正在接收 3 个 Jackson jar(core、jaxrs 和mapper,所有版本 1.8.5)并将它们打包在 web 应用程序中。我唯一能想到的可能是 Spring Security 不允许访问此路径,但是 中的此属性应该让已经经过身份验证的用户可以访问此路径: 拦截url模式=“/ **”访问=“ROLE_USER”
所以我不确定我错过了什么。
I'm trying to set up a controller in Spring 3 that will handle AJAX requests from the front end. I had a look at this page which explains it very concisely and simply: http://blog.springsource.com/2010/01/25/ajax-simplifications-in-spring-3-0/
I was able to get the example application working correctly. However, in my own application it's just getting a 404 when I make a request to the url where it should be...
Here's the Java code I have (which right now just takes a String parameter and returns it):
@RequestMapping(value="/test", method=RequestMethod.GET)
public @ResponseBody String test(@RequestParam String teacherId) {
return teacherId;
}
When I try and call this via jQuery's getJSON method, I get a 404.
jQuery.getJSON("test", {teacherId:"123"});
I have the tag in my app-servlet.xml, and Maven is taking in 3 Jackson jars (core, jaxrs, and mapper, all versions 1.8.5) and packaging them in the webapp. The only thing I can think of is maybe Spring Security is not allowing access to this path, but this attribute in the should let already authenticated users have access to this:
intercept-url pattern="/**" access="ROLE_USER"
So I'm not sure what I'm missing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我从这篇文章中找出了问题 - 没有找到带有 URI 的 HTTP 请求的映射 [/WEB-INF/pages/apiForm.jsp]
如果我在 DispatcherServlet 的 servlet-mapping 特殊中添加一个扩展,例如所以:
然后将扩展添加到请求映射注释中:
一切顺利。
I figured out the problem from this post - No mapping found for HTTP request with URI [/WEB-INF/pages/apiForm.jsp]
If I add an extension in the servlet-mapping special to the DispatcherServlet, like so:
and then add the extension to the request mapping annotation:
Things work out fine.