request - How to post JSON in Spring MVC -
my controller , model classes below:
@controller @requestmapping("/apkinfo") public class apkinfocontroller { public @responsebody string registerapk(@requestbody apkinforeq req){ //do } } public class apkinfo implements serializable { private string deviceid; private list<apkinfo> apkinfo; //gettters , setters } public class apkinfo implements serializable { private string appname; private string packagename; private string version; private date installdate; }
then tried post data server using jquery ajax.
var param = { deviceid: '1234', apkinfo: [ {appname:'a', packagename:'a.a', version: '1', installdate:"2013-05-03 10:00:00"}, {appname:'b', packagename:'a.b', version: '2', installdate:"2013-05-03 10:00:00"} ] } $.ajax({ type: 'post', url : 'to_my_server_url', contenttype: 'application/json', datatype: 'json', data: param, error: function(){}, success: function(data){ //do } })
but getting error 'unspported mediatype'. have jackson mapping setting in applicationcontext.xml , @responsebody of other mothods work fine.
applicationcontext.xml
<context:component-scan base-package: "xx.xx.xxxxx.xx"; <contex:annotation-config /> <mvc:annotation-driven /> <bean id="jacksonmappingconverter" class="org.springframework.htttp.converter.json.mappingjacksonhttpmessageconverter"> <bean class="org.springframework.web.servlet.mvc.annotation.annotationmethodhandleradapter"> <property name="messageconverters"> <list> <ref bean ="jacksonmappingconverter" /> </list> </property> </bean>
how can post josn data? thanks.
edit:
before sending request converted json object:
$.ajax({ ... data: json.stringify(param) ... });
and have jackson library in pom.xml , no issue @responsebody.
i missed installdate field.
do have jackson jar inside classpath? can add @requestmapping(value="/mapping", consumes=mediatype.application_json_value) on method handler see sample- mappingcontroller
Comments
Post a Comment