jQuery post can't get entity object as json - json

I need to get my 'Comment' (stored in DAO) object from my controller and display it in my JSP but every time I see the error message from the error block.
Why is this happenning and what should I do?
Logic of my code is the next:
After clicking on the 'Reply' button data from the form is sent to my controller.
Controller saves data in DB and returns 'Comment' entity.
I get this 'Comment' entity in my JSP page and use it for publishing on the page.
But I get the error msg from error block instead of msg from the success block.
Here is my form:
<form id="comment_${comment.getCommentId()}">
<textarea class="textarea" rows="10" name="text"></textarea>
<input type="hidden" name="bookId" value="${book.getBookId()}" />
<input type="hidden" name="parentId" />
<input type="hidden" name="commentId" value="${comment.getCommentId()}" /><br />
<input type="button" class="button" value="Reply" id="submit_${comment.getCommentId()}" onclick="ajaxsubmit(this.id)"/>
</form>
Here is my script:
<script type="text/javascript">
function ajaxsubmit(buttonId){
var formId = document.getElementById(buttonId).parentNode.id;
var dataString = $("#" + formId).serialize();
$.ajax( {
url: "ajaxcomment.htm",
type: "post",
data: dataString,
dataType: "json",
success: function(data) {
alert(data.getCommentAdded());
},
error: function() {
alert("error");
}
} );
}
Here is my controller
#RequestMapping(value = "ajaxcomment.htm", method = RequestMethod.POST)
public #ResponseBody Comment ajaxcomment(
HttpServletRequest httpServletRequest,
#RequestParam(value = "bookId", required = false) Long bookId,
#RequestParam(value = "parentId", required = false) Long parentId,
#RequestParam(value = "commentId", required = false) Long commentId,
#RequestParam(value = "text", required = true) String text) {
String username = httpServletRequest.getRemoteUser();
User user = userDao.getUserByUsername(username);
Comment comment = new Comment();
// DAO logic
commentDao.addComment(comment);
return comment;
}
Here is my 'Comment' entity:
#Entity #Table(name = "COMMENT") public class Comment implements Serializable {
#Id
#GeneratedValue
#Column(name = "COMMENT_ID", nullable = false)
private Long commentId;
#Column(name = "COMMENT_TEXT", length = 512, nullable = true)
private String commentText;
#Column(name = "COMMENT_ADDED", length = 128, nullable = true)
#Temporal(TemporalType.TIMESTAMP)
private Date commentAdded;
#ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.EAGER)
#JoinColumn(name = "BOOK_ID")
private Book book;
#ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.EAGER)
#JoinColumn(name = "PARENT_ID")
private Comment parentComment;
#OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "parentComment")
#OrderBy("commentAdded")
private Collection<Comment> subcomments;
public void setCommentText(String commentText) {
this.commentText = commentText;
}
public String getCommentText() {
return this.commentText;
}
// other getters and setters are public too
And here is my applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="com.demo"/>
<context:component-scan base-package="com.demo.service"/>
<context:annotation-config />
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jacksonMessageConverter" />
</list>
</property>
</bean>
<bean id="exceptionMessageAdapter"
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver">
<property name="messageConverters">
<list>
<!-- Support JSON -->
<ref bean="jacksonMessageConverter" />
</list>
</property>
</bean>

It might be that in your form you are setting values like value="${book.getBookId()}" instead of value="${book.bookId}".
In other words you have to use the expression language by referring to the name of the field in the bean (entity or whatever) rather than trying to use the getter for that field directly.

It works.
I've updated my controller method (handler):
#Autowired private DAOComment commentDao;
#RequestMapping(value = "ajaxcomment.htm", method = RequestMethod.POST)
public #ResponseBody String ajaxcomment(
HttpServletRequest httpServletRequest,
#RequestParam(value = "bookId", required = false) Long bookId,
#RequestParam(value = "parentId", required = false) Long parentId,
#RequestParam(value = "commentId", required = false) Long commentId,
#RequestParam(value = "text", required = true) String text) {
String username = httpServletRequest.getRemoteUser();
User user = userDao.getUserByUsername(username);
Comment comment = new Comment();
// DAO logic
commentDao.addComment(comment);
return "{\"id\":\"" + comment.getCommentId() + "\"," +
"\"username\":\"" + comment.getUser().getUsername() + "\"," +
"\"text\":\"" + comment.getCommentText() + "\"," +
"\"added\":\"" + comment.getFormattedDate() + "\"}";
}
I used \" instead of ' because JSON.parse() doesn't parse string like 'param':'value', "param":"value" only.
Updated script:
<script type="text/javascript">
function ajaxsubmit(buttonId, className){
var formId = document.getElementById(buttonId).parentNode.id;
var dataString = $("#" + formId).serialize();
$.ajax( {
url: "ajaxcomment.htm",
type: "post",
data: dataString,
dataType: "text",
success: function(data) {
var response = JSON.parse(data);
//This's it.
alert(response.id);
},
error: function() {
alert("Error has occured.");
}
} );
}
</script>

Related

Hibernate Optimistic Lock with/without a Version not working

I have a legacy app that uses:
hibernate-3.5.5.jar
hibernate-jpa-2.0-api-1.0.0.jar
Spring 3.0.2
Tapestry 5.3.8
MySQL
Tomcat 7.0.64
It has a serious issue with multiple users updating the same table row at the same time and loosing the first update. Basically user A says "I want to own the record" (place my id in the record) and user B says "I want to own the record" the code being processed takes a bit of time. So user A gets it and then user B doesn't notice that user A had it and so user B gets it when he shouldn't have because user A already had it.
I've tried using:
#org.hibernate.annotations.Entity(dynamicUpdate = true, optimisticLock = OptimisticLockType.ALL)
on the Entity class for the table and watching the SQL that hibernate produces it never adds the table columns to the SQL update statement. It simply has update ... where id=?.
I've also tried adding a version column to the table in question and the Entity class and annotated the field with
#Version.
This has exactly the same effect as above, nothing in the generated SQL is using the version column. It never gets incremented either.
I'm guessing that I'm missing something with setting this up or there is something about the way the app uses hibernate that is keeping this from working as everything I have read says it should "Just Work".
appContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- Configurer that replaces ${...} placeholders with values from a properties
file -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:app_jdbc.properties"/>
</bean>
<!-- Message source for this context, loaded from localized files -->
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>app_app</value>
<value>app_env</value>
<value>pdf</value>
</list>
</property>
</bean>
<!-- Define data source -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<value>${jdbc.driverClassName}</value>
</property>
<property name="url">
<value>${jdbc.url}</value>
</property>
<property name="username">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
<property name="defaultAutoCommit">
<value>${jdbc.autoCommit}</value>
</property>
<property name="maxActive">
<value>${dbcp.maxActive}</value>
</property>
<property name="maxWait">
<value>${dbcp.maxWait}</value>
</property>
</bean>
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
...
<value>company.app.domain.Overtime</value>
...
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.query.substitutions">${hibernate.query.substitutions}</prop>
</props>
</property>
</bean>
<!-- Transaction manager for a single Hibernate SessionFactory (alternative
to JTA) -->
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<!-- regular beans -->
<bean id="baseDao" class="vive.db.BaseHbDao">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
...
<bean id="overtimeDao" class="company.app.dataaccess.OvertimeDao">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
...
<!-- service beans -->
<bean id="appService" class="company.app.services.AppService">
<property name="baseDao"><ref local="baseDao"/></property>
...
</bean>
<!-- transaction advice -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="serviceOperation"
expression="execution(* company.app.services.*Service.*(..))" />
<aop:advisor pointcut-ref="serviceOperation" advice-ref="txAdvice" />
</aop:config>
</beans>
Overtime Entity class:
package company.app.domain;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;
import org.hibernate.annotations.OptimisticLockType;
#Entity
#Table(name = "over_time")
#org.hibernate.annotations.Entity(dynamicUpdate = true, optimisticLock = OptimisticLockType.ALL)
public class Overtime implements java.io.Serializable {
private static final long serialVersionUID = 7263309927526074109L;
#Id
#GeneratedValue(generator = "ot_gen")
#GenericGenerator(name = "ot_gen", strategy = "hilo", parameters = {
#Parameter(name = "table", value = "unique_key"), #Parameter(name = "column", value = "next_hi"),
#Parameter(name = "max_lo", value = "99") })
private Integer id;
#Deprecated
#Column(name = "from_time")
private Date fromTime;
#Deprecated
#Column(name = "to_time")
private Date toTime;
#Column(name = "fm_dttm")
private Long fromDttm;
#Column(name = "to_dttm")
private Long toDttm;
#Column(name = "post_dttm")
private Long postDttm;
private String dow;
private String shift;
#Column(name = "sub_groups")
private String subGroups;
#Column(name = "created_by")
private String createdBy;
#Column(name = "signed_up_by")
private String signedUpBy;
#Column(name = "signed_up_via")
private String signedUpVia;
#Column(name = "date_signed_up")
private Date dateSignedUp;
#Column(name = "signed_up_by_partner_username")
private String signedUpByPartnerUsername;
#Column(name = "signed_up_by_partner_ot_refno")
private String signedUpByPartnerOtRefNo;
private String comment;
private Integer status;
#Column(name = "title_abbrev")
private String titleAbbrev;
#Column(name = "record_status")
private String recordStatus;
#Column(name = "ref_no")
private String refNo;
#Column(name = "ref_id")
private String refId;
#Column(name = "misc_notes")
private String miscNotes;
#Column(name = "sends_notif_upon_posting")
private Boolean sendsNotificationUponPosting;
#Column(name = "notify_post_person_when_filled")
private Boolean notifyPostPersonWhenFilled;
#Column(name = "notify_others_when_filled")
private Boolean notifyOthersWhenFilled;
#Column(name = "vehicle_needed")
private Boolean vehicleNeeded;
#Column(name = "agency_id")
private Integer agencyId;
#Column(name = "schedule_id")
private Integer scheduleId;
#Column(name = "post_date")
private Date postDate;
#Column(name = "enrollment_opens_at")
private Date enrollmentOpensAt;
#Column(name = "enrollment_closes_at")
private Date enrollmentClosesAt;
#ManyToOne(fetch = FetchType.EAGER)
#JoinColumn(name = "class_id")
private OvertimeClass overtimeClass;
public Overtime() {
}
//getters and setters
}
The Tapestry page class where the user tries to sign up for the overtime:
package company.app.pages;
import java.io.*;
import java.text.MessageFormat;
import java.util.*;
import org.apache.tapestry5.StreamResponse;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.InjectComponent;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.corelib.components.Zone;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.PageRenderLinkSource;
import org.apache.tapestry5.services.Request;
import org.apache.tapestry5.services.RequestGlobals;
import org.hibernate.StaleObjectStateException;
import org.slf4j.Logger;
import org.springframework.transaction.annotation.Transactional;
import vive.util.*;
import company.t5ext.LabelValueSelectModel;
import company.t5ext.components.DateTimeField;
import company.app.*;
import company.app.domain.*;
import company.app.services.CacheService;
import company.app.services.AppService;
import company.app.comparator.OtComparator;
#RequiresLogin
public class ListPostedOvertime {
#SessionState
#Property
private AppSessionState visit;
#Inject
private RequestGlobals requestGlobals;
#Inject
#Property
private AppService appService;
#Inject
private Request request;
void setupRender() {
...
}
// this method handle the case when a user tries to sign up for an overtime slot
void onSignUp(Integer overtimeId) {
// check to see if the OT has been deleted or modified or signed-up
Overtime ot = (Overtime)appService.getById(Overtime.class, overtimeId);
if (ot == null) {
visit.setOneTimeMessage("The overtime has already been deleted.");
return;
}
if (ot.getStatus() != null && ot.getStatus() != AppConst.OT_NEW) {
visit.setOneTimeMessage("The overtime has already been signed up. Please choose a different one to sign up.");
return;
}
...
try {
appService.validateOvertimeForUser(agency, user, ot);
appService.handleSignUpOvertime(agency, user, ot);
// log activity
String what = "Signed up for overtime " + ot.getRefNo() + ".";
appService.logActivity(user, AppConst.LOG_OVERTIME, what);
} catch(StaleObjectStateException e) {
visit.setOneTimeMessage("The overtime record has been changed by another user, please try again.");
return;
} catch(Exception e) {
visit.setOneTimeMessage(e.getMessage());
return;
}
...
}
}
The AppService class that is used by the Tapestry page to update the overtime record:
package company.app.services;
import java.io.Serializable;
import java.util.*;
import java.text.DecimalFormat;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.support.ResourceBundleMessageSource;
import vive.db.BaseHbDao;
import vive.util.*;
import company.app.*;
import company.app.comparator.LeaveRequestComparator;
import company.app.comparator.UserOtInterestComparator;
import company.app.dataaccess.*;
import company.app.domain.*;
public class AppService
{
private Log log = LogFactory.getLog(this.getClass().getName());
private BaseHbDao baseDao;
private OvertimeDao otDao;
private MiscDao miscDao;
private ResourceBundleMessageSource msgSource;
/**
* Default constructor.
*/
public AppService() {
}
public void save(Object item) {
if (item != null) {
baseDao.save(item);
}
}
public void update(Object item) {
if (item != null) {
baseDao.update(item);
}
}
public void saveOrUpdate(Object item) {
if (item != null) {
baseDao.saveOrUpdate(item);
}
}
public void saveOrUpdateAll(Collection col) {
if (col != null) {
baseDao.saveOrUpdateAll(col);
}
}
public void delete(Object item) {
if (item != null) {
baseDao.delete(item);
}
}
public void deleteAll(Collection col) {
if (col != null) {
baseDao.deleteAll(col);
}
}
public Object getById(Class clazz, Serializable id) {
return baseDao.get(clazz, id);
}
public Object getById(Class clazz, Serializable id, LockMode lockMode) {
return baseDao.get(clazz, id, lockMode);
}
public void validateOvertimeForUser(Agency agency, User user, Overtime ot) throws Exception {
validateOvertimeForUser(agency.getId(), agency, user, ot);
}
public void validateOvertimeForUser(AgencyLite agency, User user, Overtime ot) throws Exception {
validateOvertimeForUser(agency.getId(), agency, user, ot);
}
public void handleSignUpOvertime(AgencyBase agency, User user, Integer otId) {
Overtime ot = (Overtime)getById(Overtime.class, otId);
handleSignUpOvertime(agency, user, ot);
}
public void handleSignUpOvertime(AgencyBase agency, User user, Overtime ot) {
handleSignUpOvertime(agency, user, ot, 1.0d);
}
public void handleSignUpOvertime(AgencyBase agency, User user, Integer otId, Double ptsPerOt) {
Overtime ot = (Overtime)getById(Overtime.class, otId);
handleSignUpOvertime(agency, user, ot, ptsPerOt);
}
public void handleSignUpOvertime(AgencyBase agency, User user, Overtime ot, Double ptsPerOt) {
handleSignUpOvertime(agency, user, ot, ptsPerOt, null, null);
}
public void handleSignUpOvertime(AgencyBase agency, User user, Overtime ot, Double ptsPerOt, String viaUsername, String viaName) {
Date today = new Date();
boolean isOtConfirmRequired = AppUtil.isTrue(agency.getOtConfirmRequired());
Integer otConfirmThreshold = 0;
if (agency.getOtConfirmThreshold() != null) {
otConfirmThreshold = agency.getOtConfirmThreshold();
}
long otInDays = (ot.getFromDttm() - today.getTime()) / AppConst.MILLIS_IN_DAY;
ot.setSignedUpBy(user.getUsername());
ot.setDateSignedUp(today);
ot.setSignedUpVia(viaUsername);
if (isOtConfirmRequired && otInDays >= otConfirmThreshold) {
ot.setStatus(AppConst.OT_PDG);
} else {
ot.setStatus(AppConst.OT_FIN);
}
saveOrUpdate(ot);
user.setLastOtSignupDate(today);
user.setPoints(AppUtil.addPoints(ptsPerOt, user.getPoints()));
saveOrUpdate(user);
...
// email notification sent from caller
}
...
}
The base class for all of the DAO classes:
package vive.db;
import java.io.Serializable;
import java.util.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.HibernateException;
import org.hibernate.type.Type;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import vive.XException;
import vive.util.XUtil;
/**
* The superclass for hibernate data access object.
*/
public class BaseHbDao extends HibernateDaoSupport implements BaseHbDaoInterface
{
private Log log;
public BaseHbDao() {
super();
log = LogFactory.getLog(getClass());
}
...
/**
* Save or update an object.
*/
public void saveOrUpdate(Object obj) {
getHibernateTemplate().saveOrUpdate(obj);
}
public void save(Object obj) {
getHibernateTemplate().save(obj);
}
public void update(Object obj) {
getHibernateTemplate().update(obj);
}
/**
* Delete an object.
*/
public void delete(Object obj) {
getHibernateTemplate().delete(obj);
}
/**
* Retrieve an object of the given id, null if it does not exist.
* Similar to "load" except that an exception will be thrown for "load" if
* the given record does not exist.
*/
public Object get(Class clz, Serializable id) {
return getHibernateTemplate().get(clz, id);
}
public Object get(Class clz, Serializable id, LockMode lockMode) {
return getHibernateTemplate().get(clz, id, lockMode);
}
...
public void flush() {
getHibernateTemplate().flush();
}
/**
* Retrieve a HB session.
* Make sure to release it after you are done with the session by calling
* releaseHbSession.
*/
public Session getHbSession() {
try {
return getSession();
} catch (Exception e) {
return null;
}
}
/**
* Release a HB Session
*/
public void releaseHbSession(Session sess) {
releaseSession(sess);
}
}
Okay I got it working!
First, I'm using the #Version annotation so I added a version column to the table with the problem.
alter table over_time add version INT(11) DEFAULT 0;
Second, add the Version annotation and member to the Entity class:
public class Overtime implements java.io.Serializable {
private static final long serialVersionUID = 7263309927526074109L;
#Id
#GeneratedValue(generator = "ot_gen")
#GenericGenerator(name = "ot_gen", strategy = "hilo", parameters = {
#Parameter(name = "table", value = "unique_key"), #Parameter(name = "column", value = "next_hi"),
#Parameter(name = "max_lo", value = "99") })
private Integer id;
#Version
#Column(name = "version")
private int version;
...
When I tried this the first couple of times I was using an Integer object not an int primitive for the version member of the class. I THINK this was the issue.
Also make sure that the other hibernate specific annotation is NOT on the entity class:
#org.hibernate.annotations.Entity(dynamicUpdate = true, optimisticLock = OptimisticLockType.ALL)
Third, the exception that gets thrown is not what any of the web sites I've read say it should be, so let's catch the one that really is thrown in the Tapestry page class that handles the user signing up for the overtime record.
void onSignUp(Integer overtimeId) {
// check to see if the OT has been deleted or modified or signed-up
Overtime ot = (Overtime)appService.getById(Overtime.class, overtimeId);
if (ot == null) {
visit.setOneTimeMessage("The overtime has already been deleted.");
return;
}
if (ot.getStatus() != null && ot.getStatus() != AppConst.OT_NEW) {
visit.setOneTimeMessage("The overtime has already been signed up. Please choose a different one to sign up.");
return;
}
...
try {
appService.validateOvertimeForUser(agency, user, ot);
appService.handleSignUpOvertime(agency, user, ot);
// log activity
String what = "Signed up for overtime " + ot.getRefNo() + ".";
appService.logActivity(user, AppConst.LOG_OVERTIME, what);
} catch(HibernateOptimisticLockingFailureException x) {
visit.setOneTimeMessage("The overtime record has been changed by another user, please try again.");
return;
} catch(Exception e) {
visit.setOneTimeMessage(e.getMessage());
return;
}
...

json request not parsed in controller correctly

i am struggling with something that should work but doesn't...
i have this mapping in my controller:
#RequestMapping(value = "/keys", method = RequestMethod.POST)
#Consumes(MediaType.APPLICATION_JSON)
public ResponseEntity<Void> parseKeyList(keyList keyList) {
return new ResponseEntity<Void>(HttpStatus.OK);
}
with the simple class
#XmlRootElement
public class keyList {
private String keys;
public String getKeys() {
return keys;
}
public void setKeys(String keys) {
this.keys = keys;
}
}
And I am sending this simple JSON post:
{"keys": "This is my key list"}
but I am getting null in the keys.
As requested the original dispatcher-servlet:
<context:component-scan base-package="com.api" />
<!-- <mvc:resources mapping="/*" location="/WEB-INF/pages/" /> -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
You haven't attached your keyList param to the request body, change the method to:
#RequestMapping(value = "/keys", method = RequestMethod.POST)
#Consumes(MediaType.APPLICATION_JSON)
public ResponseEntity<Void> parseKeyList(#RequestBody keyList keyList) {
return new ResponseEntity<Void>(HttpStatus.OK);
}
Btw.: Class keyList should rather be KeyList (with big K).
Also there is (look at the chat) a
<mvc:annotation-driven/>
missing from your dispatcher-servlet.xml. That's the one that is registering the jackson mapper/marshaller.

Getting HTTP status 400 - The request sent by the client was syntactically incorrect: using curl to post/put json request

I am working with spring MVC with both xml/json objects and I am getting following error:
HTTP Status 400 - The request sent by the client was syntactically incorrect ().
This is my controller.
#RequestMapping(method=RequestMethod.POST, value="/emp")
public #ResponseBody EmployeeList addEmp(#RequestBody Employee e) {
employeeDS.add(e);
List<Employee> employees = employeeDS.getAll();
EmployeeList list = new EmployeeList(employees);
return list;
}
#RequestMapping(method=RequestMethod.PUT, value="/emp/{id}")
public #ResponseBody EmployeeList updateEmp(#RequestBody Employee e, #PathVariable String id) {
employeeDS.update(e);
List<Employee> employees = employeeDS.getAll();
EmployeeList list = new EmployeeList(employees);
return list;
}
I am trying to send JSON object using curl:
curl -v -X PUT -HContent-type:application/json --data '{"id":3,"name":"guest","email":"guest#ibm.com"}' http://localhost:8080/rest/service/emp/1
curl -v -X POST -HContent-type:application/json --data '{"id":3,"name":"guest","email":"guest#ibm.com"}' http://localhost:8080/rest/service/emp
I have added following jackson dependencies in pom file:
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-jaxrs</artifactId>
<version>1.9.12</version>
</dependency>
I have also added following configuration in my servlet-dispatcher.xml:
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="1" />
<property name="mediaTypes">
<map>
<entry key="xml" value="application/xml"/>
<entry key="json" value="application/json" />
<!--entry key="html" value="text/html"/-->
</map>
</property>
<property name="defaultViews">
<list>
<!-- JSON View -->
<bean
class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
</bean>
<!-- JAXB XML View -->
<bean class="org.springframework.web.servlet.view.xml.MarshallingView">
<constructor-arg>
<bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>com.mkyong.common.bean.Employee</value>
<value>com.mkyong.common.bean.EmployeeList</value>
</list>
</property>
</bean>
</constructor-arg>
</bean>
</list>
</property>
<property name="ignoreAcceptHeader" value="false" />
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="order" value="2" />
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
</list>
</property>
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="marshallingConverter" />
<ref bean="jsonConverter" />
</list>
</property>
</bean>
<bean id="marshallingConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<property name="marshaller" ref="jaxbMarshaller" />
<property name="unmarshaller" ref="jaxbMarshaller" />
<property name="supportedMediaTypes" value="application/xml"/>
</bean>
<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes" value="application/json" />
</bean>
Here is how the Employee class looks like:
package com.mkyong.common.bean;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement(name="employee")
public class Employee {
private long id;
private String name;
private String email;
public Employee() {}
public Employee(long id, String name, String email) {
this.id = id;
this.name = name;
this.email = email;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
I have the following jars in my lib:
aopalliance-1.0.jar
aspectjrt-1.5.3.jar
aspectjweaver-1.5.3.jar
axiom-api-1.2.7.jar
commons-lang-2.5.jar
commons-logging-1.1.1.jar
jackson-core-asl-1.9.13.jar
jackson-jaxrs-1.9.12.jar
jackson-mapper-asl-1.9.13.jar
jaxb-api-2.1.jar
jaxb-impl-2.2.jar
joda-time-1.6.2.jar
opensaml-2.5.1-1.jar
openws-1.4.2-1.jar
slf4j-api-1.7.2.jar
spring-aop-3.1.3.RELEASE.jar
spring-beans-3.2.2.RELEASE.jar
spring-context-3.2.2.RELEASE.jar
spring-context-support-3.1.3.RELEASE.jar
spring-core-3.2.2.RELEASE.jar
spring-expression-3.2.2.RELEASE.jar
spring-jdbc-3.2.2.RELEASE.jar
spring-oxm-3.2.2.RELEASE.jar
spring-security-config-3.1.3.RELEASE.jar
spring-security-core-3.1.3.RELEASE.jar
spring-security-web-3.1.3.RELEASE.jar
spring-tx-3.1.3.RELEASE.jar
spring-web-3.2.2.RELEASE.jar
spring-webmvc-3.1.3.RELEASE.jar
spring-ws-core-2.1.2.RELEASE.jar
spring-ws-security-2.1.2.RELEASE.jar
spring-xml-2.1.2.RELEASE.jar
stax-api-1.0-2.jar
wsdl4j-1.6.1.jar
wss4j-1.6.5.jar
xmlsec-1.5.1.jar
xmlsec-2.0.jar
xmltooling-1.3.2-1.jar
xws-security-1.3.1.jar
My GET works just fine for both XML and JSON:
#RequestMapping(method=RequestMethod.GET, value="/emp/{id}", headers="Accept=application/xml, application/json")
public #ResponseBody Employee getEmp(#PathVariable String id) {
Employee e = employeeDS.get(Long.parseLong(id));
return e;
}
for following curl command:
curl -HAccept:application/xml http://localhost:8080/rest/service/emp/1
curl -HAccept:application/json http://localhost:8080/rest/service/emp/1
I have actually created your project. Using eclipse in windows on tomcat. I used spring 3.2.4 (but I don't think it makes a difference).
I encountered the same problem as you when I sent it incorrect json so I think it is your curl command which is wrong. Are you on windows? If so you must escape your quotes. I sent it the following:
C:\> curl -v -X POST -HContent-type:application/json -d "{\"id\":3,\"name\":\"guest\",\"email\":\"guest#ibm.com\"}" http://localhost:8080/HelperSpringMVC/emp
* Adding handle: conn: 0x6a3400
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x6a3400) send_pipe: 1, recv_pipe: 0
* About to connect() to localhost port 8080 (#0)
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> POST /HelperSpringMVC/emp HTTP/1.1
> User-Agent: curl/7.32.0
> Host: localhost:8080
> Accept: */*
> Content-type:application/json
> Content-Length: 47
>
* upload completely sent off: 47 out of 47 bytes
< HTTP/1.1 200 OK
* Server Apache-Coyote/1.1 is not blacklisted
< Server: Apache-Coyote/1.1
< Content-Type: application/json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Wed, 11 Sep 2013 21:07:47 GMT
<
[{"id":10,"name":"john","email":"email"},{"id":3,"name":"guest","email":"guest#ibm.com"}]
* Connection #0 to host localhost left intact
Note my controller is a bit different it is here:
#Controller
public class controller {
#RequestMapping(method=RequestMethod.POST, value="/emp")
public #ResponseBody List<Employee> addEmp(#RequestBody Employee e, BindingResult results) {
if (results.hasErrors()) {
return new ArrayList<Employee>();
}
List<Employee> list = new ArrayList<Employee>();
list.add(new Employee(10, "john", "email"));
list.add(e);
return list;
}
....
Shout if this doesn't help or you want any of my files.
I had a similar error when I posted to a normal http controller. My problem happened when the binding failed, the solution was that I needed to include BindingResult result. You could try adding that after your model for POST.
I am not sure if it's the syntax of curl, but my curl command is as follow and works for me (your -H option part is different from mine)
curl -v -X PUT -H "Content-Type:application/json" -d '{"id":123, "name":"User123"}' http://localhost:8080/restful/user
Here is how Employee class look like
package com.mkyong.common.bean;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement(name="employee")
public class Employee {
private long id;
private String name;
private String email;
public Employee() {}
public Employee(long id, String name, String email) {
this.id = id;
this.name = name;
this.email = email;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Here is my controller method after adding BindingResult
#RequestMapping(method=RequestMethod.PUT, value="/emp/{id}")
public #ResponseBody EmployeeList updateEmp(#RequestBody Employee e, #PathVariable String id, BindingResult result) {
if(result.hasErrors()) {
System.out.println("Error ::: " +result.toString());
}
employeeDS.update(e);
List<Employee> employees = employeeDS.getAll();
EmployeeList list = new EmployeeList(employees);
return list;
}

Flex 4, Spring 3 BlazeDS4 mySQL dbcp query excecution

I've implemented the SpringFlex 1.5 with a connection to mySQL DB
and it works and retrieves data initially but when I enter an invalid
data that doesn't exist in my table the application seems to freeze
and not even entering valid data works after that and I need to
stop and start Tomcat again to get it working again
applicationContext.xml
<bean id="authToLeaveService" class="com.model.atl.AuthToLeaveServiceImpl">
<constructor-arg ref="dataSource" />
</bean>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://dxfcm:3306/wookie?autoReconnect=true&zeroDateTimeBehavior=convertToNull"/>
<property name="username" value="darth" />
<property name="password" value="vader" />
<property name="validationQuery" value="SELECT 1"/>
</bean>
MyView
<fx:Declarations>
<s:ChannelSet id="cs">
<s:AMFChannel url="http://localhost:8400/flexspring/messagebroker/amf"/>
</s:ChannelSet>
<s:RemoteObject id="atlRO" channelSet="{cs}" destination="authToLeaveService"/>
</fx:Declarations>
[Bindable]
private var job:AtlJob;
private function onEnter(event:FlexEvent):void
{
var token:AsyncToken = atlRO.findByBarcode(this.txtBarcode.text);
token.addResponder(new AsyncResponder(onResult, onFault));
}
private function onResult(event:ResultEvent, token:Object):void
{ job = event.result as AtlJob; }
private function onFault(event:FaultEvent, token:Object):void
{ }
<s:TextInput id="txtBarcode" x="23" y="60" width="218"
enter="onEnter(event)" maxChars="16"/>
AltJob.as
[Bindable]
[RemoteClass (alias="com.model.atl.AtlJob")]
public class AtlJob
{
public var barcode:String;
public var pieces:int;
public var customerName:String;
}
AtlJob.java
package com.model.atl;
public class AtlJob implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private String barcode;
private String customerName;
private int pieces;
public AtlJob() { }
public AtlJob(String barcode, int pieces, String customerName) {
this.barcode = barcode;
this.customerName = customerName;
this.pieces= pieces;
}
Getters and Setters defined
#Service("authToLeaveService")
#RemotingDestination(channels = { "my-amf", "my-secure-amf" })
public class AuthToLeaveServiceImpl implements AuthToLeaveService {
private final DataSource dataSource;
public AuthToLeaveServiceImpl(DataSource dataSource) {
this.dataSource = dataSource; }
#RemotingInclude
public AtlJob findByBarcode(String barcode) {
AtlJob job = new AtlJob();
Connection con = null;
final String sql = "SELECT * FROM atl_job WHERE card_barcode = ?";
try {
con = this.dataSource.getConnection();
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, barcode);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
job.setBarcode(rs.getString("barcode"));
job.setPieces(rs.getInt("pieces"));
job.setCustomerName(rs.getString("customerName"));}
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
if (con!=null) {try { con.close();
} catch (SQLException e) {
e.printStackTrace();
} }} return job; }
You are not throwing any exceptions if the record is not found like here
...
if (rs.next()) {
job.setBarcode(rs.getString("barcode"));
job.setPieces(rs.getInt("pieces"));
job.setCustomerName(rs.getString("customerName"));
}
// Put an else clause here and throw exception
else
{
throw new Exception("Record Not found");
}
Try showing caught exception in your faultMethod ..
private function onFault(event:FaultEvent, token:Object):void
{
//Handle Fault event here Put some Alert here
}

Spring MVC 3 + JSON

I'm trying to use Spring MVC with JSON. It works great when a return an object from the controller, but when I try to make an AJAX call passing a custom object as parameter I'm getting HTTP 415 error.
My spring-servlet.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<tx:annotation-driven />
<context:annotation-config/>
<context:component-scan
base-package="com.sommer.controller" />
<tx:annotation-driven transaction-manager="transactionManager"/>
<context:component-scan base-package="com.sommer.service" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8"/>
</bean>
<!-- ========= [ADDED FOR JSON SUPPORT] ========= -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonConverter" />
</list>
</property>
</bean>
<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes" value="application/json" />
</bean>
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="es"/>
</bean>
<bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<ref bean="localeChangeInterceptor" />
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/sommer"/>
<property name="username" value="root"/>
<property name="password" value="master"/>
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:dataSource-ref="dataSource"
p:jpaVendorAdapter-ref="jpaAdapter">
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
</property>
<property name="persistenceUnitName" value="sommerPersistenceUnit"></property>
</bean>
<bean id="jpaAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
p:database="MYSQL"
p:showSql="true"
p:generateDdl="true"/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="entityManagerFactory"/>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
My controller:
#RequestMapping(value="/editJSON2",headers={"content-type=application/json,application/xml,application/x-www-form-urlencoded"})
public #ResponseBody ActionResult editJSON2(#RequestBody CustomerTO toEdit){
return new ActionResult(toEdit);
}
Classes:
public class ActionResult {
private Boolean success;
private String message;
private Object object;
public ActionResult(){
this.success = true;
this.object = null;
this.message = null;
}
public ActionResult(Boolean isSuccess,Object obj, String message){
this.success = isSuccess;
this.object = obj;
this.message = message;
}
public ActionResult(Object obj){
this.success = true;
this.object = obj;
this.message = "";
}
public ActionResult(String message){
this.success = false;
this.object = null;
this.message = message;
}
public void setError(String msg){
this.success = false;
this.message = msg;
}
public Boolean getSuccess() {
return success;
}
public void setSuccess(Boolean success) {
this.success = success;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Object getObject() {
return object;
}
public void setObject(Object object) {
this.object = object;
}
}
public class CustomerTO {
private Long id;
private String name;
private String email;
private TestObject[] items;
public TestObject[] getItems() {
return items;
}
public void setItems(TestObject[] items) {
this.items = items;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public DocumentType getDocumentType() {
return documentType;
}
public void setDocumentType(DocumentType documentType) {
this.documentType = documentType;
}
public String getDocumentNumber() {
return documentNumber;
}
public void setDocumentNumber(String documentNumber) {
this.documentNumber = documentNumber;
}
private String surname;
private String sex;
private DocumentType documentType;
private String documentNumber;
public CustomerTO() {
}
public CustomerTO(Customer customer) {
this.id = customer.getId();
this.documentNumber = customer.getDocumentNumber();
this.documentType = customer.getDocumentType();
this.name = customer.getName();
this.surname = customer.getSurname();
this.sex = Sex.MALE.equals(customer.getSex())?"M":"F";
this.email = customer.getEmail();
this.items = new TestObject[1];
TestObject tio = new TestObject();
tio.setText("ITEM !");
this.items[0] = tio;
}
My ajax call:
var currentCustomer = {
'id': $('#id').val()
,'name' :$('#name').val()
,'surname' :$('#surname').val()
,'documentType' :$('#documentType').val()
,'documentNumber' :$('#documentNumber').val()
,'sex' :$('#sex').val()
,'email' :$('#email').val()
};
// Send the request
$.post('editJSON2.html', {toEdit:currentCustomer}, function(response) {
alert('OK');
}, 'json');
The problem I think is here:
public #ResponseBody ActionResult editJSON2(#RequestBody CustomerTO toEdit)
I think #ResquestBody is not working for me. I also have
#RequestMapping("/editJSON")
public #ResponseBody ActionResult editJSON(#RequestParam(required=false) Long customerId){
CustomerTO toEdit = customerId!=null ? new CustomerTO(customerService.getById(customerId)):new CustomerTO();
return new ActionResult(toEdit);
}
And when I call it I have no problem.
This is information I collected from firebug:
Parámetrosapplication/x-www-form-urlencoded
toEdit[documentNumber] 36466
toEdit[documentType] DNI
toEdit[email] jpruizmdq#hotmail.com
toEdit[id] 2
toEdit[name] John
toEdit[surname] Doe
Código fuente
toEdit%5Bid%5D=2&toEdit%5Bname%5D=John&toEdit%5Bsurname%5D=Doe&toEdit%5BdocumentType%5D=DNI&toEdit%5BdocumentNumber%5D=36466&toEdit%5Bemail%5D=jpruizmdq%40hotmail.com
It's no tot working because content type of your request is application/x-www-form-urlencoded
and it supposed to be application/json
try to send it with Jquery the following way:
$.ajax({
type: "POST",
url: "someurl",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: "{id: '" + someId + "'}",
success: function(json) {
}};
Thanks! Problem solved. Here is the code
#RequestMapping(value="/editJSON2")
public #ResponseBody ActionResult editJSON2(#RequestBody CustomerTO toEdit){
return new ActionResult(toEdit);
}
ajaxCall('editJSON2.html',JSON.stringify(currentCustomer),function(valor){
alert('OK');
});
function ajaxCall(url,data,callback,onError){
jQuery.ajax({
url:url
,dataType: 'json'
,data:data
,type: "POST"
,contentType: "application/json; charset=utf-8"
,success:function(actionResult){
-----------
}
,error:function(jqXHR, textStatus, errorThrown){
---
}
});
}
It was simple! I added contentType: "application/json; charset=utf-8" and i used JSON.stringify(currentCustomer). With that it worked