Spring MVC-来自JDBC的JSON响应-根据请求“不可接受”;接受;标题
我发现以下错误,想知道如何解决:Spring MVC-来自JDBC的JSON响应-根据请求“不可接受”;接受;标题,json,spring,jsp,Json,Spring,Jsp,我发现以下错误,想知道如何解决: The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers. 我已附加控制器代码和JSP视图以呈现数据库调用的结果: 控制器: @Controller public class JsonController
The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.
我已附加控制器代码和JSP视图以呈现数据库调用的结果:
控制器:
@Controller
public class JsonController {
private static final String EMPLOYEE_DROP_DOWN = "employeeRequest";
@Autowired
EmployeeDAO employeeDAO;
@Autowired
EmployeeDAOImpl employeeDAOImpl;
@Autowired
ApplicationContext context;
@RequestMapping(value = "/employees/json/dropdown.htm", method = RequestMethod.GET, headers = "Accept=application/xml, application/json", produces = {
"application/json", "application/xml" })
public String getEmployeeDropDown(Map<String, Object> model) {
Employee employees = new Employee();
model.put("employees", employees);
List<String> employeeList = new ArrayList<String>();
List<Employee> details = employeeDAO.getEmployeeRelationshipList();
for (Employee record : details) {
employeeList.add(record.getLastName());
}
model.put("employeeList", employeeList);
return EMPLOYEE_DROP_DOWN;
}
@RequestMapping(value="/employees/json/data.htm", method = RequestMethod.GET, produces = "application/json")
public @ResponseBody Employee post( @ModelAttribute Employee employees) {
System.out.println(employees.getFirstName() + " " + employees.getLastName());
return employees;
}
}
例如:我在表单字段中输入的表单给了我一个错误。您正在执行一个
POST
,这对于表单提交是正确的,但是控制器只响应GET
。此外,您的api也是一团糟——文件扩展名意味着html响应,但您试图只返回json或xml。代码的其余部分也很混乱——但您还没有问过这一点。您是如何提出请求的?您是否正确指定了accept标头?您正在执行一个POST
,这对于表单提交是正确的,但是控制器只响应一个GET
。此外,您的api也很混乱——文件扩展名意味着html响应,但您试图只返回json或xml。不确定代码是否混乱。但是getEmployeeDropDown方法从数据库中获取下拉列表的数据。在表单字段中,如您所见,我有一个输入字段,我可以在下拉列表中手动输入。因此,我在这一点上的问题是:如何改进这段代码,并从下拉列表中获取lastName和手动输入firtname的JSON响应?谢谢你的回复。
<%@ include file="/WEB-INF/views/includes/taglibs.jsp" %>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<%-- for responsive side --%>
<script src="<c:url value="../../resources/js/bootstrap.js" />"></script>
<title>Employee Information</title>
<!--[if IE]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
</head>
<body>
<div class="container">
<p align="center">Please choose the last name and provide the corresponding first name...</p>
<form:form method="GET" action="/spring-jdbc/employees/json/data.htm" modelAttribute="employees">
<table align="center" cellpadding="10">
<tr>
<td>Employee Last Name:</td>
<td>
<form:select path="lastName">
<form:option value="NONE" label=" select the employee last name "/>
<form:options items="${employeeList}"/>
</form:select>
</td>
</tr>
<tr>
<td>Employee First Name:</td>
<td>
<form:input type="text" path="firstName"/>
</td>
</tr>
<td>
<input type="submit" value="Save Changes"/>
</td>
</table>
</form:form>
</div>
<script>
$(document).ready(function(){
sendAjax();
});
function sendAjax(){
$.ajax({
url: "/spring-jdbc/employees/json/data.htm",
type: 'POST',
dataType: 'json',
data: "{\"First Name\": \""+lastName+"\", \"Last Name\": \""+firstName+"\"}",
mimeTypes: 'application/json',
contentType: 'application/json',
success: function(data){
alert(data.firstName + " " + data.lastName);
},
error:function(data, status, er){
alert("error: "+data+" status: "+status+" er:"+er);
}
});
}
</script>
</body>
</html>
data: {
"First Name": name,
"Last Name": name
}