【问题标题】:JSON Response with Spring 3 MVC and jQuery使用 Spring 3 MVC 和 jQuery 的 JSON 响应
【发布时间】:2013-06-26 10:35:22
【问题描述】:

我在从简单的 Spring Controller 接收 JSON 时遇到了一些巨大的麻烦,尽管我检查了许多其他教程甚至官方 Spring 博客,但没有发现任何差异,所以请帮助我。

所以我在项目中的依赖是:

  • spring-context 3.2.2 发布
  • spring-web 3.2.2 发布
  • spring-webmvc 3.2.2 发布
  • spring-test 3.2.2 发布
  • junit 4.10
  • servlet-api 2.5
  • 大气运行时 1.1.0 RC4
  • logback-经典 1.0.13
  • libthrift 0.9.0
  • jackson-mapper-asl 1.9.12
  • jackson-core-asl 1.9.12

我的控制器非常简单,只是生成一个随机的 UUID 并返回它。它看起来如下:

@Controller
public class SimpleController {

@RequestMapping(value="/new", method=RequestMethod.GET)
public @ResponseBody SimpleResponse new() throws JsonGenerationException, JsonMappingException, IOException {

    SimpleResponse sr = new SimpleResponse();
    sr.setId(UUID.randomUUID().toString());

    return sr;

    }
}

模型只是一个简单的 POJO 类

public class SimpleResponse {

private String id;

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

}

配置是这样完成的

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     version="2.5" xmlns="http://java.sun.com/xml/ns/javaee">

<display-name>SimpleTest</display-name>

<servlet>
    <servlet-name>app</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>app</servlet-name>
    <url-pattern>/app/*</url-pattern>
</servlet-mapping>

</web-app>

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<mvc:annotation-driven />
<context:component-scan base-package="de.tum.ibis.wsc" />

</beans>

这就是服务器端。在客户端,我有一个只有一行 jQuery 代码的 html 页面

<!DOCTYPE html>
<html>
<head>
    <title>Test</title>
    <script src="http://code.jquery.com/jquery.js"></script>
    <script>

        $(document).ready(function() {

            $.getJSON("http://localhost:8080/Frontend/app/new", function(data) { console.log("it works"); });

        });

    </script>

</head>
<body>

</body>
</html>

现在根据我所阅读的所有内容,这应该可以工作,但它不适合我。如果我直接在浏览器中调用 localhost:8080/Frontend/app/new 我会得到如下信息: {"id":"b46b8d67-5614-44ed-90ef-d2da14d260f6"} 并且 Firebug 告诉我来自服务器的响应标头是

HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Server: Jetty(7.6.5.v20120716)

所以内容类型应该没问题。好吧,如果我现在运行 jquery ajax 调用,我会在 jquery.js 中收到错误“JSON.parse: unexpected end of data”,但我不知道为什么。我希望任何人都可以帮助我。谢谢!

------更新------

Firebug:jQuery 错误

萤火虫:我得到的一切

Firebug:如果直接访问 url,我会得到这样的结果

【问题讨论】:

  • 用你的萤火虫检查 ajax 请求并查看响应文本
  • 我添加了三张我得到的所有照片
  • 我注意到在您的请求(直接访问)中,您正在接受 text/html(这是不正确的),而您的 JSON 请求正在请求 application/json。您应该尝试使用 REST 客户端(我认为 Firefox 有一个插件),看看这是否导致了问题。我认为您的 Spring 控制器仅映射到 text/html 请求。
  • @DimitriM 我刚刚尝试使用 REST Client 插件访问该 URL,它也可以正常工作
  • 你定义了一个标题Accept: application/json吗?

标签: json spring jquery spring-mvc


【解决方案1】:

尝试在 Spring XML 配置中配置 ContentNegotiationManagerFactoryBean,参见 Spring docs

favorPathExtension 设置为false 并像这样更新方法的@RequestMapping

@RequestMapping(value="/new", method=RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)

【讨论】:

  • 感谢您的回答,但没有任何效果:(
  • @Ralf 只是一个疯狂的猜测 - 您拥有 jQuery 代码的 HTML 页面,它是否从您的应用程序中提供服务(即它是否在同一个域 - 本地主机上)? Firebug 数据中的Origin: null 看起来很可疑。
  • 天哪!!!我做了最愚蠢的事。不知何故,我没有在思考,我的大脑完全被转动了。我试图从位于我的文件系统上的 html 页面调用在 localhost 上运行的控制器,因此 file:///。如果我将 html 页面放在服务器上并运行 ajax 调用,它就可以正常工作。为我感到羞耻。谢谢!! :)
【解决方案2】:

在您的 AJAX 请求中,您正在使用

http://localhost:8080/Frontend/app/new

并且您的 servlet 声明 URL“/new”,您应该改用“/app/new”。

@RequestMapping(value="/app/new", method=RequestMethod.GET)

【讨论】:

  • 感谢您的回答,但 /app 指定了 spring 调度程序 servlet,如 web.xml /app/* 中所示。所以这不会导致问题,我可以通过调用 url 直接访问它,因此请求调度是正确的。
猜你喜欢
  • 1970-01-01
  • 2011-05-11
  • 2016-11-13
  • 2011-09-21
  • 1970-01-01
  • 1970-01-01
  • 2014-06-29
  • 2011-01-15
  • 1970-01-01
相关资源
最近更新 更多