gpt4 book ai didi

java - 如何处理 Spring data mongodb 中的 DuplicateKeyException

转载 作者:行者123 更新时间:2023-11-30 02:22:18 51 4
gpt4 key购买 nike

插入数据的方法

    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";
}

实体类

  @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;
}

}

但是它没有捕获异常。它将显示在用户界面上。那么我如何在java端捕获这个异常。

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)

最佳答案

您可以创建 HandlerExceptionResolver 来处理并向 UI 显示用户友好的消息。

How to create HandlerExceptionResolver

更新:如果您只想处理异常,请在catch block 中写入org.springframework.dao.DuplicateKeyException

try{
//TODO
} catch(org.springframework.dao.DuplicateKeyException e){
//TODO - Do what you want
}

关于java - 如何处理 Spring data mongodb 中的 DuplicateKeyException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46465216/

51 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com