英文:
Why I am getting this Exception in Springboot application?
问题
@RestController
public class CustomerAccountsControllerImpl {
@Autowired
private AccountsService accountService;
@RequestMapping(value="/createAccount", method = RequestMethod.POST)
public Accounts createAccount(@RequestBody Accounts value, @RequestHeader(name = "User-Id", required = true)
@Pattern(regexp = "^[0-9a-z]{12}", message = "请输入有效的用户ID")
@Valid @Parameter(name ="User-Id", description = "用户ID") Integer userId) throws AccountCreationException {
Accounts AccountCreated = accountService.createAccount(value, userId);
System.out.println("亲爱的" + userId + "您的账户已创建");
if(AccountCreated!=null)
{
int user = value.getUserId();
System.out.println("亲爱的" + user + "您的账户已创建");
}
else
{
throw new AccountCreationException("创建您的账户时出现问题,请稍后再试");
}
return AccountCreated;
}
}
@Service
public class AccountsServiceImpl implements AccountsService {
@Autowired
private AccountsRespository accountrepo;
private static final Logger LOG = LoggerFactory.getLogger(AccountsServiceImpl.class);
Accounts created = null;
@Override
public Accounts createAccount(Accounts data, Integer userId) {
try {
if (data != null && !userId.equals(null))
{
data.setUserId(userId);
created = accountrepo.save(data);
}
else
{
LOG.debug("输入数据为空或用户ID为空,请再次检查输入");
}
}
catch (Exception e) {
LOG.debug("在调用AccountsServiceImpl的createAccount方法时出现异常");
}
return created;
}
}
@Repository
public interface AccountsRespository extends CrudRepository<Accounts,String>
{
//public Accounts save(Accounts craeteAccount);
}
@Entity
@Data
public class Accounts {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private String accountNumber;
@Column
private int transactionNumber;
@Column
private int userId;
@Column
private int upiIdLimit;
@Column
private double accountBalance;
@Column
private String transactionLimitPerDay;
}
server.port = 8083
spring.mvc.pathmatch.matching-strategy=ant-path-matcher
spring.datasource.username=user
spring.datasource.password=usernames
spring.datasource.url=jdbc:mysql://localhost:3306/ICICIBANKBHIMUPIDB
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.application.name=Accounts
[2m2023-07-23 15:35:53.024[0;39m [32m INFO[0;39m [35m11284[0;39m [2m---[0;39m [2m[nio-8083-exec-2][0;39m [36mo.s.web.servlet.DispatcherServlet [0;39m [2m:[0;39m 初始化Servlet 'dispatcherServlet'
[2m2023-07-23 15:35:53.026[0;39m [32m INFO[0;39m [35m11284[0;39m [2m---[0;39m [2m[nio-8083-exec-2][0;39m [36mo.s.web.servlet.DispatcherServlet [0;39m [2m:[0;39m 在2毫秒内完成初始化
亲爱的543214466您的账户已创建
[2m2023-07-23 15:35:53.155[0;39m [31mERROR[0;39m [35m11284[0;39m [2m---[0;39m [2m[nio-8083-exec-2][0;39m [36mo.a.c.c.C.[.[.[/].[dispatcherServlet] [0;39m [2m:[0;39m 请求处理失败;嵌套异常是com.icicibank.accounts.Exceptions.AccountCreationException: 创建您的账户时出现问题,请稍后再试
com.icicibank.accounts.Exceptions.AccountCreationException: 创建您的账户时出现问题,请稍后再试
at com.icicibank.accounts.controller.CustomerAccountsControllerImpl.createAccount(CustomerAccountsControllerImpl.java:38) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-5.3.29.jar:5.3.29]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150) ~[spring-web-5.3.29.jar:5.3.29]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117) ~[spring-webmvc-5.3.29.jar:5.3.29]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) ~[spring-webmvc-5.3.29.jar:5.3.29]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808) ~[spring-webmvc-5.3.29.jar:5.3.29]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.3.29.jar:5.3.29]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1072) ~[spring-webmvc-5.3.29.jar:5.3.29]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:965) ~[spring-webmvc-5.3.29.jar:5.3.29]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.3.29.jar:5.3.29]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) ~[spring-webmvc-5.3.29.jar:5.3.29]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:555) ~[tomcat-embed-core-9.0.78.jar:4.0
<details>
<summary>英文:</summary>
-How to Solve this Issue
> How can I solve this Issue ? what is the exact mistake I did, I am not able to identify ?
`ControllerClass`
@RestController
public class CustomerAccountsControllerImpl {
@Autowired
private AccountsService accountService;
@RequestMapping(value="/createAccount",method = RequestMethod.POST)
public Accounts createAccount(@RequestBody Accounts value,@RequestHeader(name = "User-Id",required = true)
@Pattern(regexp = "^[0-9a-z]{12}", message = "Please enter valid UserId")
@Valid @Parameter(name ="User-Id",description = "user id") Integer userId) throws AccountCreationException {
Accounts AccountCreated = accountService.createAccount(value, userId);
System.out.println("Dear" + userId + "your Account has been created");
if(AccountCreated!=null)
{
int user = value.getUserId();
System.out.println("Dear" + user + "your Account has been created");
}
else
{
throw new AccountCreationException("There is an Issue while Creating "
+ "your account please try after sometime");
}
return AccountCreated;
}
}
`Service Implementation`
```
package com.icicibank.accounts.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.icicibank.accounts.model.Accounts;
import com.icicibank.accounts.repository.AccountsRespository;
@Service
public class AccountsServiceImpl implements AccountsService {
@Autowired
private AccountsRespository accountrepo;
private static final Logger LOG = LoggerFactory.getLogger(AccountsServiceImpl.class);
Accounts created = null;
@Override
public Accounts createAccount(Accounts data, Integer userId) {
try {
if (data != null && !userId.equals(null))
{
data.setUserId(userId);
created = accountrepo.save(data);
}
else
{
LOG.debug("Data input is empty or userId is null please check the iputs once again");
}
}
catch (Exception e) {
LOG.debug("Hey there is an Exception while calling the craeteAccount " +
"method in AccountsServiceImpl");
}
return created;
}
}
Repository Interface
@Repository
public interface AccountsRespository extends CrudRepository<Accounts,String>
{
//@SuppressWarnings("unchecked")
//public Accounts save(Accounts craeteAccount);
}
Model Class
@Entity
@Data
public class Accounts {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private String accountNumber;
@Column
private int transactionNumber;
@Column
private int userId;
@Column
private int upiIdLimit;
@Column
private double accountBalance;
@Column
private String transactionLimitPerDay;
}
application.properties file
server.port = 8083
spring.mvc.pathmatch.matching-strategy=ant-path-matcher
spring.datasource.username=user
spring.datasource.password=usernames
spring.datasource.url=jdbc:mysql://localhost:3306/ICICIBANKBHIMUPIDB
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.application.name=Accounts
Log File
[2m2023-07-23 15:35:53.024[0;39m [32m INFO[0;39m [35m11284[0;39m [2m---[0;39m [2m[nio-8083-exec-2][0;39m [36mo.s.web.servlet.DispatcherServlet [0;39m [2m:[0;39m Initializing Servlet 'dispatcherServlet'
[2m2023-07-23 15:35:53.026[0;39m [32m INFO[0;39m [35m11284[0;39m [2m---[0;39m [2m[nio-8083-exec-2][0;39m [36mo.s.web.servlet.DispatcherServlet [0;39m [2m:[0;39m Completed initialization in 2 ms
Dear543214466your Account has been created
[2m2023-07-23 15:35:53.155[0;39m [31mERROR[0;39m [35m11284[0;39m [2m---[0;39m [2m[nio-8083-exec-2][0;39m [36mo.a.c.c.C.[.[.[/].[dispatcherServlet] [0;39m [2m:[0;39m Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is com.icicibank.accounts.Exceptions.AccountCreationException: There is an Issue while Creating your account please try after sometime] with root cause
Here is the Log File
Root Cause in Log File
com.icicibank.accounts.Exceptions.AccountCreationException: There is an Issue while Creating your account please try after sometime
at com.icicibank.accounts.controller.CustomerAccountsControllerImpl.createAccount(CustomerAccountsControllerImpl.java:38) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-5.3.29.jar:5.3.29]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150) ~[spring-web-5.3.29.jar:5.3.29]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117) ~[spring-webmvc-5.3.29.jar:5.3.29]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) ~[spring-webmvc-5.3.29.jar:5.3.29]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808) ~[spring-webmvc-5.3.29.jar:5.3.29]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.3.29.jar:5.3.29]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1072) ~[spring-webmvc-5.3.29.jar:5.3.29]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:965) ~[spring-webmvc-5.3.29.jar:5.3.29]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.3.29.jar:5.3.29]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) ~[spring-webmvc-5.3.29.jar:5.3.29]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:555) ~[tomcat-embed-core-9.0.78.jar:4.0.FR]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.3.29.jar:5.3.29]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:623) ~[tomcat-embed-core-9.0.78.jar:4.0.FR]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:209) ~[tomcat-embed-core-9.0.78.jar:9.0.78]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:153) ~[tomcat-embed-core-9.0.78.jar:9.0.78]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) ~[tomcat-embed-websocket-9.0.78.jar:9.0.78]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:178) ~[tomcat-embed-core-9.0.78.jar:9.0.78]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:153) ~[tomcat-embed-core-9.0.78.jar:9.0.78]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.3.29.jar:5.3.29]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.29.jar:5.3.29]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:178) ~[tomcat-embed-core-9.0.78.jar:9.0.78]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:153) ~[tomcat-embed-core-9.0.78.jar:9.0.78]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.3.29.jar:5.3.29]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.29.jar:5.3.29]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:178) ~[tomcat-embed-core-9.0.78.jar:9.0.78]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:153) ~[tomcat-embed-core-9.0.78.jar:9.0.78]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.3.29.jar:5.3.29]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.29.jar:5.3.29]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:178) ~[tomcat-embed-core-9.0.78.jar:9.0.78]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:153) ~[tomcat-embed-core-9.0.78.jar:9.0.78]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:167) ~[tomcat-embed-core-9.0.78.jar:9.0.78]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:90) ~[tomcat-embed-core-9.0.78.jar:9.0.78]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:481) ~[tomcat-embed-core-9.0.78.jar:9.0.78]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:130) ~[tomcat-embed-core-9.0.78.jar:9.0.78]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:93) ~[tomcat-embed-core-9.0.78.jar:9.0.78]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) ~[tomcat-embed-core-9.0.78.jar:9.0.78]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) ~[tomcat-embed-core-9.0.78.jar:9.0.78]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:390) ~[tomcat-embed-core-9.0.78.jar:9.0.78]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:63) ~[tomcat-embed-core-9.0.78.jar:9.0.78]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:926) ~[tomcat-embed-core-9.0.78.jar:9.0.78]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1791) ~[tomcat-embed-core-9.0.78.jar:9.0.78]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52) ~[tomcat-embed-core-9.0.78.jar:9.0.78]
at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) ~[tomcat-embed-core-9.0.78.jar:9.0.78]
at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) ~[tomcat-embed-core-9.0.78.jar:9.0.78]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.78.jar:9.0.78]
at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]
..................................................................................................................................................................................................................
答案1
得分: 1
看起来您的数据未保存到数据存储中。
可能原因:生成的主键不正确。
您的Accounts实体具有ID类型=String。
请尝试使用以下注解来设置您的主键:
- 通用生成器注解
- @GeneratedValue(generator = "uuid")
示例:
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
private String accountNumber;
第二个选项:
您可以使用Integer/Long/UUID类型作为您的主键:
实体字段:
@Id
@GeneratedValue
private Long accountNumber;
存储库(CrudRepository<Accounts, Long>):
@Repository
public interface AccountsRepository extends CrudRepository<Accounts, Long>
{
}
请按照这些注解和示例进行设置。
英文:
It looks like your data was not saved to the data storage.
Probable reason: incorrect generated Primary Key.
Your Accounts entity has an ID type = String.
Please try to use the following annotations for your primary key:
- the Generic generator annotation
- @GeneratedValue(generator = "uuid")
Example:
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
private String accountNumber;
The second option:
<br>You can use Integer/Long/UUID type for your primary key:
Entity field:
@Id
@GeneratedValue
private Long accountNumber;
Repository (CrudRepository<Accounts, Long> );
@Repository
public interface AccountsRespository extends CrudRepository<Accounts, Long>
{
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论