json - 406 error on ajax call spring mvc -
this question has answer here:
i'm having hard time fixing 406 error spring mvc. want make ajax call , json response. i'm trying output json using alert(js). how implement it.
my controller:
@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; }
customer:
@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> <!-- controllers use controllerclassnamehandlermapping above, index controller using parameterizableviewcontroller, must define explicit mapping 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" /> <!-- 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>
i added following jars:
- jackson-core-asl 1.9.13
- jackson-mapper-asl 1.9.13
- jackson-core 2.4.4
- jackson-databind 2.4.4
- jackson-annotations 2.4.4
i dont know what's wrong. i'll appreciate suggestions.
Comments
Post a Comment