StackOverflowError 来自于对仓库中 .save() 方法的循环调用。

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

StackOverflowError from cycling call of .save() method from repository

问题

我有一个“简单的问题”,但我在哪里出错了我不知道。
我有一个在Java 11、Spring Boot 2.3.3和H2数据库上运行的简单的CRUD Web应用程序。
问题是,每当我进行HTTP POST请求以调用.save()方法时,我会收到以下错误。

    java.lang.StackOverflowError: null
    ...

从我理解的情况来看,这是一个循环问题,可能是多次调用.save()方法导致了这个错误。

我的领域实体类:

@Entity
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Darwiner {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotBlank
    private String darwinerName;
}

我的Repository接口:

@Repository
public interface DarwinerRepository extends CrudRepository<darwiner, Long> {
}

我的CrudService接口:

public interface CrudService<T, ID> {

    Set<T> findAll();

    T findById(ID id);

    T save(T object);

    void deleteById(ID id);
}

Repository接口的实际实现:

@Service
public class DarwinerRepositoryImpl implements CrudService<Darwiner, Long> {

    private final DarwinerRepository darwinerRepository;

    public DarwinerRepositoryImpl(DarwinerRepository darwinerRepository) {
        this.darwinerRepository = darwinerRepository;
    }

    @Override
    public Set<Darwiner> findAll() {
        Set<Darwiner> darwins = new HashSet<>();
        darwinerRepository.findAll().forEach(darwins::add);
        return darwins;
    }

    @Override
    public Darwiner findById(Long id) {
        return darwinerRepository.findById(id).orElse(new Darwiner());
    }

    @Override
    public Darwiner save(Darwiner darwiner) {
        return darwinerRepository.save(darwiner);
    }

    @Override
    public void deleteById(Long darwiner) {
        darwinerRepository.deleteById(darwiner);
    }
}

最后是我的控制器类:

@RestController
@RequestMapping("/darwiner")
public class DarwinerController {

    private final DarwinerRepositoryImpl darwinerRepositoryImpl;

    public DarwinerController(DarwinerRepositoryImpl darwinerRepositoryImpl) {
        this.darwinerRepositoryImpl = darwinerRepositoryImpl;
    }

    @GetMapping(value = "/allDarwins", produces = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody Set<Darwiner> findAllDarwinerNames(){
        return darwinerRepositoryImpl.findAll();
    }

    @GetMapping(value = "/darwinNameById/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody Darwiner findDarwinerNameById(@RequestBody @PathVariable @Validated Long id){
        return darwinerRepositoryImpl.findById(id);
    }

    @PostMapping(value = "/saveDarwinName", consumes = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody ResponseEntity saveDarwinerName(@RequestBody Darwiner darwin){
        darwinerRepositoryImpl.save(darwiner);
        return ResponseEntity.ok(HttpStatus.OK + " Darwiner name has been successfully saved to database");
    }

    @DeleteMapping(value = "/delete/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody ResponseEntity deleteDarwinerNameById(@RequestBody @PathVariable Long id){
        darwinerRepositoryImpl.deleteById(id);
        return ResponseEntity.ok(HttpStatus.OK + " Darwin has been successfully removed");
    }
}
英文:

I have a "simple problem" but I am struggling to spot where am I making a mistake.
I have a simple CRUD web application running on Java 11, Spring Boot 2.3.3 and H2 DB
The problem is whenever I make a HTTP POST request that is invoking the .save() method I get this error.

java.lang.StackOverflowError: null
at org.springframework.transaction.interceptor.DefaultTransactionAttribute.rollbackOn(DefaultTransactionAttribute.java:135) ~[spring-tx-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.transaction.interceptor.RuleBasedTransactionAttribute.rollbackOn(RuleBasedTransactionAttribute.java:157) ~[spring-tx-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.transaction.interceptor.DelegatingTransactionAttribute.rollbackOn(DelegatingTransactionAttribute.java:58) ~[spring-tx-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.completeTransactionAfterThrowing(TransactionAspectSupport.java:649) ~[spring-tx-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:371) ~[spring-tx-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:118) ~[spring-tx-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139) ~[spring-tx-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:160) ~[spring-data-jpa-2.3.3.RELEASE.jar:2.3.3.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:95) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at com.sun.proxy.$Proxy95.save(Unknown Source) ~[na:na]
at jdk.internal.reflect.GeneratedMethodAccessor40.invoke(Unknown Source) ~[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:566) ~[na:na]
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:205) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at com.sun.proxy.$Proxy70.save(Unknown Source) ~[na:na]
at com.dtisk.leanx.service.DarwinerRepositoryImpl.save(DarwinerRepositoryImpl.java:41) ~[classes/:na]
at com.dtisk.leanx.service.DarwinerRepositoryImpl.save(DarwinerRepositoryImpl.java:14) ~[classes/:na]
at jdk.internal.reflect.GeneratedMethodAccessor41.invoke(Unknown Source) ~[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:566) ~[na:na]
at org.springframework.data.repository.core.support.ImplementationInvocationMetadata.invoke(ImplementationInvocationMetadata.java:72) ~[spring-data-commons-2.3.3.RELEASE.jar:2.3.3.RELEASE]
at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:382) ~[spring-data-commons-2.3.3.RELEASE.jar:2.3.3.RELEASE]
at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:205) ~[spring-data-commons-2.3.3.RELEASE.jar:2.3.3.RELEASE]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:549) ~[spring-data-commons-2.3.3.RELEASE.jar:2.3.3.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:155) ~[spring-data-commons-2.3.3.RELEASE.jar:2.3.3.RELEASE]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:130) ~[spring-data-commons-2.3.3.RELEASE.jar:2.3.3.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:367) ~[spring-tx-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:118) ~[spring-tx-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139) ~[spring-tx-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:160) ~[spring-data-jpa-2.3.3.RELEASE.jar:2.3.3.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:95) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at com.sun.proxy.$Proxy95.save(Unknown Source) ~[na:na]
at jdk.internal.reflect.GeneratedMethodAccessor40.invoke(Unknown Source) ~[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:566) ~[na:na]
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:205) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at com.sun.proxy.$Proxy70.save(Unknown Source) ~[na:na]
at com.dtisk.leanx.service.DarwinerRepositoryImpl.save(DarwinerRepositoryImpl.java:41) ~[classes/:na]
at com.dtisk.leanx.service.DarwinerRepositoryImpl.save(DarwinerRepositoryImpl.java:14) ~[classes/:na]
at jdk.internal.reflect.GeneratedMethodAccessor41.invoke(Unknown Source) ~[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:566) ~[na:na]
at org.springframework.data.repository.core.support.ImplementationInvocationMetadata.invoke(ImplementationInvocationMetadata.java:72) ~[spring-data-commons-2.3.3.RELEASE.jar:2.3.3.RELEASE]
at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:382) ~[spring-data-commons-2.3.3.RELEASE.jar:2.3.3.RELEASE]
at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:205) ~[spring-data-commons-2.3.3.RELEASE.jar:2.3.3.RELEASE]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:549) ~[spring-data-commons-2.3.3.RELEASE.jar:2.3.3.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:155) ~[spring-data-commons-2.3.3.RELEASE.jar:2.3.3.RELEASE]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:130) ~[spring-data-commons-2.3.3.RELEASE.jar:2.3.3.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:367) ~[spring-tx-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:118) ~[spring-tx-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139) ~[spring-tx-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:160) ~[spring-data-jpa-2.3.3.RELEASE.jar:2.3.3.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:95) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at com.sun.proxy.$Proxy95.save(Unknown Source) ~[na:na]
at jdk.internal.reflect.GeneratedMethodAccessor40.invoke(Unknown Source) ~[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:566) ~[na:na]
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:205) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at com.sun.proxy.$Proxy70.save(Unknown Source) ~[na:na]
at com.dtisk.leanx.service.DarwinerRepositoryImpl.save(DarwinerRepositoryImpl.java:41) ~[classes/:na]
at com.dtisk.leanx.service.DarwinerRepositoryImpl.save(DarwinerRepositoryImpl.java:14) ~[classes/:na]
at jdk.internal.reflect.GeneratedMethodAccessor41.invoke(Unknown Source) ~[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:566) ~[na:na]
at org.springframework.data.repository.core.support.ImplementationInvocationMetadata.invoke(ImplementationInvocationMetadata.java:72) ~[spring-data-commons-2.3.3.RELEASE.jar:2.3.3.RELEASE]
at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:382) ~[spring-data-commons-2.3.3.RELEASE.jar:2.3.3.RELEASE]
at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:205) ~[spring-data-commons-2.3.3.RELEASE.jar:2.3.3.RELEASE]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:549) ~[spring-data-commons-2.3.3.RELEASE.jar:2.3.3.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:155) ~[spring-data-commons-2.3.3.RELEASE.jar:2.3.3.RELEASE]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:130) ~[spring-data-commons-2.3.3.RELEASE.jar:2.3.3.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:367) ~[spring-tx-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:118) ~[spring-tx-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139) ~[spring-tx-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:160) ~[spring-data-jpa-2.3.3.RELEASE.jar:2.3.3.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:95) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at com.sun.proxy.$Proxy95.save(Unknown Source) ~[na:na]
at jdk.internal.reflect.GeneratedMethodAccessor40.invoke(Unknown Source) ~[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:566) ~[na:na]
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:205) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at com.sun.proxy.$Proxy70.save(Unknown Source) ~[na:na]
at com.dtisk.leanx.service.DarwinerRepositoryImpl.save(DarwinerRepositoryImpl.java:41) ~[classes/:na]
at com.dtisk.leanx.service.DarwinerRepositoryImpl.save(DarwinerRepositoryImpl.java:14) ~[classes/:na]

From what I understand it is a cycling problem, probably with calling the .save() method multiple times
and this causes this error.

My domain entity class:

@Entity
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Darwiner {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank
private String darwinerName;
}

My Repository interface:

@Repository
public interface DarwinerRepository extends CrudRepository&lt;Darwiner, Long&gt; {
}

My CrudService interface:

public interface CrudService&lt;T, ID&gt; {
Set&lt;T&gt; findAll();
T findById(ID id);
T save(T object);
void deleteById(ID id);
}

The actual implementation of repository intefaces:

@Service
public class DarwinerRepositoryImpl implements CrudService&lt;Darwiner, Long&gt; {
private final DarwinerRepository darwinerRepository;
public DarwinerRepositoryImpl(DarwinerRepository darwinerRepository) {
this.darwinerRepository = darwinerRepository;
}
@Override
public Set&lt;Darwiner&gt; findAll() {
Set&lt;Darwiner&gt; darwins = new HashSet&lt;&gt;();
darwinerRepository.findAll().forEach(darwins::add);
return darwins;
}
@Override
public Darwiner findById(Long id) {
return darwinRepository.findById(id).orElse(new Darwin());
}
@Override
public Darwiner save(Darwiner darwiner) {
return darwinerRepository.save(darwiner);
}
@Override
public void deleteById(Long darwiner) {
darwinRepository.deleteById(darwiner);
}
}

and finally my controller class:

@RestController
@RequestMapping(&quot;/darwiner&quot;)
public class DarwinerController {
private final DarwinerRepositoryImpl darwinerRepositoryImpl;
public DarwinerController(DarwinerRepositoryImpl darwinerRepositoryImpl) {
this.darwinerRepositoryImpl = darwinerRepositoryImpl;
}
@GetMapping(value = &quot;/allDarwins&quot;, produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody Set&lt;Darwiner&gt; findAllDarwinerNames(){
return darwinerRepositoryImpl.findAll();
}
@GetMapping(value = &quot;/darwinNameById/{id}&quot;, produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody Darwiner findDarwinerNameById(@RequestBody @PathVariable @Validated Long id){
return darwinerRepositoryImpl.findById(id);
}
@PostMapping(value = &quot;/saveDarwinName&quot;, consumes = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody ResponseEntity saveDarwinerName(@RequestBody Darwiner darwin){
darwinerRepositoryImpl.save(darwiner);
return ResponseEntity.ok(HttpStatus.OK + &quot; Darwiner name has been successfully saved to database&quot;);
}
@DeleteMapping(value = &quot;/delete/{id}&quot;, produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody ResponseEntity deleteDarwinerNameById(@RequestBody @PathVariable Long id){
darwinerRepositoryImpl.deleteById(id);
return ResponseEntity.ok(HttpStatus.OK + &quot; Darwin has been successfully removed&quot;);
}

答案1

得分: 4

能否将DarwinerRepositoryImpl重新命名?这种命名模式在Spring Data中是特殊的。

英文:

Could you rename DarwinerRepositoryImpl? This naming pattern is special in Spring Data.

答案2

得分: 2

你必须将DarwinerRepository的名称更改为其他名称,因为Spring使用名称DarwinerRepositoryImpl来查找接口DarwinerRepository的实现。在你的情况下,DarwinerRepositoryImpl不是DarwinerRepository的实现。我已经在以下URL上提供了一个示例:https://github.com/andresetevejob/demostackoverflow
我已将名称DarwinerRepository更改为TestRepository。

英文:

You must change the name of DarwinerRepository in other name because Spring use the name DarwinerRepositoryImpl to find the implementation of the interface DarwinerRepository.In your case DarwinerRepositoryImpl is not the implementation of DarwinerRepository.I have do a an example to this url : https://github.com/andresetevejob/demostackoverflow
I have change the name DarwinerRepository to TestRepository

答案3

得分: 0

在你的 DarwinerRepositoryImpl 类中,我没有找到任何关于 DarwinerRepository@Autowired 注入的 bean。
我认为你需要像下面这样为 bean darwinerRepository 添加 @Autowired 注解。

@Service
public class DarwinerRepositoryImpl implements CrudService<Darwiner, Long> {

    private final DarwinerRepository darwinerRepository;

    @Autowired
    public DarwinerRepositoryImpl(DarwinerRepository darwinerRepository) {
        this.darwinerRepository = darwinerRepository;
    }

    @Override
    public Set<Darwiner> findAll() {
        Set<Darwiner> darwins = new HashSet<>();
        darwinerRepository.findAll().forEach(darwins::add);
        return darwins;
    }

    @Override
    public Darwiner findById(Long id) {
        return darwinRepository.findById(id).orElse(new Darwin());
    }

    @Override
    public Darwiner save(Darwiner darwiner) {
        return darwinerRepository.save(darwiner);
    }

    @Override
    public void deleteById(Long darwiner) {
        darwinRepository.deleteById(darwiner);
    }
}
英文:

In your DarwinerRepositoryImpl class I am not getting any @Autowired bean for DarwinerRepository.<br/>
I believe you need to add the @Autowired for bean darwinerRepository injection as shown below.

@Service
public class DarwinerRepositoryImpl implements CrudService&lt;Darwiner, Long&gt; {
private final DarwinerRepository darwinerRepository;
@Autowired
public DarwinerRepositoryImpl(DarwinerRepository darwinerRepository) {
this.darwinerRepository = darwinerRepository;
}
@Override
public Set&lt;Darwiner&gt; findAll() {
Set&lt;Darwiner&gt; darwins = new HashSet&lt;&gt;();
darwinerRepository.findAll().forEach(darwins::add);
return darwins;
}
@Override
public Darwiner findById(Long id) {
return darwinRepository.findById(id).orElse(new Darwin());
}
@Override
public Darwiner save(Darwiner darwiner) {
return darwinerRepository.save(darwiner);
}
@Override
public void deleteById(Long darwiner) {
darwinRepository.deleteById(darwiner);
}
}

huangapple
  • 本文由 发表于 2020年9月4日 03:43:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63730634.html
匿名

发表评论

匿名网友

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

确定