gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:45:34 26 4
gpt4 key购买 nike

尝试将 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com