我很难用spring MVC解决这个406错误。我想做的是进行Ajax调用并获得json响应。此外,我正在尝试使用alert(js)输出该json。这就是我实现它的方式。
我的控制器:
@RequestMapping(value="/search", headers="Accept=*/*", produces=MediaType.APPLICATION_JSON_VALUE) // no 'params' argument
@ResponseBody
public Customer findByFirstName() {
Customer test = new Customer();
test.setFirstName("test");
return test;
}
客户:
@Entity
@Table(name="customer",catalog="revised_cws_db")
public class Customer implements java.io.Serializable {
@Id @GeneratedValue(strategy=IDENTITY)
@Column(name="id", unique=true, nullable=false)
private int id;
@Version @Temporal(TemporalType.TIMESTAMP)
@Column(name="timestamp", nullable=false)
private Date timestamp;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="address_id", nullable=false)
private Address address;
@Temporal(TemporalType.DATE)
@Column(name="birth_date", nullable=false, length=10)
private Date birthDate;
@Column(name="first_name", nullable=false, length=45)
private String firstName;
@Column(name="middle_name", nullable=false, length=45)
private String middleName;
@Column(name="gender", nullable=false, length=45)
private String gender;
@Column(name="contact_number", length=45)
private String contactNumber;
@Column(name="family_members_count", nullable=false, length=45)
private String familyMembersCount;
@Column(name="occupation_id", nullable=false)
private int occupationId;
@Column(name="active", nullable=false)
private boolean active;
@OneToMany(fetch=FetchType.LAZY, mappedBy="customer")
private Set<Issues> issueses = new HashSet<Issues>(0);
@OneToMany(fetch=FetchType.LAZY, mappedBy="customer")
private Set<Account> accounts = new HashSet<Account>(0);
public Customer() {
}
public Customer(int id, Address address, Date birthDate, String firstName, String middleName, String gender, String familyMembersCount, int occupationId, boolean active) {
this.id = id;
this.address = address;
this.birthDate = birthDate;
this.firstName = firstName;
this.middleName = middleName;
this.gender = gender;
this.familyMembersCount = familyMembersCount;
this.occupationId = occupationId;
this.active = active;
}
public Customer(int id, Address address, Date birthDate, String firstName, String middleName, String gender, String contactNumber, String familyMembersCount, int occupationId, boolean active, Set<Issues> issueses, Set<Account> accounts) {
this.id = id;
this.address = address;
this.birthDate = birthDate;
this.firstName = firstName;
this.middleName = middleName;
this.gender = gender;
this.contactNumber = contactNumber;
this.familyMembersCount = familyMembersCount;
this.occupationId = occupationId;
this.active = active;
this.issueses = issueses;
this.accounts = accounts;
}
//getters, setters omitted
的servlet:
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<mvc:annotation-driven/>
<context:component-scan base-package="com.controller" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="cacheSeconds" value="0" />
</bean>
<!--
Most controllers will use the ControllerClassNameHandlerMapping above, but
for the index controller we are using ParameterizableViewController, so we must
define an explicit mapping for it.
-->
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<!--
The index controller.
-->
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
JSP:
<%@taglib uri = "http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
$(document).ready(function() {
$(".search-form").keyup(function() {
if ($(this).val().length >= 2) {
$('#searchForm').submit();
}
});
$('#searchForm').submit(function(event) {
$.ajax({
dataType: 'json',
data : $(this).serialize(),
url: $(this).attr("action"),
success: function( result ) {
alert("test");
}
});
event.preventDefault();
});
});
</script>
</head>
<body>
<h1>Customers</h1>
<form id="searchForm" action="search.htm">
Search Customer:
<input class="search-form" name="firstName" placeholder="First Name">
<input type="submit">
</form>
</body>
我已经添加了以下罐子:
我不知道什么是错的。我会感激任何建议。