试图将JSON数据发布到Spring控制器..根本不工作

时间:2012-06-13 16:47:42

标签: java ajax json spring spring-mvc

尝试将JSON数据发布到Spring控制器..根本不工作

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>

控制器代码:

@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

让我们看看是否有人能够正常工作......

1 个答案:

答案 0 :(得分:2)

尝试摘下

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

部分,我不把它放在我的控制器中,它们工作正常。