I am constructing the following JSON in my jQuery and sending to controller via ajax
var jsonResponse = [];
$(columns).each(function() {
if(!isEmpty($(this))) {
var lang = $(this).find(".language").val();
var item = {
"label": label,
"language": $(this).find(".language").val(),
"value": $(this).find(".value").val()
};
jsonResponse.push(item);
}
});
Since it is an array of individual items, I need to map it to an ArrayList in my controller.
My model objects are like this: FormModel - maps to the item
JSON element
class FormModel {
String label;
String language;
String value;
// getters & setters
}
FormModelWrapper - maps to the array of items
class FormModelWrapper {
private List<FormModel> formModel;
// getters & Setters
}
Finally the controller:
@RequestMapping(value = View Name, method = RequestMethod.POST)
public String performTask( @RequestBody FormModelWrapper wrapper) {
...
}
I have Jackson configured correctly. I have actually verified it by sending a JSON item
element and accepting it in FormModel
. The 415 Error occurs when I use array of json elements.
Any help would be appreciated.
I changed a lot of stuff here and there so I am not sure what was causing the error initially. Here is my solution that works:
jQuery:
var jsonResponse = [];
$(columns).each(function() {
if(!isEmpty($(this))) {
var lang = $(this).find(".language").val();
var item = {
"label": label,
"language": $(this).find(".language").val(),
"value": $(this).find(".value").val()
};
jsonResponse.push(item);
}
});
$.ajax({
url: "actual_url",
type: "POST",
dataType: 'json',
contentType:'application/json',
data: JSON.stringify(jsonResponse),
success: function(result) {
alert(" Call Succeeded");
},
error: function(jqXHR, ajaxOptions, thrownError) {
alert("Call Failed, " + thrownError);
}
});
Next, we have the form Model that maps to the item
element in the jQuery
class FormModel {
String label;
String language;
String value;
// getters & setters
}
Here is the controller using the FormModel
@RequestMapping(value = viewName, method = RequestMethod.POST)
public String doTask( @RequestBody List<FormModel> data) {
//your code here
}
I am using Jackson to do the JSON to POJO mapping
In my spring-context.xml
, I have the Jackson View Resolver as default View for the View Resolver
<property name="defaultViews">
<list>
<bean
class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
</list>
</property>
Finally, the Jackson Configuration, this is also in servlet-context.xml
<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> </bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jacksonMessageConverter"/>
</list>
</property>
</bean>
This code works, atleast for me. Hope that helps someone.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.