Issue
Method to insert data
public String home() {
try
{
MongoData data=new MongoData();
data.setDate(new Date());
data.setName("frji");
//mongoTemplate.insert(data);
mongoTemplate.save(data);
}
catch (DuplicateKeyException e) {
}
catch (MongoException e) {
}
return "home";
}
Entity class
@Document(collection="integer_test")
public class MongoData {
@Id
private String id;
private Date date;
@Indexed(unique=true)
private String name;
@Override
public String toString() {
return "MongoData [date=" + date + ", id=" + id + "]";
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
But it does not catch the exception. it will show on user interface.Then how can i catch this exception in java end.
com.mongodb.MongoException$DuplicateKey: { "serverUsed" : "192.168.1.113:27017" , "ok" : 1 , "n" : 0 , "err" : "E11000 duplicate key error index: jmailer_digiengage.integer_test.$name dup key: { : \"frji\" }" , "code" : 11000} at com.mongodb.CommandResult.getWriteException(CommandResult.java:88) at com.mongodb.CommandResult.getException(CommandResult.java:79) at com.mongodb.DBCollectionImpl.translateBulkWriteException(DBCollectionImpl.java:415) at com.mongodb.DBCollectionImpl.insertImpl(DBCollectionImpl.java:210) at com.mongodb.DBCollection.insert(DBCollection.java:176) at com.mongodb.DBCollection.insert(DBCollection.java:161) at com.mongodb.DBCollection.insert(DBCollection.java:107) at com.mongodb.DBCollection.save(DBCollection.java:1214) at com.mongodb.DBCollection.save(DBCollection.java:1179) at org.springframework.data.mongodb.core.MongoTemplate$11.doInCollection(MongoTemplate.java:1106) at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:479) at org.springframework.data.mongodb.core.MongoTemplate.saveDBObject(MongoTemplate.java:1101) at org.springframework.data.mongodb.core.MongoTemplate.doSave(MongoTemplate.java:1034) at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:981) at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:969) at com.mongo.test.HomeController.home(HomeController.java:62) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:111) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:806) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:729) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) at javax.servlet.http.HttpServlet.service(HttpServlet.java:635) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:341) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:783) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:798) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1441) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:745)
Solution
You can create your HandlerExceptionResolver to handle and show the user friendly message to the UI.
How to create HandlerExceptionResolver
Update : If you just want to handle the exception then write org.springframework.dao.DuplicateKeyException
in the catch block.
try{
//TODO
} catch(org.springframework.dao.DuplicateKeyException e){
//TODO - Do what you want
}
Answered By - Mehraj Malik