Spring Boot 将@deleteMapping添加到Sping Boot Rest服务会导致UnsatisifedDependencyException和NullPointerException

jdgnovmf  于 2023-03-29  发布在  Spring
关注(0)|答案(1)|浏览(114)

我正在做一个Sping Boot Rest服务,当我添加删除Map时遇到了一个问题。在我添加它之前,代码工作正常,运行没有问题,现在当我尝试使用mvn clean package在shell中编译它时,我得到了下面的错误。
错误:

java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'eligibleAddOnController': Unsatisfied dependency expressed through field 'eligibilityRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'eligibilityRepository' defined in pack.psl.repository.EligibilityRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract void pack.psl.repository.EligibilityRepository.deleteAddOn(java.lang.String,java.lang.String); Reason: null; nested exception is java.lang.NullPointerException
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'eligibilityRepository' defined in nz.one.psl.repository.EligibilityRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract void pack.psl.repository.EligibilityRepository.deleteAddOn(java.lang.String,java.lang.String); Reason: null; nested exception is java.lang.NullPointerException
Caused by: org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract void pack.psl.repository.EligibilityRepository.deleteAddOn(java.lang.String,java.lang.String); Reason: null; nested exception is java.lang.NullPointerException
Caused by: java.lang.NullPointerException

这是我的代码
适用范围:

package pack.psl;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class TcsApplication {

    public static void main(String[] args) {
        SpringApplication.run(TcsApplication.class, args);
    }

}

型号:

package pack.model;

import javax.persistence.*;
import javax.validation.constraints.*;

@Entity
@Table(name = "DEALS")
public class EligibleAddOn {
    
    @Id
    @NotNull
    private String deals_id;
    @NotNull
    private String cust_id;
    @NotNull
    private String addon_name;
    @NotNull
    private String eligibility_from_date;
    @NotNull
    private String eligibility_to_date;

    public EligibleAddOn() {
        super();
    }

    public String getDeals_id() {
        return deals_id;
    }

    public void setDeals_id(String deals_id) {
        this.deals_id = deals_id;
    }

    public String getCust_id() {
        return cust_id;
    }

    public void setCust_id(String cust_id) {
        this.cust_id = cust_id;
    }

    public String getAddon_Name() {
        return addon_name;
    }

    public void setAddon_Name(String addon_Name) {
        this.addon_name = addon_Name;
    }

    public String getEligibility_from_date() {
        return eligibility_from_date;
    }

    public void setEligibility_from_date(String eligibility_from_date) {
        this.eligibility_from_date = eligibility_from_date;
    }

    public String getEligibility_to_date() {
        return eligibility_to_date;
    }

    public void setEligibility_to_date(String eligibility_to_date) {
        this.eligibility_to_date = eligibility_to_date;
    }

}

储存库:

package pack.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import pack.model.EligibleAddOn;

public interface EligibilityRepository extends JpaRepository<EligibleAddOn, String> {
    @Transactional
    @Query(value = "select * from DEALS "
            + "where CUST_ID IN "
            + "(select CUST_ID from TCS_CUSTOMER where msisdn = ?) "
            + "and ELIGIBILITY_FROM_DATE < current_timestamp "
            + "and current_timestamp < ELIGIBILITY_TO_DATE "
            + "ORDER BY DISPLAY_PRIORITY", nativeQuery = true)
    List<EligibleAddOn> findByMsisdn(String msisdn);

    @Modifying
    @Transactional
    @Query(value = "delete from DEALS where " +
    "CUST_ID in (select CUST_ID from TCS_CUSTOMER " +
    "where MSISDN = :msisdn) and addon_name" + " = :addon_name", nativeQuery 
    =true) void deleteAddOn(@Param("msisdn")String
    msisdn, @Param("addon_name")String addon_name);

}

控制器:

package pack.controller;

import pack.exception.EligibleAddOnNotFoundException;
import pack.model.EligibleAddOn;
import pack.repository.EligibilityRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/tcs")
public class EligibleAddOnController {

    @Autowired
    EligibilityRepository eligibilityRepository;

// Get a Single deal

    @GetMapping("/eligibility-list/msisdn/{msisdn}")
    public List<EligibleAddOn> getEligibleAddOnByMsisdn(@PathVariable(value = "msisdn") String msisdn)
            throws EligibleAddOnNotFoundException {
        return eligibilityRepository.findByMsisdn(msisdn); // .orElseThrow(() -> new
                                                            // EligibleAddOnNotFoundException(msisdn));

    }

// Delete a deal
@DeleteMapping("/eligibility-list/msisdn/{msisdn}/radon/{addon_name}") public
     ResponseEntity<?> deleteEligibleAddOn(@PathVariable("msisdn") String
     msisdn,@PathVariable("addon_name") String addon_name){
     
     eligibilityRepository.deleteAddOn(msisdn, addon_name);
     
     return ResponseEntity.ok().build(); }
     
}

Application.properties

spring.jpa.hibernate.ddl-auto=none
spring.datasource.url= datasource
spring.datasource.username= username
spring.datasource.password= password
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.jpa.database-platform=org.hibernate.dialect.Oracle12cDialect
server.error.include-stacktrace = always
mzsu5hc0

mzsu5hc01#

原来问题出在 Spring Boot 本身而不是代码上。
按照这个https://github.com/spring-projects/spring-boot/issues/34363降级我的项目从2.7.9到2.6.6解决了这个问题。

相关问题