我为什么在Springboot应用程序中收到这个异常?

huangapple go评论91阅读模式
英文:

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 
     



&gt; 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=&quot;/createAccount&quot;,method = RequestMethod.POST)
public Accounts createAccount(@RequestBody Accounts value,@RequestHeader(name = &quot;User-Id&quot;,required = true) 
@Pattern(regexp = &quot;^[0-9a-z]{12}&quot;, message = &quot;Please enter valid UserId&quot;)
@Valid @Parameter(name =&quot;User-Id&quot;,description = &quot;user id&quot;) Integer userId) throws AccountCreationException {
	 
	
Accounts AccountCreated = accountService.createAccount(value, userId);
System.out.println(&quot;Dear&quot; + userId + &quot;your Account has been created&quot;);
if(AccountCreated!=null)
{
	int user = value.getUserId();
	System.out.println(&quot;Dear&quot; + user + &quot;your Account has been created&quot;);
}
else
{
	throw new AccountCreationException(&quot;There is an Issue while Creating &quot;
			+ &quot;your account please try after sometime&quot;);
}
	
	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 &amp;&amp; !userId.equals(null))
			{
				data.setUserId(userId);
				created = accountrepo.save(data);
			} 
			else 
			{
				LOG.debug(&quot;Data input is empty or userId is null please check the iputs once again&quot;);
			}
			
		} 
		catch (Exception e) {
			LOG.debug(&quot;Hey there is an Exception while calling the craeteAccount &quot; + 
		&quot;method in AccountsServiceImpl&quot;);
		}
		return created;

	}

}

Repository Interface

@Repository
public interface AccountsRespository extends CrudRepository&lt;Accounts,String&gt; 
{
	//@SuppressWarnings(&quot;unchecked&quot;)
	//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 &#39;dispatcherServlet&#39;
[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

请尝试使用以下注解来设置您的主键:

  1. 通用生成器注解
  2. @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:

  1. the Generic generator annotation
  2. @GeneratedValue(generator = "uuid")

Example:

@Id
@GeneratedValue(generator = &quot;uuid&quot;)
@GenericGenerator(name = &quot;uuid&quot;, strategy = &quot;uuid2&quot;)
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&lt;Accounts, Long&gt; 
{

}

huangapple
  • 本文由 发表于 2023年7月23日 18:41:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76747800.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定