java - 尝试将 JSON 数据发布到 Spring Controller ..根本不起作用

标签 java ajax json spring spring-mvc

尝试将 JSON 数据发布到 Spring Controller ..根本不起作用

JSP代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>This is a project to show how to use RESTful</title>
</head>
<body>
<script type="text/javascript">var contexPath = "<%=request.getContextPath()%>";</script>
<script src="<%=request.getContextPath()%>/js/jquery.js"></script>


<script type="text/javascript">
function doAjaxPost() {  

    alert("doAjaxPost Called");

     var queryString = $('#htmlform').serialize();

       $.ajax({
            contentType : "application/json",
            dataType : 'json',
            type : "PUT",
            url : contexPath + "/service/employee",
            data : queryString, //json serialization (like array.serializeArray() etc)

            success : function(data) {
                alert("Thanks for submitting.  \n\n" + response.result);
               // response
            },
            error : function(request, status, error) {
                   alert('Error: ' + e); 
            }
        });
    }  
</script>

<H1>Add Employee</H1>

<p>
<form name="htmlform">
<table border=1>
    <thead><tr>
        <th>ID</th>
        <th>Name</th>
        <th>Email</th>
    </tr></thead>

    <tr>
        <td><input  type="text" name="ID" maxlength="5" size="3"></td>
        <td><input  type="text" name="Name" maxlength="10" size="10"></td>
        <td><input  type="text" name="Email" maxlength="10" size="10"></td>
    </tr>

</table>
<input type="button" value="Save Employee" onclick="doAjaxPost();" />
<p>
<p>
</form>
[<a href="http://localhost:8080/RESTful/service/employees">List all Employees</a> | <a href="add.jsp">Employee Form Test</a>]


</body>
</html>

Controller 代码:

@RequestMapping(headers ={"Accept=application/json"}, method=RequestMethod.PUT, value="/employee" )
    public ModelAndView updateEmployee(@RequestBody Employee employee ) {
        System.out.println("in put body:");
        System.out.println("HELLO WORD" + employee.toString());

        return new ModelAndView(XML_VIEW_NAME, "object", employee);
    }

现在是输出或错误:

2012-06-13 12:46:52,571 [http-8080-2] DEBUG org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'rest' processing PUT request for [/RESTful/service/employee]
2012-06-13 12:46:52,576 [http-8080-2] DEBUG org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - Mapping [/employee] to HandlerExecutionChain with handler [dw.spring3.rest.controller.EmployeeController@7ba6eeab] and 1 interceptor
2012-06-13 12:46:52,604 [http-8080-2] DEBUG org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver - Resolving exception from handler [dw.spring3.rest.controller.EmployeeController@7ba6eeab]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported
2012-06-13 12:46:52,606 [http-8080-2] DEBUG org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver - Resolving exception from handler [dw.spring3.rest.controller.EmployeeController@7ba6eeab]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported
2012-06-13 12:46:52,606 [http-8080-2] DEBUG org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Resolving exception from handler [dw.spring3.rest.controller.EmployeeController@7ba6eeab]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported
2012-06-13 12:46:52,607 [http-8080-2] DEBUG org.springframework.web.servlet.DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'rest': assuming HandlerAdapter completed request handling
2012-06-13 12:46:52,607 [http-8080-2] DEBUG org.springframework.web.servlet.DispatcherServlet - Successfully completed request

让我们看看是否有人可以让这个工作......

最佳答案

尝试取下

headers ={"Accept=application/json"}, 

部分,我没有将其放入我的 Controller 中,它们工作正常。

关于java - 尝试将 JSON 数据发布到 Spring Controller ..根本不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11019565/

相关文章:

Java:生成幂集

java - LocalDate.plusMonth 给出的二月结果不正确

php - 使用 AJAX + jQuery + PHP 将数据发送到 MySQL

php - 使用ajax php mysql的用户通知/警报系统

c# - JSON 文件发布到 azure 后无法工作,但在本地可以正常工作

java - 无法从 JSONObject 获取值

java - 什么东西便宜 : Traversal using n iterators of a single ConcurrentHashMap or n instances of a HashMap

java - 如何从另一个类访问字符串值?

javascript - 无法将函数绑定(bind)到 ajax 请求

asp.net - 在project.json中用 "bin"包装多个DLL的语法是什么