Unable to catch exception in Controller in Spring Data -



Unable to catch exception in Controller in Spring Data -

i've spring mvc application @controller, @service , @repository components (all generated spring roo). i'm trying grab constraintviolationexception in controller i'm not able to. i'm trying handle exceptions @ controller level problem when create phone call repository service , if exception occurs, i'm not getting flow within grab block in controller. i'm unable figure out why happening. configured spring? in advance.

controller:

@requestmapping(method = requestmethod.post, headers = "accept=application/json") public responseentity<string> createfromjson(@requestbody individualproviderdto individualproviderdto) { httpheaders headers = new httpheaders(); headers.add("content-type", "application/json"); httpstatus httpstatus = null; seek { individualproviderservice.saveindividualprovider(individualproviderdto); httpstatus = httpstatus.created; } grab (constraintviolationexception e) { httpstatus = httpstatus.conflict; } homecoming new responseentity<string>(headers, httpstatus); }

service:

public void saveindividualprovider(individualproviderdto individualproviderdto) { individualprovider individualprovider = individualproviderdtotoindividualprovidermapper.map(individualproviderdto); individualproviderrepository.save(individualprovider); }

stack:

2014-06-24 09:15:36,117 ["http-bio-9001"-exec-6] error org.hibernate.engine.jdbc.spi.sqlexceptionhelper - duplicate entry 'xxx@gmail.com' key 'uk_thwi4ibpanesigdfbx3iqt0bg' servlet.service() servlet [bham] in context path [/cds] threw exception [request processing failed; nested exception org.springframework.orm.jpa.jpasystemexception: org.hibernate.exception .constraintviolationexception: not execute statement; nested exception javax.persistence.persistenceexception: org.hibernate.exception.constraintviolationexception: not execute statemen t] root cause com.mysql.jdbc.exceptions.jdbc4.mysqlintegrityconstraintviolationexception: duplicate entry 'xxx@gmail.com' key 'uk_thwi4ibpanesigdfbx3iqt0bg' @ sun.reflect.nativeconstructoraccessorimpl.newinstance0(native method) @ sun.reflect.nativeconstructoraccessorimpl.newinstance(nativeconstructoraccessorimpl.java:62) @ sun.reflect.delegatingconstructoraccessorimpl.newinstance(delegatingconstructoraccessorimpl.java:45) @ java.lang.reflect.constructor.newinstance(constructor.java:408) @ com.mysql.jdbc.util.handlenewinstance(util.java:411) @ com.mysql.jdbc.util.getinstance(util.java:386) @ com.mysql.jdbc.sqlerror.createsqlexception(sqlerror.java:1041) @ com.mysql.jdbc.mysqlio.checkerrorpacket(mysqlio.java:4237) @ com.mysql.jdbc.mysqlio.checkerrorpacket(mysqlio.java:4169) @ com.mysql.jdbc.mysqlio.sendcommand(mysqlio.java:2617) @ com.mysql.jdbc.mysqlio.sqlquerydirect(mysqlio.java:2778) @ com.mysql.jdbc.connectionimpl.execsql(connectionimpl.java:2825) @ com.mysql.jdbc.preparedstatement.executeinternal(preparedstatement.java:2156) @ com.mysql.jdbc.preparedstatement.executeupdate(preparedstatement.java:2441) @ com.mysql.jdbc.preparedstatement.executeupdate(preparedstatement.java:2366) @ com.mysql.jdbc.preparedstatement.executeupdate(preparedstatement.java:2350) @ org.apache.commons.dbcp.delegatingpreparedstatement.executeupdate(delegatingpreparedstatement.java:105) @ org.apache.commons.dbcp.delegatingpreparedstatement.executeupdate(delegatingpreparedstatement.java:105) @ org.hibernate.engine.jdbc.internal.resultsetreturnimpl.executeupdate(resultsetreturnimpl.java:187) @ org.hibernate.id.identitygenerator$getgeneratedkeysdelegate.executeandextract(identitygenerator.java:96) @ org.hibernate.id.insert.abstractreturningdelegate.performinsert(abstractreturningdelegate.java:58) @ org.hibernate.persister.entity.abstractentitypersister.insert(abstractentitypersister.java:3032) @ org.hibernate.persister.entity.abstractentitypersister.insert(abstractentitypersister.java:3558) @ org.hibernate.action.internal.entityidentityinsertaction.execute(entityidentityinsertaction.java:98) @ org.hibernate.engine.spi.actionqueue.execute(actionqueue.java:490) @ org.hibernate.engine.spi.actionqueue.addresolvedentityinsertaction(actionqueue.java:195) @ org.hibernate.engine.spi.actionqueue.addinsertaction(actionqueue.java:179) @ org.hibernate.engine.spi.actionqueue.addaction(actionqueue.java:214) @ org.hibernate.event.internal.abstractsaveeventlistener.addinsertaction(abstractsaveeventlistener.java:324) @ org.hibernate.event.internal.abstractsaveeventlistener.performsaveorreplicate(abstractsaveeventlistener.java:288) @ org.hibernate.event.internal.abstractsaveeventlistener.performsave(abstractsaveeventlistener.java:194) @ org.hibernate.event.internal.abstractsaveeventlistener.savewithgeneratedid(abstractsaveeventlistener.java:125) @ org.hibernate.jpa.event.internal.core.jpapersisteventlistener.savewithgeneratedid(jpapersisteventlistener.java:84) @ org.hibernate.event.internal.defaultpersisteventlistener.entityistransient(defaultpersisteventlistener.java:206) @ org.hibernate.event.internal.defaultpersisteventlistener.onpersist(defaultpersisteventlistener.java:149) @ org.hibernate.event.internal.defaultpersisteventlistener.onpersist(defaultpersisteventlistener.java:75) @ org.hibernate.internal.sessionimpl.firepersist(sessionimpl.java:811) @ org.hibernate.internal.sessionimpl.persist(sessionimpl.java:784) @ org.hibernate.internal.sessionimpl.persist(sessionimpl.java:789) @ org.hibernate.jpa.spi.abstractentitymanagerimpl.persist(abstractentitymanagerimpl.java:1181) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:483) @ org.springframework.orm.jpa.extendedentitymanagercreator$extendedentitymanagerinvocationhandler.invoke(extendedentitymanagercreator.java:342) @ com.sun.proxy.$proxy97.persist(unknown source) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:483) @ org.springframework.orm.jpa.sharedentitymanagercreator$sharedentitymanagerinvocationhandler.invoke(sharedentitymanagercreator.java:262) @ com.sun.proxy.$proxy96.persist(unknown source) @ org.springframework.data.jpa.repository.support.simplejparepository.save(simplejparepository.java:369) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:483) @ org.springframework.data.repository.core.support.repositoryfactorysupport$queryexecutormethodinterceptor.executemethodon(repositoryfactorysupport.java:358) @ org.springframework.data.repository.core.support.repositoryfactorysupport$queryexecutormethodinterceptor.invoke(repositoryfactorysupport.java:343) @ org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:179) @ org.springframework.transaction.interceptor.transactioninterceptor$1.proceedwithinvocation(transactioninterceptor.java:98) @ org.springframework.transaction.interceptor.transactionaspectsupport.invokewithintransaction(transactionaspectsupport.java:262) @ org.springframework.transaction.interceptor.transactioninterceptor.invoke(transactioninterceptor.java:95) @ org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:179) @ org.springframework.dao.support.persistenceexceptiontranslationinterceptor.invoke(persistenceexceptiontranslationinterceptor.java:136) @ org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:179) @ org.springframework.data.jpa.repository.support.lockmoderepositorypostprocessor$lockmodepopulatingmethodintercceptor.invoke(lockmoderepositorypostprocessor.java:105) @ org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:179) @ org.springframework.aop.interceptor.exposeinvocationinterceptor.invoke(exposeinvocationinterceptor.java:92) @ org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:179) @ org.springframework.aop.framework.jdkdynamicaopproxy.invoke(jdkdynamicaopproxy.java:207) @ com.sun.proxy.$proxy98.save(unknown source) @ com.mycompany.bham.service.provider.individualproviderserviceimpl.saveindividualprovider(individualproviderserviceimpl.java:82) @ com.mycompany.bham.web.individualprovidercontroller.createfromjson(individualprovidercontroller.java:88) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:483)

you may want utilize exception handler of spring http://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc

here illustration code:

@controller public class exceptionhandlingcontroller { // @requesthandler methods ... // exception handling methods // convert predefined exception http status code @responsestatus(value=httpstatus.conflict, reason="data integrity violation") // 409 @exceptionhandler(dataintegrityviolationexception.class) public void conflict() { // nil } // specify name of specific view used display error: @exceptionhandler({sqlexception.class,dataaccessexception.class}) public string databaseerror() { // nil do. returns logical view name of error page, passed // view-resolver(s) in usual way. // note exception _not_ available view (it not added // model) see "extending exceptionhandlerexceptionresolver" below. homecoming "databaseerror"; } // total command - setup model , homecoming view name yourself. or consider // subclassing exceptionhandlerexceptionresolver (see below). @exceptionhandler(exception.class) public modelandview handleerror(httpservletrequest req, exception exception) { logger.error("request: " + req.getrequesturl() + " raised " + exception); modelandview mav = new modelandview(); mav.addobject("exception", exception); mav.addobject("url", req.getrequesturl()); mav.setviewname("error"); homecoming mav; } }

spring spring-mvc spring-data spring-roo spring-data-jpa

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -