使用 Spring 3 MVC 和 jQuery 的 JSON 响应

标签 json spring jquery spring-mvc

我在从简单的 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-classic 1.0.13
  • libthrift 0.9.0
  • jackson 映射器-asl 1.9.12
  • jackson-core-asl 1.9.12

我的 Controller 非常简单,只是生成一个随机 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>

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

<!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 告诉我来自服务器的响应 header 是

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:意外的数据结束”,但我不知道为什么。我希望任何人都可以帮助我。谢谢!

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

Firebug:jQuery 错误

Firebug: jQuery error

Firebug:我得到的一切

Firebug: All I get

Firebug:这是直接访问 url 时得到的结果

Firebug: This is what I get if a access the url directly

最佳答案

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

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

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

关于使用 Spring 3 MVC 和 jQuery 的 JSON 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17317742/

相关文章:

javascript - 测试 JSON 中的名称是否可用,如果不可用,则执行其他 JSON 请求

spring - @NotTransactional替代

javascript - 如何获取浏览器时区和国家名称?

javascript - 使用循环索引从对象中提取数据的问题

javascript - Chrome 中的 json-server "Access to localhost was denied"

javascript - 无法使用 AJAX 和 JSON 响应在 HTML 中创建表格

java - 在动态创建的类中实例化 spring bean

Spring Netflix Eureka 服务器以非法 URL 在 intelliJ 启动

jquery - 如何使用 javascript 函数调用 MVC 中的 URL 操作?

javascript - MVC Jquery文件上传Request.Files始终为空