Java Spring RestFull API
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
im trying to learn how restful service works in java. but i came across this problem which i cant seem to figure out. i'm trying to call this controller to which will take data and store it in a database. but the exception i get is something i haven't came across before. if i remove the model class as a parameter and try to return just a basic string it works fine. but my goal is to store what ever data that is passed to the controller in a database.
enter code here
person.java // model class
@Entity
@Table(name = "Person")
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(value = {"createdAt", "updatedAt"}, allowGetters = true)
public class Person implements Serializable {
@JsonProperty("key")
public int getKey() {
return key;
}
public void setKey(int key) {
this.key = key;
}
@JsonProperty("name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@JsonProperty("m")
public int getMotherKey() {
return motherKey;
}
public void setMotherKey(int motherKey) {
this.motherKey = motherKey;
}
@JsonProperty("f")
public int getFatherKey() {
return fatherKey;
}
public void setFatherKey(int fatherKey) {
this.fatherKey = fatherKey;
}
@JsonProperty("dob")
public int getDateOfBirth() {
return DateOfBirth;
}
public void setDateOfBirth(int dateOfBirth) {
DateOfBirth = dateOfBirth;
}
@JsonProperty("g")
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Person() {
}
@NotNull
int key;
@NotBlank
String name;
@NotNull
int motherKey;
@NotNull
int fatherKey;
@NotNull
int DateOfBirth; //19921210->December 10th 1992
@NotBlank
String gender;
**Controller class **
package com.example.FamilyTree;
import java.util.List;
import javax.validation.Valid;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import Repository.TreeRepository;
import exception.ResourceNotFoundException;
import model.Person;
@RestController
@RequestMapping("/api")
public class FamilityTreeController {
TreeRepository treeRepository;
@PostMapping("/addPerson")
public Person addPerson(@Valid @RequestBody Person person) {
return treeRepository.save( person);
}
}
Exception
at
com.example.FamilyTree.FamilityTreeController.addPerson(FamilityTreeController.java:32) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[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:564) ~[na:na]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:189) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:908) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:660) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.13.jar:9.0.13]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490) [tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) [tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) [tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408) [tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:791) [tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1417) [tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.13.jar:9.0.13]
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1135) [na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) [na:na]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.13.jar:9.0.13]
at java.base/java.lang.Thread.run(Thread.java:844) [na:na]
Respository
package Repository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;
import model.Person;
public interface TreeRepository extends JpaRepository<Person, Integer> {
}
Full stack trace from Postman
{
"timestamp": "2019-01-03T14:06:28.971+0000",
"status": 500,
"error": "Internal Server Error",
"message": "No message available",
"trace": "java.lang.NullPointerExceptionntat com.example.FamilyTree.FamilityTreeController.addPerson(FamilityTreeController.java:27)ntat java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)ntat java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)ntat java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)ntat java.base/java.lang.reflect.Method.invoke(Method.java:564)ntat org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:189)ntat org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)ntat org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)ntat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)ntat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800)ntat org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)ntat org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038)ntat org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)ntat org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)ntat org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:908)ntat javax.servlet.http.HttpServlet.service(HttpServlet.java:660)ntat org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)ntat javax.servlet.http.HttpServlet.service(HttpServlet.java:741)ntat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)ntat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)ntat org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)ntat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)ntat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)ntat org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)ntat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)ntat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)ntat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)ntat org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)ntat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)ntat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)ntat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)ntat org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)ntat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)ntat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)ntat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)ntat org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)ntat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)ntat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)ntat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)ntat org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)ntat org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)ntat org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)ntat org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)ntat org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)ntat org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)ntat org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)ntat org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)ntat org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)ntat org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:791)ntat org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1417)ntat org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)ntat java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1135)ntat java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)ntat org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)ntat java.base/java.lang.Thread.run(Thread.java:844)n",
"path": "/api/addPerson"
}
**FamilyTreeApplication class **
package com.example.FamilyTree;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import controller.FamilityTreeController;
@SpringBootApplication
@EnableJpaAuditing
public class FamilyTreeApplication {
public static void main(String args) {
SpringApplication.run(FamilyTreeApplication.class, args);
}
}
updated Packages structure
**cmd **
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaAuditingHandler': Cannot resolve reference to bean 'jpaMappingContext' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: At least one JPA metamodel must be present!
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:378) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:110) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:662) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:188) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1308) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1154) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:538) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:846) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863) ~[spring-context-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546) ~[spring-context-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) ~[spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at com.example.FamilyTree.FamilyTreeApplication.main(FamilyTreeApplication.java:22) [classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[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:564) ~[na:na]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.1.1.RELEASE.jar:2.1.1.RELEASE]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: At least one JPA metamodel must be present!
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1745) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:576) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:367) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
... 26 common frames omitted
Caused by: java.lang.IllegalArgumentException: At least one JPA metamodel must be present!
at org.springframework.util.Assert.notEmpty(Assert.java:464) ~[spring-core-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.data.jpa.mapping.JpaMetamodelMappingContext.<init>(JpaMetamodelMappingContext.java:57) ~[spring-data-jpa-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at org.springframework.data.jpa.repository.config.JpaMetamodelMappingContextFactoryBean.createInstance(JpaMetamodelMappingContextFactoryBean.java:80) ~[spring-data-jpa-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at org.springframework.data.jpa.repository.config.JpaMetamodelMappingContextFactoryBean.createInstance(JpaMetamodelMappingContextFactoryBean.java:44) ~[spring-data-jpa-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at org.springframework.beans.factory.config.AbstractFactoryBean.afterPropertiesSet(AbstractFactoryBean.java:142) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1804) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1741) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
... 33 common frames omitted
java spring maven jpa
add a comment |
im trying to learn how restful service works in java. but i came across this problem which i cant seem to figure out. i'm trying to call this controller to which will take data and store it in a database. but the exception i get is something i haven't came across before. if i remove the model class as a parameter and try to return just a basic string it works fine. but my goal is to store what ever data that is passed to the controller in a database.
enter code here
person.java // model class
@Entity
@Table(name = "Person")
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(value = {"createdAt", "updatedAt"}, allowGetters = true)
public class Person implements Serializable {
@JsonProperty("key")
public int getKey() {
return key;
}
public void setKey(int key) {
this.key = key;
}
@JsonProperty("name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@JsonProperty("m")
public int getMotherKey() {
return motherKey;
}
public void setMotherKey(int motherKey) {
this.motherKey = motherKey;
}
@JsonProperty("f")
public int getFatherKey() {
return fatherKey;
}
public void setFatherKey(int fatherKey) {
this.fatherKey = fatherKey;
}
@JsonProperty("dob")
public int getDateOfBirth() {
return DateOfBirth;
}
public void setDateOfBirth(int dateOfBirth) {
DateOfBirth = dateOfBirth;
}
@JsonProperty("g")
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Person() {
}
@NotNull
int key;
@NotBlank
String name;
@NotNull
int motherKey;
@NotNull
int fatherKey;
@NotNull
int DateOfBirth; //19921210->December 10th 1992
@NotBlank
String gender;
**Controller class **
package com.example.FamilyTree;
import java.util.List;
import javax.validation.Valid;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import Repository.TreeRepository;
import exception.ResourceNotFoundException;
import model.Person;
@RestController
@RequestMapping("/api")
public class FamilityTreeController {
TreeRepository treeRepository;
@PostMapping("/addPerson")
public Person addPerson(@Valid @RequestBody Person person) {
return treeRepository.save( person);
}
}
Exception
at
com.example.FamilyTree.FamilityTreeController.addPerson(FamilityTreeController.java:32) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[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:564) ~[na:na]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:189) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:908) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:660) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.13.jar:9.0.13]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490) [tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) [tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) [tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408) [tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:791) [tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1417) [tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.13.jar:9.0.13]
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1135) [na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) [na:na]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.13.jar:9.0.13]
at java.base/java.lang.Thread.run(Thread.java:844) [na:na]
Respository
package Repository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;
import model.Person;
public interface TreeRepository extends JpaRepository<Person, Integer> {
}
Full stack trace from Postman
{
"timestamp": "2019-01-03T14:06:28.971+0000",
"status": 500,
"error": "Internal Server Error",
"message": "No message available",
"trace": "java.lang.NullPointerExceptionntat com.example.FamilyTree.FamilityTreeController.addPerson(FamilityTreeController.java:27)ntat java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)ntat java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)ntat java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)ntat java.base/java.lang.reflect.Method.invoke(Method.java:564)ntat org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:189)ntat org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)ntat org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)ntat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)ntat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800)ntat org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)ntat org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038)ntat org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)ntat org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)ntat org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:908)ntat javax.servlet.http.HttpServlet.service(HttpServlet.java:660)ntat org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)ntat javax.servlet.http.HttpServlet.service(HttpServlet.java:741)ntat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)ntat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)ntat org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)ntat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)ntat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)ntat org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)ntat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)ntat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)ntat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)ntat org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)ntat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)ntat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)ntat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)ntat org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)ntat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)ntat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)ntat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)ntat org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)ntat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)ntat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)ntat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)ntat org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)ntat org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)ntat org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)ntat org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)ntat org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)ntat org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)ntat org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)ntat org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)ntat org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)ntat org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:791)ntat org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1417)ntat org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)ntat java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1135)ntat java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)ntat org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)ntat java.base/java.lang.Thread.run(Thread.java:844)n",
"path": "/api/addPerson"
}
**FamilyTreeApplication class **
package com.example.FamilyTree;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import controller.FamilityTreeController;
@SpringBootApplication
@EnableJpaAuditing
public class FamilyTreeApplication {
public static void main(String args) {
SpringApplication.run(FamilyTreeApplication.class, args);
}
}
updated Packages structure
**cmd **
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaAuditingHandler': Cannot resolve reference to bean 'jpaMappingContext' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: At least one JPA metamodel must be present!
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:378) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:110) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:662) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:188) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1308) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1154) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:538) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:846) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863) ~[spring-context-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546) ~[spring-context-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) ~[spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at com.example.FamilyTree.FamilyTreeApplication.main(FamilyTreeApplication.java:22) [classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[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:564) ~[na:na]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.1.1.RELEASE.jar:2.1.1.RELEASE]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: At least one JPA metamodel must be present!
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1745) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:576) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:367) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
... 26 common frames omitted
Caused by: java.lang.IllegalArgumentException: At least one JPA metamodel must be present!
at org.springframework.util.Assert.notEmpty(Assert.java:464) ~[spring-core-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.data.jpa.mapping.JpaMetamodelMappingContext.<init>(JpaMetamodelMappingContext.java:57) ~[spring-data-jpa-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at org.springframework.data.jpa.repository.config.JpaMetamodelMappingContextFactoryBean.createInstance(JpaMetamodelMappingContextFactoryBean.java:80) ~[spring-data-jpa-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at org.springframework.data.jpa.repository.config.JpaMetamodelMappingContextFactoryBean.createInstance(JpaMetamodelMappingContextFactoryBean.java:44) ~[spring-data-jpa-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at org.springframework.beans.factory.config.AbstractFactoryBean.afterPropertiesSet(AbstractFactoryBean.java:142) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1804) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1741) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
... 33 common frames omitted
java spring maven jpa
Comments are not for extended discussion; this conversation has been moved to chat.
– Samuel Liew♦
Jan 3 at 23:13
add a comment |
im trying to learn how restful service works in java. but i came across this problem which i cant seem to figure out. i'm trying to call this controller to which will take data and store it in a database. but the exception i get is something i haven't came across before. if i remove the model class as a parameter and try to return just a basic string it works fine. but my goal is to store what ever data that is passed to the controller in a database.
enter code here
person.java // model class
@Entity
@Table(name = "Person")
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(value = {"createdAt", "updatedAt"}, allowGetters = true)
public class Person implements Serializable {
@JsonProperty("key")
public int getKey() {
return key;
}
public void setKey(int key) {
this.key = key;
}
@JsonProperty("name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@JsonProperty("m")
public int getMotherKey() {
return motherKey;
}
public void setMotherKey(int motherKey) {
this.motherKey = motherKey;
}
@JsonProperty("f")
public int getFatherKey() {
return fatherKey;
}
public void setFatherKey(int fatherKey) {
this.fatherKey = fatherKey;
}
@JsonProperty("dob")
public int getDateOfBirth() {
return DateOfBirth;
}
public void setDateOfBirth(int dateOfBirth) {
DateOfBirth = dateOfBirth;
}
@JsonProperty("g")
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Person() {
}
@NotNull
int key;
@NotBlank
String name;
@NotNull
int motherKey;
@NotNull
int fatherKey;
@NotNull
int DateOfBirth; //19921210->December 10th 1992
@NotBlank
String gender;
**Controller class **
package com.example.FamilyTree;
import java.util.List;
import javax.validation.Valid;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import Repository.TreeRepository;
import exception.ResourceNotFoundException;
import model.Person;
@RestController
@RequestMapping("/api")
public class FamilityTreeController {
TreeRepository treeRepository;
@PostMapping("/addPerson")
public Person addPerson(@Valid @RequestBody Person person) {
return treeRepository.save( person);
}
}
Exception
at
com.example.FamilyTree.FamilityTreeController.addPerson(FamilityTreeController.java:32) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[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:564) ~[na:na]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:189) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:908) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:660) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.13.jar:9.0.13]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490) [tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) [tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) [tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408) [tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:791) [tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1417) [tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.13.jar:9.0.13]
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1135) [na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) [na:na]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.13.jar:9.0.13]
at java.base/java.lang.Thread.run(Thread.java:844) [na:na]
Respository
package Repository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;
import model.Person;
public interface TreeRepository extends JpaRepository<Person, Integer> {
}
Full stack trace from Postman
{
"timestamp": "2019-01-03T14:06:28.971+0000",
"status": 500,
"error": "Internal Server Error",
"message": "No message available",
"trace": "java.lang.NullPointerExceptionntat com.example.FamilyTree.FamilityTreeController.addPerson(FamilityTreeController.java:27)ntat java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)ntat java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)ntat java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)ntat java.base/java.lang.reflect.Method.invoke(Method.java:564)ntat org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:189)ntat org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)ntat org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)ntat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)ntat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800)ntat org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)ntat org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038)ntat org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)ntat org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)ntat org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:908)ntat javax.servlet.http.HttpServlet.service(HttpServlet.java:660)ntat org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)ntat javax.servlet.http.HttpServlet.service(HttpServlet.java:741)ntat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)ntat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)ntat org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)ntat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)ntat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)ntat org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)ntat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)ntat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)ntat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)ntat org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)ntat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)ntat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)ntat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)ntat org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)ntat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)ntat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)ntat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)ntat org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)ntat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)ntat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)ntat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)ntat org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)ntat org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)ntat org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)ntat org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)ntat org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)ntat org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)ntat org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)ntat org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)ntat org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)ntat org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:791)ntat org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1417)ntat org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)ntat java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1135)ntat java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)ntat org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)ntat java.base/java.lang.Thread.run(Thread.java:844)n",
"path": "/api/addPerson"
}
**FamilyTreeApplication class **
package com.example.FamilyTree;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import controller.FamilityTreeController;
@SpringBootApplication
@EnableJpaAuditing
public class FamilyTreeApplication {
public static void main(String args) {
SpringApplication.run(FamilyTreeApplication.class, args);
}
}
updated Packages structure
**cmd **
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaAuditingHandler': Cannot resolve reference to bean 'jpaMappingContext' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: At least one JPA metamodel must be present!
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:378) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:110) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:662) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:188) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1308) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1154) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:538) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:846) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863) ~[spring-context-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546) ~[spring-context-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) ~[spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at com.example.FamilyTree.FamilyTreeApplication.main(FamilyTreeApplication.java:22) [classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[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:564) ~[na:na]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.1.1.RELEASE.jar:2.1.1.RELEASE]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: At least one JPA metamodel must be present!
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1745) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:576) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:367) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
... 26 common frames omitted
Caused by: java.lang.IllegalArgumentException: At least one JPA metamodel must be present!
at org.springframework.util.Assert.notEmpty(Assert.java:464) ~[spring-core-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.data.jpa.mapping.JpaMetamodelMappingContext.<init>(JpaMetamodelMappingContext.java:57) ~[spring-data-jpa-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at org.springframework.data.jpa.repository.config.JpaMetamodelMappingContextFactoryBean.createInstance(JpaMetamodelMappingContextFactoryBean.java:80) ~[spring-data-jpa-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at org.springframework.data.jpa.repository.config.JpaMetamodelMappingContextFactoryBean.createInstance(JpaMetamodelMappingContextFactoryBean.java:44) ~[spring-data-jpa-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at org.springframework.beans.factory.config.AbstractFactoryBean.afterPropertiesSet(AbstractFactoryBean.java:142) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1804) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1741) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
... 33 common frames omitted
java spring maven jpa
im trying to learn how restful service works in java. but i came across this problem which i cant seem to figure out. i'm trying to call this controller to which will take data and store it in a database. but the exception i get is something i haven't came across before. if i remove the model class as a parameter and try to return just a basic string it works fine. but my goal is to store what ever data that is passed to the controller in a database.
enter code here
person.java // model class
@Entity
@Table(name = "Person")
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(value = {"createdAt", "updatedAt"}, allowGetters = true)
public class Person implements Serializable {
@JsonProperty("key")
public int getKey() {
return key;
}
public void setKey(int key) {
this.key = key;
}
@JsonProperty("name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@JsonProperty("m")
public int getMotherKey() {
return motherKey;
}
public void setMotherKey(int motherKey) {
this.motherKey = motherKey;
}
@JsonProperty("f")
public int getFatherKey() {
return fatherKey;
}
public void setFatherKey(int fatherKey) {
this.fatherKey = fatherKey;
}
@JsonProperty("dob")
public int getDateOfBirth() {
return DateOfBirth;
}
public void setDateOfBirth(int dateOfBirth) {
DateOfBirth = dateOfBirth;
}
@JsonProperty("g")
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Person() {
}
@NotNull
int key;
@NotBlank
String name;
@NotNull
int motherKey;
@NotNull
int fatherKey;
@NotNull
int DateOfBirth; //19921210->December 10th 1992
@NotBlank
String gender;
**Controller class **
package com.example.FamilyTree;
import java.util.List;
import javax.validation.Valid;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import Repository.TreeRepository;
import exception.ResourceNotFoundException;
import model.Person;
@RestController
@RequestMapping("/api")
public class FamilityTreeController {
TreeRepository treeRepository;
@PostMapping("/addPerson")
public Person addPerson(@Valid @RequestBody Person person) {
return treeRepository.save( person);
}
}
Exception
at
com.example.FamilyTree.FamilityTreeController.addPerson(FamilityTreeController.java:32) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[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:564) ~[na:na]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:189) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:908) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:660) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.13.jar:9.0.13]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490) [tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) [tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) [tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408) [tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:791) [tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1417) [tomcat-embed-core-9.0.13.jar:9.0.13]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.13.jar:9.0.13]
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1135) [na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) [na:na]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.13.jar:9.0.13]
at java.base/java.lang.Thread.run(Thread.java:844) [na:na]
Respository
package Repository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;
import model.Person;
public interface TreeRepository extends JpaRepository<Person, Integer> {
}
Full stack trace from Postman
{
"timestamp": "2019-01-03T14:06:28.971+0000",
"status": 500,
"error": "Internal Server Error",
"message": "No message available",
"trace": "java.lang.NullPointerExceptionntat com.example.FamilyTree.FamilityTreeController.addPerson(FamilityTreeController.java:27)ntat java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)ntat java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)ntat java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)ntat java.base/java.lang.reflect.Method.invoke(Method.java:564)ntat org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:189)ntat org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)ntat org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)ntat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)ntat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800)ntat org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)ntat org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038)ntat org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)ntat org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)ntat org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:908)ntat javax.servlet.http.HttpServlet.service(HttpServlet.java:660)ntat org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)ntat javax.servlet.http.HttpServlet.service(HttpServlet.java:741)ntat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)ntat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)ntat org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)ntat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)ntat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)ntat org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)ntat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)ntat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)ntat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)ntat org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)ntat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)ntat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)ntat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)ntat org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)ntat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)ntat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)ntat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)ntat org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)ntat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)ntat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)ntat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)ntat org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)ntat org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)ntat org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)ntat org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)ntat org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)ntat org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)ntat org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)ntat org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)ntat org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)ntat org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:791)ntat org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1417)ntat org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)ntat java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1135)ntat java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)ntat org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)ntat java.base/java.lang.Thread.run(Thread.java:844)n",
"path": "/api/addPerson"
}
**FamilyTreeApplication class **
package com.example.FamilyTree;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import controller.FamilityTreeController;
@SpringBootApplication
@EnableJpaAuditing
public class FamilyTreeApplication {
public static void main(String args) {
SpringApplication.run(FamilyTreeApplication.class, args);
}
}
updated Packages structure
**cmd **
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaAuditingHandler': Cannot resolve reference to bean 'jpaMappingContext' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: At least one JPA metamodel must be present!
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:378) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:110) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:662) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:188) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1308) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1154) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:538) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:846) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863) ~[spring-context-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546) ~[spring-context-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) ~[spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at com.example.FamilyTree.FamilyTreeApplication.main(FamilyTreeApplication.java:22) [classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[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:564) ~[na:na]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.1.1.RELEASE.jar:2.1.1.RELEASE]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: At least one JPA metamodel must be present!
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1745) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:576) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:367) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
... 26 common frames omitted
Caused by: java.lang.IllegalArgumentException: At least one JPA metamodel must be present!
at org.springframework.util.Assert.notEmpty(Assert.java:464) ~[spring-core-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.data.jpa.mapping.JpaMetamodelMappingContext.<init>(JpaMetamodelMappingContext.java:57) ~[spring-data-jpa-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at org.springframework.data.jpa.repository.config.JpaMetamodelMappingContextFactoryBean.createInstance(JpaMetamodelMappingContextFactoryBean.java:80) ~[spring-data-jpa-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at org.springframework.data.jpa.repository.config.JpaMetamodelMappingContextFactoryBean.createInstance(JpaMetamodelMappingContextFactoryBean.java:44) ~[spring-data-jpa-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at org.springframework.beans.factory.config.AbstractFactoryBean.afterPropertiesSet(AbstractFactoryBean.java:142) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1804) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1741) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
... 33 common frames omitted
java spring maven jpa
java spring maven jpa
edited Jan 3 at 16:16
al_khalid
asked Jan 3 at 13:13
al_khalidal_khalid
92111
92111
Comments are not for extended discussion; this conversation has been moved to chat.
– Samuel Liew♦
Jan 3 at 23:13
add a comment |
Comments are not for extended discussion; this conversation has been moved to chat.
– Samuel Liew♦
Jan 3 at 23:13
Comments are not for extended discussion; this conversation has been moved to chat.
– Samuel Liew♦
Jan 3 at 23:13
Comments are not for extended discussion; this conversation has been moved to chat.
– Samuel Liew♦
Jan 3 at 23:13
add a comment |
3 Answers
3
active
oldest
votes
The problem is that treeRepository
is not injected by Spring and thus is null
.
First, make sure that the TreeRepository
is properly configured by adding @EnableJpaRepositories(basePackages = "<package of your repository interface>")
) to your Spring config class (FamilyTreeApplication
).
Then to inject it properly, you should either add an @Autowired
annotation :
@Autowired
private TreeRepository treeRepository;
or have it injected in a constructor, which is the preferred way according to the Spring team.
private TreeRepository treeRepository;
//@Autowired not needed, implicit as of Spring 4.3
public FamilityTreeController(TreeRepository treeRepository) {
this.treeRepository = treeRepository;
}
Also there's something off with your package hierarchy that might impact how components are found by Spring Boot : your main config class (FamilyTreeApplication
) should be located in the app "base package" and all other classes in sub packages, something like that :
com.example.familytree
|_ FamilyTreeApplication.java
|_ controller
|_ FamilyTreeController.java
|_ repository
|_ TreeRepositry.java
...
2
@al_khalid I don't see@EnableJpaRepositories
in your config class as I suggested.
– Matt
Jan 3 at 15:06
add a comment |
You are missing the @Repository
annotation on your repository:
@Repository
public interface TreeRepository extends JpaRepository<Person, Integer> { }
And remember to add the @Autowired
to inject the repository:
@Autowired
private TreeRepository treeRepository;
UPDATE
It seems Spring is not able to find the bean even with @Repository
added. I would suggest you to put all your packages under com.example
and add a component scan
configuration referencing that package.
adding @autowired causing the following exception org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'familityTreeController': Unsatisfied dependency expressed through field 'treeRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'respository.TreeRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
– al_khalid
Jan 3 at 14:41
Are you using any kind of component scan on your project? Please post it in the question, it seems even with@Repository
added Spring is not able to see it
– ngueno
Jan 3 at 14:43
1
You don't need to add the@Repository
annotation with Spring Data. Make sure, your JpaRepository is configured properly (See my answer)
– Matt
Jan 3 at 14:44
1
@ngueno component scan is done by default with Spring Boot, but it will not find the repository as it should be generated by Spring Data first (and is already to Spring context)
– Matt
Jan 3 at 15:00
1
TheSpringBootApplication
annotation includes@ComponentScan
(amongst other things) and if you don't specify any base package, it will scan from the package of the config class. But it doesn't hurt to add it, because Spring Boot is doing so many things automatically that it's difficult for junior developers to understand what's happening under the hood.
– Matt
Jan 3 at 15:37
|
show 6 more comments
It is best if you use a service class
This annotation serves as a specialization of @Component, allowing for implementation classes to be autodetected through classpath scanning.
@Service
public class TreeService {
@Autowired
TreeRepository treeRepository;
public Person save(Person person) {
return treeRepository.save(person);
}
}
Change your Controller class as this:
@RestController
@RequestMapping("/api")
public class FamilityTreeController {
@Autowired
TreeService treeService;
@PostMapping("/addPerson")
public Person addPerson(@Valid @RequestBody Person person) {
return treeService.save( person);
}
}
Please make the package names lowercase and follow the naming convention:
Change this: Repository.TreeRepository
to this: com.example.repository.TreeRepository
and for others too: com.example.model.Person
com.example.exception.ResourceNotFoundException
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'familityTreeController': Unsatisfied dependency expressed through field 'treeService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.services.TreeService.TreeService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
– al_khalid
Jan 3 at 15:13
i get the following exception when compiling
– al_khalid
Jan 3 at 15:13
com.example.services.TreeService.TreeService
I hope you have created a package with the namecom.example.services.TreeService
andTressService
java class in it.
– Nikhil
Jan 3 at 15:16
It is best if you use the naming convention for packages as lowercase. Uppercase is for class names.
– Nikhil
Jan 3 at 15:17
1
I recommend you follow this tutorial. It is old but really good for learning Spring Boot youtube.com/watch?v=msXL2oDexqw
– Nikhil
Jan 3 at 15:53
|
show 6 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54023019%2fjava-spring-restfull-api%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
The problem is that treeRepository
is not injected by Spring and thus is null
.
First, make sure that the TreeRepository
is properly configured by adding @EnableJpaRepositories(basePackages = "<package of your repository interface>")
) to your Spring config class (FamilyTreeApplication
).
Then to inject it properly, you should either add an @Autowired
annotation :
@Autowired
private TreeRepository treeRepository;
or have it injected in a constructor, which is the preferred way according to the Spring team.
private TreeRepository treeRepository;
//@Autowired not needed, implicit as of Spring 4.3
public FamilityTreeController(TreeRepository treeRepository) {
this.treeRepository = treeRepository;
}
Also there's something off with your package hierarchy that might impact how components are found by Spring Boot : your main config class (FamilyTreeApplication
) should be located in the app "base package" and all other classes in sub packages, something like that :
com.example.familytree
|_ FamilyTreeApplication.java
|_ controller
|_ FamilyTreeController.java
|_ repository
|_ TreeRepositry.java
...
2
@al_khalid I don't see@EnableJpaRepositories
in your config class as I suggested.
– Matt
Jan 3 at 15:06
add a comment |
The problem is that treeRepository
is not injected by Spring and thus is null
.
First, make sure that the TreeRepository
is properly configured by adding @EnableJpaRepositories(basePackages = "<package of your repository interface>")
) to your Spring config class (FamilyTreeApplication
).
Then to inject it properly, you should either add an @Autowired
annotation :
@Autowired
private TreeRepository treeRepository;
or have it injected in a constructor, which is the preferred way according to the Spring team.
private TreeRepository treeRepository;
//@Autowired not needed, implicit as of Spring 4.3
public FamilityTreeController(TreeRepository treeRepository) {
this.treeRepository = treeRepository;
}
Also there's something off with your package hierarchy that might impact how components are found by Spring Boot : your main config class (FamilyTreeApplication
) should be located in the app "base package" and all other classes in sub packages, something like that :
com.example.familytree
|_ FamilyTreeApplication.java
|_ controller
|_ FamilyTreeController.java
|_ repository
|_ TreeRepositry.java
...
2
@al_khalid I don't see@EnableJpaRepositories
in your config class as I suggested.
– Matt
Jan 3 at 15:06
add a comment |
The problem is that treeRepository
is not injected by Spring and thus is null
.
First, make sure that the TreeRepository
is properly configured by adding @EnableJpaRepositories(basePackages = "<package of your repository interface>")
) to your Spring config class (FamilyTreeApplication
).
Then to inject it properly, you should either add an @Autowired
annotation :
@Autowired
private TreeRepository treeRepository;
or have it injected in a constructor, which is the preferred way according to the Spring team.
private TreeRepository treeRepository;
//@Autowired not needed, implicit as of Spring 4.3
public FamilityTreeController(TreeRepository treeRepository) {
this.treeRepository = treeRepository;
}
Also there's something off with your package hierarchy that might impact how components are found by Spring Boot : your main config class (FamilyTreeApplication
) should be located in the app "base package" and all other classes in sub packages, something like that :
com.example.familytree
|_ FamilyTreeApplication.java
|_ controller
|_ FamilyTreeController.java
|_ repository
|_ TreeRepositry.java
...
The problem is that treeRepository
is not injected by Spring and thus is null
.
First, make sure that the TreeRepository
is properly configured by adding @EnableJpaRepositories(basePackages = "<package of your repository interface>")
) to your Spring config class (FamilyTreeApplication
).
Then to inject it properly, you should either add an @Autowired
annotation :
@Autowired
private TreeRepository treeRepository;
or have it injected in a constructor, which is the preferred way according to the Spring team.
private TreeRepository treeRepository;
//@Autowired not needed, implicit as of Spring 4.3
public FamilityTreeController(TreeRepository treeRepository) {
this.treeRepository = treeRepository;
}
Also there's something off with your package hierarchy that might impact how components are found by Spring Boot : your main config class (FamilyTreeApplication
) should be located in the app "base package" and all other classes in sub packages, something like that :
com.example.familytree
|_ FamilyTreeApplication.java
|_ controller
|_ FamilyTreeController.java
|_ repository
|_ TreeRepositry.java
...
edited Jan 3 at 15:17
answered Jan 3 at 14:41
MattMatt
1,77711522
1,77711522
2
@al_khalid I don't see@EnableJpaRepositories
in your config class as I suggested.
– Matt
Jan 3 at 15:06
add a comment |
2
@al_khalid I don't see@EnableJpaRepositories
in your config class as I suggested.
– Matt
Jan 3 at 15:06
2
2
@al_khalid I don't see
@EnableJpaRepositories
in your config class as I suggested.– Matt
Jan 3 at 15:06
@al_khalid I don't see
@EnableJpaRepositories
in your config class as I suggested.– Matt
Jan 3 at 15:06
add a comment |
You are missing the @Repository
annotation on your repository:
@Repository
public interface TreeRepository extends JpaRepository<Person, Integer> { }
And remember to add the @Autowired
to inject the repository:
@Autowired
private TreeRepository treeRepository;
UPDATE
It seems Spring is not able to find the bean even with @Repository
added. I would suggest you to put all your packages under com.example
and add a component scan
configuration referencing that package.
adding @autowired causing the following exception org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'familityTreeController': Unsatisfied dependency expressed through field 'treeRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'respository.TreeRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
– al_khalid
Jan 3 at 14:41
Are you using any kind of component scan on your project? Please post it in the question, it seems even with@Repository
added Spring is not able to see it
– ngueno
Jan 3 at 14:43
1
You don't need to add the@Repository
annotation with Spring Data. Make sure, your JpaRepository is configured properly (See my answer)
– Matt
Jan 3 at 14:44
1
@ngueno component scan is done by default with Spring Boot, but it will not find the repository as it should be generated by Spring Data first (and is already to Spring context)
– Matt
Jan 3 at 15:00
1
TheSpringBootApplication
annotation includes@ComponentScan
(amongst other things) and if you don't specify any base package, it will scan from the package of the config class. But it doesn't hurt to add it, because Spring Boot is doing so many things automatically that it's difficult for junior developers to understand what's happening under the hood.
– Matt
Jan 3 at 15:37
|
show 6 more comments
You are missing the @Repository
annotation on your repository:
@Repository
public interface TreeRepository extends JpaRepository<Person, Integer> { }
And remember to add the @Autowired
to inject the repository:
@Autowired
private TreeRepository treeRepository;
UPDATE
It seems Spring is not able to find the bean even with @Repository
added. I would suggest you to put all your packages under com.example
and add a component scan
configuration referencing that package.
adding @autowired causing the following exception org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'familityTreeController': Unsatisfied dependency expressed through field 'treeRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'respository.TreeRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
– al_khalid
Jan 3 at 14:41
Are you using any kind of component scan on your project? Please post it in the question, it seems even with@Repository
added Spring is not able to see it
– ngueno
Jan 3 at 14:43
1
You don't need to add the@Repository
annotation with Spring Data. Make sure, your JpaRepository is configured properly (See my answer)
– Matt
Jan 3 at 14:44
1
@ngueno component scan is done by default with Spring Boot, but it will not find the repository as it should be generated by Spring Data first (and is already to Spring context)
– Matt
Jan 3 at 15:00
1
TheSpringBootApplication
annotation includes@ComponentScan
(amongst other things) and if you don't specify any base package, it will scan from the package of the config class. But it doesn't hurt to add it, because Spring Boot is doing so many things automatically that it's difficult for junior developers to understand what's happening under the hood.
– Matt
Jan 3 at 15:37
|
show 6 more comments
You are missing the @Repository
annotation on your repository:
@Repository
public interface TreeRepository extends JpaRepository<Person, Integer> { }
And remember to add the @Autowired
to inject the repository:
@Autowired
private TreeRepository treeRepository;
UPDATE
It seems Spring is not able to find the bean even with @Repository
added. I would suggest you to put all your packages under com.example
and add a component scan
configuration referencing that package.
You are missing the @Repository
annotation on your repository:
@Repository
public interface TreeRepository extends JpaRepository<Person, Integer> { }
And remember to add the @Autowired
to inject the repository:
@Autowired
private TreeRepository treeRepository;
UPDATE
It seems Spring is not able to find the bean even with @Repository
added. I would suggest you to put all your packages under com.example
and add a component scan
configuration referencing that package.
edited Jan 3 at 14:45
answered Jan 3 at 14:35
nguenongueno
96411019
96411019
adding @autowired causing the following exception org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'familityTreeController': Unsatisfied dependency expressed through field 'treeRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'respository.TreeRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
– al_khalid
Jan 3 at 14:41
Are you using any kind of component scan on your project? Please post it in the question, it seems even with@Repository
added Spring is not able to see it
– ngueno
Jan 3 at 14:43
1
You don't need to add the@Repository
annotation with Spring Data. Make sure, your JpaRepository is configured properly (See my answer)
– Matt
Jan 3 at 14:44
1
@ngueno component scan is done by default with Spring Boot, but it will not find the repository as it should be generated by Spring Data first (and is already to Spring context)
– Matt
Jan 3 at 15:00
1
TheSpringBootApplication
annotation includes@ComponentScan
(amongst other things) and if you don't specify any base package, it will scan from the package of the config class. But it doesn't hurt to add it, because Spring Boot is doing so many things automatically that it's difficult for junior developers to understand what's happening under the hood.
– Matt
Jan 3 at 15:37
|
show 6 more comments
adding @autowired causing the following exception org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'familityTreeController': Unsatisfied dependency expressed through field 'treeRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'respository.TreeRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
– al_khalid
Jan 3 at 14:41
Are you using any kind of component scan on your project? Please post it in the question, it seems even with@Repository
added Spring is not able to see it
– ngueno
Jan 3 at 14:43
1
You don't need to add the@Repository
annotation with Spring Data. Make sure, your JpaRepository is configured properly (See my answer)
– Matt
Jan 3 at 14:44
1
@ngueno component scan is done by default with Spring Boot, but it will not find the repository as it should be generated by Spring Data first (and is already to Spring context)
– Matt
Jan 3 at 15:00
1
TheSpringBootApplication
annotation includes@ComponentScan
(amongst other things) and if you don't specify any base package, it will scan from the package of the config class. But it doesn't hurt to add it, because Spring Boot is doing so many things automatically that it's difficult for junior developers to understand what's happening under the hood.
– Matt
Jan 3 at 15:37
adding @autowired causing the following exception org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'familityTreeController': Unsatisfied dependency expressed through field 'treeRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'respository.TreeRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
– al_khalid
Jan 3 at 14:41
adding @autowired causing the following exception org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'familityTreeController': Unsatisfied dependency expressed through field 'treeRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'respository.TreeRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
– al_khalid
Jan 3 at 14:41
Are you using any kind of component scan on your project? Please post it in the question, it seems even with
@Repository
added Spring is not able to see it– ngueno
Jan 3 at 14:43
Are you using any kind of component scan on your project? Please post it in the question, it seems even with
@Repository
added Spring is not able to see it– ngueno
Jan 3 at 14:43
1
1
You don't need to add the
@Repository
annotation with Spring Data. Make sure, your JpaRepository is configured properly (See my answer)– Matt
Jan 3 at 14:44
You don't need to add the
@Repository
annotation with Spring Data. Make sure, your JpaRepository is configured properly (See my answer)– Matt
Jan 3 at 14:44
1
1
@ngueno component scan is done by default with Spring Boot, but it will not find the repository as it should be generated by Spring Data first (and is already to Spring context)
– Matt
Jan 3 at 15:00
@ngueno component scan is done by default with Spring Boot, but it will not find the repository as it should be generated by Spring Data first (and is already to Spring context)
– Matt
Jan 3 at 15:00
1
1
The
SpringBootApplication
annotation includes @ComponentScan
(amongst other things) and if you don't specify any base package, it will scan from the package of the config class. But it doesn't hurt to add it, because Spring Boot is doing so many things automatically that it's difficult for junior developers to understand what's happening under the hood.– Matt
Jan 3 at 15:37
The
SpringBootApplication
annotation includes @ComponentScan
(amongst other things) and if you don't specify any base package, it will scan from the package of the config class. But it doesn't hurt to add it, because Spring Boot is doing so many things automatically that it's difficult for junior developers to understand what's happening under the hood.– Matt
Jan 3 at 15:37
|
show 6 more comments
It is best if you use a service class
This annotation serves as a specialization of @Component, allowing for implementation classes to be autodetected through classpath scanning.
@Service
public class TreeService {
@Autowired
TreeRepository treeRepository;
public Person save(Person person) {
return treeRepository.save(person);
}
}
Change your Controller class as this:
@RestController
@RequestMapping("/api")
public class FamilityTreeController {
@Autowired
TreeService treeService;
@PostMapping("/addPerson")
public Person addPerson(@Valid @RequestBody Person person) {
return treeService.save( person);
}
}
Please make the package names lowercase and follow the naming convention:
Change this: Repository.TreeRepository
to this: com.example.repository.TreeRepository
and for others too: com.example.model.Person
com.example.exception.ResourceNotFoundException
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'familityTreeController': Unsatisfied dependency expressed through field 'treeService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.services.TreeService.TreeService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
– al_khalid
Jan 3 at 15:13
i get the following exception when compiling
– al_khalid
Jan 3 at 15:13
com.example.services.TreeService.TreeService
I hope you have created a package with the namecom.example.services.TreeService
andTressService
java class in it.
– Nikhil
Jan 3 at 15:16
It is best if you use the naming convention for packages as lowercase. Uppercase is for class names.
– Nikhil
Jan 3 at 15:17
1
I recommend you follow this tutorial. It is old but really good for learning Spring Boot youtube.com/watch?v=msXL2oDexqw
– Nikhil
Jan 3 at 15:53
|
show 6 more comments
It is best if you use a service class
This annotation serves as a specialization of @Component, allowing for implementation classes to be autodetected through classpath scanning.
@Service
public class TreeService {
@Autowired
TreeRepository treeRepository;
public Person save(Person person) {
return treeRepository.save(person);
}
}
Change your Controller class as this:
@RestController
@RequestMapping("/api")
public class FamilityTreeController {
@Autowired
TreeService treeService;
@PostMapping("/addPerson")
public Person addPerson(@Valid @RequestBody Person person) {
return treeService.save( person);
}
}
Please make the package names lowercase and follow the naming convention:
Change this: Repository.TreeRepository
to this: com.example.repository.TreeRepository
and for others too: com.example.model.Person
com.example.exception.ResourceNotFoundException
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'familityTreeController': Unsatisfied dependency expressed through field 'treeService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.services.TreeService.TreeService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
– al_khalid
Jan 3 at 15:13
i get the following exception when compiling
– al_khalid
Jan 3 at 15:13
com.example.services.TreeService.TreeService
I hope you have created a package with the namecom.example.services.TreeService
andTressService
java class in it.
– Nikhil
Jan 3 at 15:16
It is best if you use the naming convention for packages as lowercase. Uppercase is for class names.
– Nikhil
Jan 3 at 15:17
1
I recommend you follow this tutorial. It is old but really good for learning Spring Boot youtube.com/watch?v=msXL2oDexqw
– Nikhil
Jan 3 at 15:53
|
show 6 more comments
It is best if you use a service class
This annotation serves as a specialization of @Component, allowing for implementation classes to be autodetected through classpath scanning.
@Service
public class TreeService {
@Autowired
TreeRepository treeRepository;
public Person save(Person person) {
return treeRepository.save(person);
}
}
Change your Controller class as this:
@RestController
@RequestMapping("/api")
public class FamilityTreeController {
@Autowired
TreeService treeService;
@PostMapping("/addPerson")
public Person addPerson(@Valid @RequestBody Person person) {
return treeService.save( person);
}
}
Please make the package names lowercase and follow the naming convention:
Change this: Repository.TreeRepository
to this: com.example.repository.TreeRepository
and for others too: com.example.model.Person
com.example.exception.ResourceNotFoundException
It is best if you use a service class
This annotation serves as a specialization of @Component, allowing for implementation classes to be autodetected through classpath scanning.
@Service
public class TreeService {
@Autowired
TreeRepository treeRepository;
public Person save(Person person) {
return treeRepository.save(person);
}
}
Change your Controller class as this:
@RestController
@RequestMapping("/api")
public class FamilityTreeController {
@Autowired
TreeService treeService;
@PostMapping("/addPerson")
public Person addPerson(@Valid @RequestBody Person person) {
return treeService.save( person);
}
}
Please make the package names lowercase and follow the naming convention:
Change this: Repository.TreeRepository
to this: com.example.repository.TreeRepository
and for others too: com.example.model.Person
com.example.exception.ResourceNotFoundException
answered Jan 3 at 15:05
NikhilNikhil
399614
399614
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'familityTreeController': Unsatisfied dependency expressed through field 'treeService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.services.TreeService.TreeService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
– al_khalid
Jan 3 at 15:13
i get the following exception when compiling
– al_khalid
Jan 3 at 15:13
com.example.services.TreeService.TreeService
I hope you have created a package with the namecom.example.services.TreeService
andTressService
java class in it.
– Nikhil
Jan 3 at 15:16
It is best if you use the naming convention for packages as lowercase. Uppercase is for class names.
– Nikhil
Jan 3 at 15:17
1
I recommend you follow this tutorial. It is old but really good for learning Spring Boot youtube.com/watch?v=msXL2oDexqw
– Nikhil
Jan 3 at 15:53
|
show 6 more comments
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'familityTreeController': Unsatisfied dependency expressed through field 'treeService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.services.TreeService.TreeService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
– al_khalid
Jan 3 at 15:13
i get the following exception when compiling
– al_khalid
Jan 3 at 15:13
com.example.services.TreeService.TreeService
I hope you have created a package with the namecom.example.services.TreeService
andTressService
java class in it.
– Nikhil
Jan 3 at 15:16
It is best if you use the naming convention for packages as lowercase. Uppercase is for class names.
– Nikhil
Jan 3 at 15:17
1
I recommend you follow this tutorial. It is old but really good for learning Spring Boot youtube.com/watch?v=msXL2oDexqw
– Nikhil
Jan 3 at 15:53
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'familityTreeController': Unsatisfied dependency expressed through field 'treeService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.services.TreeService.TreeService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
– al_khalid
Jan 3 at 15:13
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'familityTreeController': Unsatisfied dependency expressed through field 'treeService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.services.TreeService.TreeService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
– al_khalid
Jan 3 at 15:13
i get the following exception when compiling
– al_khalid
Jan 3 at 15:13
i get the following exception when compiling
– al_khalid
Jan 3 at 15:13
com.example.services.TreeService.TreeService
I hope you have created a package with the name com.example.services.TreeService
and TressService
java class in it.– Nikhil
Jan 3 at 15:16
com.example.services.TreeService.TreeService
I hope you have created a package with the name com.example.services.TreeService
and TressService
java class in it.– Nikhil
Jan 3 at 15:16
It is best if you use the naming convention for packages as lowercase. Uppercase is for class names.
– Nikhil
Jan 3 at 15:17
It is best if you use the naming convention for packages as lowercase. Uppercase is for class names.
– Nikhil
Jan 3 at 15:17
1
1
I recommend you follow this tutorial. It is old but really good for learning Spring Boot youtube.com/watch?v=msXL2oDexqw
– Nikhil
Jan 3 at 15:53
I recommend you follow this tutorial. It is old but really good for learning Spring Boot youtube.com/watch?v=msXL2oDexqw
– Nikhil
Jan 3 at 15:53
|
show 6 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54023019%2fjava-spring-restfull-api%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Comments are not for extended discussion; this conversation has been moved to chat.
– Samuel Liew♦
Jan 3 at 23:13