简体   繁体   English

Spring 3中的JSON请求出现404错误

[英]404 error on JSON requests in Spring 3

I'm trying to set up a controller in Spring 3 that will handle AJAX requests from the front end. 我正在尝试在Spring 3中设置一个控制器,该控制器将处理来自前端的AJAX请求。 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/ 我浏览了此页面,该页面非常简洁明了: 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... 但是,在我自己的应用程序中,当我向应有的网址发出请求时,它只会得到404 ...

Here's the Java code I have (which right now just takes a String parameter and returns it): 这是我拥有的Java代码(现在只需一个String参数并将其返回):

@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方法调用它时,得到了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. 我在我的app-servlet.xml中有标签,Maven正在使用3个Jackson罐(core,jaxrs和mapper,所有版本1.8.5)并将它们包装在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" 我唯一想到的可能是Spring Security不允许访问此路径,但是该属性中的此属性应让已通过身份验证的用户可以访问此路径:拦截URL模式=“ / **” access =“ ROLE_USER”

So I'm not sure what I'm missing. 所以我不确定我缺少什么。

I figured out the problem from this post - No mapping found for HTTP request with URI [/WEB-INF/pages/apiForm.jsp] 我从这篇文章中找出了问题-未找到带有URI [/WEB-INF/pages/apiForm.jsp]的HTTP请求的映射

If I add an extension in the servlet-mapping special to the DispatcherServlet, like so: 如果我在Servlet映射专用的DispatcherServlet中添加扩展,如下所示:

<servlet-mapping>
        <servlet-name>palladium</servlet-name>
        <url-pattern>*.json</url-pattern>
</servlet-mapping>

and then add the extension to the request mapping annotation: 然后将该扩展添加到请求映射注释中:

 @RequestMapping(value="/test.json", method=RequestMethod.GET)

Things work out fine. 一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM