Reputation: 31
I am using a simple POJO as a session bean and using the same POJO as a json response for my $.ajax calls.
POJO
public class TransportPacket implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private String fieldName , fieldValue, query, error, buttonName, formName, actionName;
private List<String> results = new ArrayList<String>();
private boolean redirect = false;
private Object[][] data;
private Map<String, String> mapOfFieldsWithValues;
private String[] fieldsToBeShown;
private List<String> extraFieldsChosen = new ArrayList<String>();
public String getFormName() {
return formName;
}
public void setFormName(String formName) {
this.formName = formName;
}
public String getActionName() {
return actionName;
}
public void setActionName(String actionName) {
this.actionName = actionName;
}
@JsonIgnore
public void initialize() {
fieldName = "";
fieldValue = "";
query = "";
error = "";
buttonName = "";
if(this.getMapOfFieldsWithValues() != null) {
this.getMapOfFieldsWithValues().clear();
}
this.getExtraFieldsChosen().clear();
this.fieldsToBeShown = null;
}
@JsonIgnore
public void clear() {
this.initialize();
}
public String getButtonName() {
return buttonName;
}
public void setButtonName(String buttonName) {
this.buttonName = buttonName;
}
public String getError() {
return error;
}
public void setError(String error) {
this.error = error;
}
public String getQuery() {
return query;
}
public void setQuery(String query) {
this.query = query;
}
public String getFieldName() {
return fieldName;
}
public void setFieldName(String fieldName) {
this.fieldName = fieldName;
}
public String getFieldValue() {
return fieldValue;
}
public void setFieldValue(String fieldValue) {
this.fieldValue = fieldValue;
}
public List<String> getResults() {
return results;
}
public void setResults(List<String> results) {
this.results = results;
}
public boolean isRedirect() {
return redirect;
}
public void setRedirect(boolean redirect) {
this.redirect = redirect;
}
public Object[][] getData() {
return data;
}
public void setData(Object[][] data) {
this.data = data;
}
public Map<String, String> getMapOfFieldsWithValues() {
return mapOfFieldsWithValues;
}
public void setMapOfFieldsWithValues(Map<String, String> mapOfFieldsWithValues) {
this.mapOfFieldsWithValues = mapOfFieldsWithValues;
}
public String[] getFieldsToBeShown() {
return fieldsToBeShown;
}
public void setFieldsToBeShown(String[] fieldsToBeShown) {
if(this.fieldsToBeShown != null) {
List<String> existingFields = Arrays.asList(this.fieldsToBeShown);
this.getExtraFieldsChosen().clear();
for(String field : fieldsToBeShown) {
if(!existingFields.contains(field)) {
this.getExtraFieldsChosen().add(field);
}
}
} else {
this.getExtraFieldsChosen().addAll(Arrays.asList(fieldsToBeShown));
}
this.fieldsToBeShown = fieldsToBeShown;
}
public List<String> getExtraFieldsChosen() {
return extraFieldsChosen;
}
public void setExtraFieldsChosen(List<String> extraFieldsChosen) {
this.extraFieldsChosen = extraFieldsChosen;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
In my ApplicationContext.xml I have added the following :
<bean id="packet" class="com.visa.vdps.dbmsrep.data.TransportPacket" scope="session">
<aop:scoped-proxy/>
</bean>
and
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
</mvc:message-converters>
</mvc:annotation-driven>
Now I am Autowiring the bean using @Autowired in one of my Controllers. In the same controller I am using the following method :
@RequestMapping(value="/SendFieldValueBack.htm", method=RequestMethod.POST)
public @ResponseBody TransportPacket validateField(HttpServletRequest request, HttpServletResponse response) {
String fieldName = request.getParameter("fieldName");
String fieldValue = request.getParameter("fieldValue");
// POJO
packet.setFieldName(fieldName);
packet.setFieldValue(fieldValue);
packet.setMapOfFieldsWithValues(this.mapOfFieldsWithValues);
this.addingActionPerformed(true);
request.getSession().setAttribute("packet", packet);
return this.getPacket();
}
This method is called from the following ajax method :
$(document).ready(
$.ajax({
url: "SendFieldValueBack.htm",
data: {"fieldName":fieldName, "fieldValue":fieldValue},
dataType: "json",
type: "POST",
cache: false,
success: function(data, textStatus, jQxhr){
alert("success");
evaluateDataFromController(data);
},
error: function(xhr, ajaxOptions, thrownError){
// code here
alert("Error from SendFieldValueBack.htm");
writeToConsole(thrownError);
}
}));
I get the error alert "Error from SendFieldValueBack.htm" and the following stacktrace :
13963 [http-bio-8080-exec-6] WARN org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: No serializer found for class org.springframework.context.expression.StandardBeanExpressionResolver and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: com.visa.vdps.dbmsrep.data.TransportPacket$$EnhancerBySpringCGLIB$$be504698["targetSource"]->org.springframework.aop.target.SimpleBeanTargetSource["beanFactory"]->org.springframework.beans.factory.support.DefaultListableBeanFactory["beanExpressionResolver"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.springframework.context.expression.StandardBeanExpressionResolver and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: com.visa.vdps.dbmsrep.data.TransportPacket$$EnhancerBySpringCGLIB$$be504698["targetSource"]->org.springframework.aop.target.SimpleBeanTargetSource["beanFactory"]->org.springframework.beans.factory.support.DefaultListableBeanFactory["beanExpressionResolver"]) 13963 [http-bio-8080-exec-6] WARN org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Handler execution resulted in exception: Could not write content: No serializer found for class org.springframework.context.expression.StandardBeanExpressionResolver and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: com.visa.vdps.dbmsrep.data.TransportPacket$$EnhancerBySpringCGLIB$$be504698["targetSource"]->org.springframework.aop.target.SimpleBeanTargetSource["beanFactory"]->org.springframework.beans.factory.support.DefaultListableBeanFactory["beanExpressionResolver"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.springframework.context.expression.StandardBeanExpressionResolver and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: com.visa.vdps.dbmsrep.data.TransportPacket$$EnhancerBySpringCGLIB$$be504698["targetSource"]->org.springframework.aop.target.SimpleBeanTargetSource["beanFactory"]->org.springframework.beans.factory.support.DefaultListableBeanFactory["beanExpressionResolver"])
I am using Jackson2HttpMessageConverter. Could someone please help me ?
Thanks in advance.
(I am posting a question for the first time here. So please let me know if I have not done this in a proper way)
Upvotes: 3
Views: 2672
Reputation: 534
I know it's been years, but I stumbled upon this because I had the same issue. The TransportPacket
object created by Spring is a proxy, so it has lots of other fields and methods on it that aren't defined by you. A good strategy here would be to create a new class, say, TransportPacketDTO
, and transfer properties from TransportPacket
to TransportPacketDTO
, and this is what you would return from your @RequestMapping
method.
Upvotes: 1