我收到以下错误,想知道如何解决此问题:
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;
}
}
JSP查看页面:
<%@ 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>
如何在JSON响应中获得结果? 我在pom.xml文件中有Jackson-mapper-asl和其他必需的依赖项。
结果我想得到:
data: {
"First Name": name,
"Last Name": name
}
例如:我在表单字段中输入的表单给了我一个错误。
答案 0 :(得分:0)
您正在执行POST
,这对于表单提交是正确的,但控制器仅响应GET
。此外,你的api搞砸了 - 文件扩展名暗示了一个html响应,但你试图只返回json或xml。其余代码也搞砸了 - 但你没有问过这个问题。