java 试图将 JSON 数据发布到 Spring 控制器.. 根本不工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11019565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trying to post JSON data to Spring controller.. Not working at all
提问by SJS
Trying to post JSON data to Spring controller.. Not working at all
试图将 JSON 数据发布到 Spring 控制器.. 根本不工作
JSP Code:
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 Code:
控制器代码:
@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);
}
and now for the output or errors:
现在是输出或错误:
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
lets see if anyone can get this working...
让我们看看是否有人可以让这个工作......
回答by Triton Man
Try just taking off the
试着脱掉
headers ={"Accept=application/json"},
Section, I don't put this in my controllers and they work fine.
部分,我没有把它放在我的控制器中,它们工作正常。